A big thanks to Phil Harding who posted an Article explaining how a property bag can actually be indexed as part of the SharePoint Search Service. While Phil provided the core of the implementation I just wanted to show the code needed to run his snippet.

$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

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

#Ensure you update these to your specific environment
$webUrl = "https://YOURSITENAME.sharepoint.com/";
$username = "YOURUSER@YOURSITENAME.onmicrosoft.com"
$password = ConvertTo-SecureString "YOURPASSWORD" -asplaintext -force

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl) 
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)

$web = $ctx.Web
$webs = $web.Webs
$lists = $web.Lists 

$ctx.Load($lists)
$ctx.Load($webs)
$ctx.Load($web)
$ctx.ExecuteQuery()

#$lists | select -Property Title

Set-PropertyBag "MYPROPNAME" "MYVAL" $true $web $ctx

Be sure to set the username, url, and password as indicated within the code to your specific environment.

No Comments

There are no comments related to this article.