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 storing them elsewhere. It stores configuration data in the Hash-Table (Key-Value) format. SharePoint allows storing configuration settings at different levels, such as site collection, site, list, and list items. Here is my collection of PowerShell scripts to work with Property bag in SharePoint Online:
PowerShell to Get a Key-Value from SharePoint Online Property Bag
To check the property bag key, use this PowerShell in SharePoint Online:
#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://Crescent.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:
To check all property bag keys in SharePoint Online, use this PowerShell script:
#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://Crescent.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 the 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://Crescent.sharepoint.com/"
$Key = "FollowLinkEnabled"
$Value="False"
#Call the function to Update Property bag Key value
Set-SPOPropertyBag -SiteURL $SiteURL -Key $Key -Value $Value
Last but not least: You can use SharePoint Designer to manipulate items in a property bag! Just open the site in SharePoint Designer and click on the “Site Options” button in the ribbon to get the property bag editor!
Read or Write Property Bag Values using PnP PowerShell
Here is how we can read a value from the property bag in SharePoint Online using the Get-PnPPropertyBag cmdlet.
#Variable
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
#Connect to PnP Online
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)
#Get the Tenant Site Object
Get-PnPPropertyBag -Key "allowdesigner"
PnP PowerShell To Update a Property Bag Value in SharePoint Online
Here is the PnP PowerShell to set property bag in SharePoint Online:
Set-PnPPropertyBagValue -Key "allowdesigner" -Value "1"
In summary, getting or setting the property bag in SharePoint Online using PowerShell is a breeze with this guide. By following the steps and best practices outlined in this article, you can easily retrieve or update property bag values and unlock the full potential of SharePoint Online.