Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phillipharding/2e6ad1e9be8e1316e9af to your computer and use it in GitHub Desktop.
Save phillipharding/2e6ad1e9be8e1316e9af to your computer and use it in GitHub Desktop.
Set a PropertyBag property as Indexable (Queryable via Search) using CSOM + Powershell
function Set-PropertyBag {
param (
[string]$PropertyName,
[string]$PropertyValue = $null,
[bool]$Indexable = $false,
[Microsoft.SharePoint.Client.Web]$Web,
[Microsoft.SharePoint.Client.ClientContext]$ClientContext
)
process {
$indexedPropertyBagKey = "vti_indexedpropertykeys"
if($Indexable) {
$ClientContext.Load($Web.AllProperties)
$ClientContext.ExecuteQuery()
$oldIndexedProperties = $Web.AllProperties.FieldValues[$indexedPropertyBagKey]
if($oldIndexedProperties -ne $null) {
$oldIndexedProperties = $oldIndexedProperties.ToString()
} else {
$oldIndexedProperties = ""
}
$propertyNameBytes = [System.Text.Encoding]::Unicode.GetBytes($PropertyName)
$encodedPropertyName = [Convert]::ToBase64String($propertyNameBytes)
if($oldIndexedProperties -notlike "*$encodedPropertyName*") {
$Web.AllProperties[$indexedPropertyBagKey] = "$oldIndexedProperties$encodedPropertyName|"
}
}
$Web.AllProperties[$PropertyName] = $PropertyValue
$Web.Update()
$ClientContext.Load($Web)
$ClientContext.Load($Web.AllProperties)
$ClientContext.ExecuteQuery()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment