SharePoint Online: Get-Set Property Bag Values with PowerShell

Property bag in SharePoint Online is a quick and convenient way to store and retrieve custom configurations, instead of having to store them in somewhere else. It stores configuration data in the Hash-Table (Key-Value) format. SharePoint allows storing configurations settings at different levels such as site collection, site, list, list item. Here is my collection of PowerShell scripts to work with Property bag in SharePoint Online:

PowerShell to Get a Key Value in SharePoint Online Property Bag:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Function to Get a particular key value in Property Bag
Function Get-SPOPropertyBagKey($SiteURL, $Key)
{
    #Setup Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
     
    #Get All Properties of the web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.AllProperties)
    $Ctx.ExecuteQuery()
 
    #Get if the Key exists in Property bag
    If ($Web.AllProperties.FieldValues.ContainsKey($Key) -eq $True)
    {
        #Update the Key value in Property bag
        $Value = $Web.AllProperties[$Key]
 
        Write-Host -f Green "Value of the Property Bag Key '$Key':" $Value
    }
    Else
    {
        Write-Host -f Yellow "Could not find Property Bag Key:" $Key
    }
}
 
#Variables
$SiteURL="https://crescenttech.sharepoint.com/"
$Key ="NoCrawl"
 
#Call the function to Get the Property bag Key value
Get-SPOPropertyBagKey -SiteURL $SiteURL -Key $Key

Get All Property Bag Values of a SharePoint Online Site using PowerShell:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Variables
$SiteURL="https://crescenttech.sharepoint.com"
 
Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
 
    #Get the Web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    #Get Property bag of the web
    $SiteProperties = $Web.AllProperties
    $Ctx.Load($SiteProperties)
    $Ctx.ExecuteQuery()

    #$SiteProperties.FieldValues
    #Iterate through each Property
    $PropertyBagKeys = $SiteProperties.FieldValues.Keys
    Foreach($Key in $PropertyBagKeys)
    {
        Write-Host $Key : $SiteProperties[$Key]
    }
}
Catch {
    write-host -f Red "Error Getting Property Bag Values!" $_.Exception.Message
}

Update Property Bag Value using PowerShell in SharePoint Online
Here is the SharePoint Online PowerShell to set property bag value
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Function to Set Property Bag Field Value in SharePoint Online Site
Function Set-SPOPropertyBag($SiteURL, $Key, $Value)
{
    #Setup Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
     
    #Get All Properties of the web
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.AllProperties)
    $Ctx.ExecuteQuery()
 
    #Get if the Key exists in Property bag
    If ($Web.AllProperties.FieldValues.ContainsKey($Key) -eq $True)
    {
        #Update the Key value in Property bag
        $Web.AllProperties[$Key] = $Value
        $web.Update()
        $Ctx.ExecuteQuery()
 
        Write-Host -f Green "Property Bag Key '$Key' Value Updated to: " $Value
    }
    Else
    {
        Write-Host -f Yellow "Could not find Property Bag Key:" $Key
    }
}
 
#variable
$SiteURL ="https://crescenttech.sharepoint.com/"
$Key = "FollowLinkEnabled"
$Value="False"
 
#Call the function to Update Property bag Key value
Set-SPOPropertyBag -SiteURL $SiteURL -Key $Key -Value $Value

Please note, if custom scripting is disabled, you can't update property bag values! You may get ""Access denied. You do not have permission to perform this action or access this resource." error. Fix is here: How to Enable Custom Script in SharePoint Online?

Last but not least: You can use SharePoint Designer to manipulate items in property bag! Just open the site in SharePoint Designer and click on "Site Options" button in the ribbon to get property bag editor!
sharepoint online powershell property bag

SharePoint Online: Get-Set Property Bag Values with PowerShell SharePoint Online: Get-Set Property Bag Values with PowerShell Reviewed by Salaudeen Rajack on March 21, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.