Last active
October 20, 2015 08:51
-
-
Save phillipharding/2e6ad1e9be8e1316e9af to your computer and use it in GitHub Desktop.
Set a PropertyBag property as Indexable (Queryable via Search) using CSOM + Powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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