What Are Indexed Property Bags And How To Use Them For SharePoint Sites

What is a PropertyBag?

 
PropertyBags are simple Key-Value pairs which are available on any SharePoint Web object that is persisted. These are primarily used to stored certain properties and configurations for a SharePoint site.
 
Below is an example of a property bag value which SharePoint internally uses to find the GUID of the site’s associated group.
  1. GroupType   Private  
  2. GroupId     9421c7ba-d9a6-4fa8-8fde-b726055f267c 
Similarly, we can define custom property bag values which we can use for building custom solutions.
 

What is an Indexed Property Bag?

 
These are similar to standard property bag values, that are also searchable. Indexed property bags are stored in the same collection of the standard Property Bag, and then we set the key to be indexed. Then SharePoint Search crawler picks these keys and creates crawled properties.
 

How to set an Indexed Property Bag Value?


Below are the different ways of setting a property bag and making it indexed.
 
Using CSOM
  1. string siteUrl = "https://tenant-name.sharepoint.com/sites/internal";  
  2. var authManager = new OfficeDevPnP.Core.AuthenticationManager();  
  3. ClientContext ctx = authManager.GetWebLoginClientContext(siteUrl);  
  4.   
  5. Web w = ctx.Web;  
  6. w.SetPropertyBagValue("customSiteType""ProjectSpace");  
  7. w.AddIndexedPropertyBagKey("customSiteType");  
  8. ctx.ExecuteQuery(); 
In the above code, a key called "customSiteType" is added to the property bag first. Then, the key is added to the IndexedPropertyBagKey collection which is another property bag named “vti_indexedpropertykeys” that stores the values of the property bag keys (to be indexed) encoded in base64 and separated by | (pipe character).
 
Using PnP PowerShell
  1. Connect-PnPOnline -Url $siteUrl  
  2. Set-PnPPropertyBagValue -Key "customSiteType" -Value "ProjectSpace" -Indexed 

Enable Custom Scripts for Modern Sites

 
For adding/updating the property bag values for modern sites, we have to enable Custom Scripts before setting the property bag values.

Using CSOM

  1. string siteUrl = "https://tenant-name.sharepoint.com/sites/internal";  
  2. var authManager = new OfficeDevPnP.Core.AuthenticationManager();  
  3. ClientContext ctx = authManager.GetWebLoginClientContext("provide the tenant admin url");  
  4.   
  5. Microsoft.Online.SharePoint.TenantAdministration.Tenant tenant = new Microsoft.Online.SharePoint.TenantAdministration.Tenant(ctx);  
  6. // Set false to enable custom scripts  
  7. // Set true to disable custom scripts  
  8. tenant.SetSiteProperties(siteUrl, noScriptSite: false);  
  9. ctx.ExecuteQuery(); 

Using PnPPowerShell

  1. Connect-PnPOnline -Url "https://tenantname-admin.sharepoint.com"  
  2. $site = Get-PnPTenantSite "https://tenantname.sharepoint.com/sites/internal" -Detailed  
  3. # Disabled - Allows Custom Script  
  4. # Enabled - Does not allow custom script  
  5. $site.DenyAddAndCustomizePages = "Disabled"  
  6. $site.Update()  
  7. Invoke-PnPQuery 

How can we use an Indexed PropertyBag?

 
The indexed property bags can be used to stored metadata of sites which are included in the search index. These can be used to search for sites and filter based on this metadata.

For example, we could set an Indexed Property Bag called ‘customSiteType’ which is used to define a classification for a site based on your organization needs. And then search for sites of a specific custom site type. Below are the steps for creating this

  1. As explained above set an indexed property bag value for some sites in your tenant with Key as customSiteType and value as ProjectSpace (or any other value)
  2. Set the site to be re-indexed so that the change in the propertybag collection is picked up for indexing. You can go to Site Settings -> Search and offline availability -> and click on Reindex site button, or use the below PnP Powershell cmdlet to reindex the site
    1. Request-PnPReIndexWeb
  3.  The re-index request could take a few hours to be done. Go to the ‘Search Schema’ and check if a crawled property with the name ‘customSiteType’ is created. If the site is not yet re-indexed by the search crawl, wait for it to complete



  4. Open the Search Administration portal and edit one of the existing managed properties of type RefinableString,


  5. Set the Alias as ‘customSiteType’

  6. And add a mapping to include the crawled property,


  7. After this, search has to perform a re-crawl again to include the changes for Managed Property. So, re-index the site so that search picks up the changes and performs the re-indexing.
  8. After the search index is updated, we can use this refiner to search for sites with this property value
A normal search for sites shows all the sites,
 
 
A search with the managed property (based on Indexed Property Bag Value) shows only those sites which have the property bag value,
 
 

Summary

 
We have seen how to manage indexed PropertyBag values for a SharePoint site using PnP PowerShell and CSOM and effectively used the indexed property bags to define custom site classifications based on the organizational needs. These indexed properties are then used to create Managed Properties within the Search schema which can be used for querying Sites with a specific custom classification type.