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.
- GroupType Private
- 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
- string siteUrl = "https://tenant-name.sharepoint.com/sites/internal";
- var authManager = new OfficeDevPnP.Core.AuthenticationManager();
- ClientContext ctx = authManager.GetWebLoginClientContext(siteUrl);
-
- Web w = ctx.Web;
- w.SetPropertyBagValue("customSiteType", "ProjectSpace");
- w.AddIndexedPropertyBagKey("customSiteType");
- 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
- Connect-PnPOnline -Url $siteUrl
- 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
- string siteUrl = "https://tenant-name.sharepoint.com/sites/internal";
- var authManager = new OfficeDevPnP.Core.AuthenticationManager();
- ClientContext ctx = authManager.GetWebLoginClientContext("provide the tenant admin url");
-
- Microsoft.Online.SharePoint.TenantAdministration.Tenant tenant = new Microsoft.Online.SharePoint.TenantAdministration.Tenant(ctx);
-
-
- tenant.SetSiteProperties(siteUrl, noScriptSite: false);
- ctx.ExecuteQuery();
Using PnPPowerShell
- Connect-PnPOnline -Url "https://tenantname-admin.sharepoint.com"
- $site = Get-PnPTenantSite "https://tenantname.sharepoint.com/sites/internal" -Detailed
- # Disabled - Allows Custom Script
- # Enabled - Does not allow custom script
- $site.DenyAddAndCustomizePages = "Disabled"
- $site.Update()
- 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
-
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)
- 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
- 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
- Open the Search Administration portal and edit one of the existing managed properties of type RefinableString,
- Set the Alias as ‘customSiteType’
- And add a mapping to include the crawled property,
- 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.
- 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.