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()
}

PowerShell to Insert Field to List View at Specific Location

Added a new field (AddressLine1) to a list, and needed to display the field in all list views. It needed to always display before another field (AddressLine2).

Script below loops through all views of a list, and if the AddressLine2 field exists, it will add AddressLine1 just before it.

Note: Can’t use ForEach loop to go through each list view as the .Update() will throw an error, due to restrictions in updating collection items while enumerating.

Continue reading

Reordering SharePoint Fields using PowerShell

The order in which fields are displayed in the New, Edit, and Display forms for a list item is determined by the FieldLinks order of its content type.

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlinkcollection_members(v=office.15).aspx

The SPFieldLinkCollection has a handy Reorder method that we can use to set the order of the fields, using their internal names. 

The script below does this through PowerShell:

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