Uploading Files to SharePoint Master Page Gallery using PowerShell

PowerShell script below uploads a CSS and JS file to the Master Page Gallery, assuming the files are in the “Files” subfolder from where the script is executed, and existing files in SharePoint will be overwritten.

Param(
    [string]$siteUrl 
)

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) 
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    write-host "Added SharePoint PowerShell snap-in" -ForegroundColor yellow
}

function Get-PSScriptRoot
{
    try
    {
        $scriptRoot = Get-Variable -Name PSScriptRoot -ValueOnly -ErrorAction Stop
    }
    catch
    {
        $scriptRoot = Split-Path $script:MyInvocation.MyCommand.Path
    }

    Write-Output $scriptRoot 
}

try
{
    $site = Get-SPSite $siteUrl
    $web = $site.RootWeb
    $list = $web.GetCatalog(116)
    $scriptPath = Get-PSScriptRoot

    # Upload CSS
    Write-Host "Uploading styles.css..." -NoNewLine
    $file = Get-ChildItem $scriptPath\Files\styles.css
    $spFile = $list.RootFolder.Files.Add("_catalogs/masterpage/css/styles.css", $file.OpenRead(), $true)
    $spFile.Publish("Uploaded from PowerShell")
    Write-Host "Done" -ForegroundColor Green
    
    Write-Host "Uploading scripts.js..." -NoNewLine
    $file = Get-ChildItem $scriptPath\Files\scripts.js
    $spFile = $list.RootFolder.Files.Add("_catalogs/masterpage/js/scripts.js", $file.OpenRead(), $true)
    $spFile.Publish("Uploaded from PowerShell")
    Write-Host "Done" -ForegroundColor Green
}
catch
{
    Write-Host "Failed: " $_.Exception.Message -ForegroundColor Red
    Write-Host $_.Exception.StackTrace -ForegroundColor Red
}

Advertisements

Setting ImageFieldValue using PowerShell

As per title:

$site = Get-SPSite $siteUrl
$web = $site.RootWeb
$list = $web.Lists.TryGetList("Images List")

if ($list -ne $null)
{
  $newItem = $list.AddItem()
  $newItem["Title"] = "New Image Item"
  
  $image = New-Object Microsoft.SharePoint.Publishing.Fields.ImageFieldValue
  $image.AlternateText = "Image of something"
  $image.ImageUrl = "/PublishingImages/something.png"
  $newItem["ImageField"] = [Microsoft.SharePoint.Publishing.Fields.ImageFieldValue]$image
  $newItem.Update()
}

SharePoint Search Expired Claims Cookie

We had an issue with a client where their SharePoint search crawls were periodically crawling successfully, but then on other subsequent crawls show a lot of warnings, saying “The claims cookie used by the crawler has expired.”

Expired Claims Cookie

For this web application, we had the Default zone set up with Windows Authentication for the search crawler, and an ADFS Trusted Identity Provider for users to log in through a custom login page.

The client also had a requirement where logins have to expire after an hour, so the CookieLifetime setting in the Security Token Service Config for the farm had been modified to 1 hour from 5 days.

Continue reading

Running Content Iterator in PowerShell

PowerShell can be very useful to quickly write up a script when you need to say update a field value for items in a list.

But what happens if your list happens to have 100,000+ items? Doing SPList.Items is going to get throttled.

And while PowerShell doesn’t have an equivalent for the ContentIterator class (which can iterate through large data sets while avoiding throwing a SPQueryThrottledException), we can run custom code through PowerShell that utilizes ContentIterator.

The script below does a simple field update for items of a specific content type in a list.

Continue reading

Deleting Site Columns

When trying to delete a site column, you may get the error “Site columns which are included in content types cannot be deleted. Remove all references to this site column prior to deleting it.”

Delete Site Column Error

This simply means that the site column needs to be manually deleted from content types before it can be properly removed.

You may have already manually done so on all content types you know of that refers to the site column, but the error still persists.

This is because it not only needs to be deleted from content types on the site collection level, but on all site level content types as well!

Continue reading

Getting SharePoint Config Database Connection String

Had an issue at a client SharePoint environment where all SharePoint sites stopped working one day, with the error Cannot connect to the configuration database.

Server architecture documentation was a few years old, and didn’t have the current SQL server name. Can’t get it through Central Administration or psconfig either because they can’t open when they can’t connect to the Config DB.

So how do you find the connection string that SharePoint uses to connect to the Config DB?

It’s in the registry 🙂

Continue reading

Wrong Results from Metadata Filtering in Grouped List View

This issue is similar to the problem reported at https://social.technet.microsoft.com/Forums/office/en-US/50001a2f-bf80-44fb-b8c9-4835cc15b7f6/problem-with-metadata-navigation-key-filters-and-grouped-views.

The error happens when you’ve enabled the Metadata Navigation and Filtering feature, and configured a list with Metadata filters, and has a list view that is grouped by a column:

  • If the Group By is set to Collapsed by default, then when you apply a Metadata Filter and expand a group, it brings back all items instead of filtered results.
  • If the Group By is set to Expanded, applying a Metadata Filter works fine, until you go to the next page, and again all items are returned instead of filtered results.

While Microsoft released a fix for this in the SharePoint 2013 July 2014 CU, it seems that there is still 1 edge case where this error is happening:

  • The Group By column you’re using is a Lookup field

As of March 2017, it seems to be happening in SharePoint Online as well.

Continue reading