Make Property Bags Searchable in SharePoint 2013

A new cool thing in SharePoint 2013 is adding properties to a property bag and getting them indexed and searchable. SharePoint 2013 makes this possible, not only for webs, but even all the way down to an SPList.

Here is the PowerShell you can use to create an “Searchable” property in an SPWeb property bag:

$web = Get-SPWeb http://mytest.com
$web.AllProperties["Searchable"] = "Yes"
$web.IndexedPropertyKeys.Add("Searchable")
$web.Update()

Here is another exmample for adding an indexable property to a list:

$list = $web.Lists["Announcements"]
$folder = $list.RootFolder
$folder.Properties["ListSearchable"] = "Cool"
$folder.Update()
$list.IndexedRootFolderPropertyKeys.Add("ListSearchable")
$list.Update()

Then, you just need a Full or incremental crawl.  Make sure the crawled property is created in the search service application. Create Managed Property that maps to the crawled property.

Note:  The IndexedPropertyKey value is also saved in AllProperties using key (vti_indexedpropertykeys).  The value is base 64 encoded so if you want to update it, you’ll need to decode from and encode to base 64 string. However, this SPWeb.IndexedPropertyKeys property is not available for CSOM. Vesa provides an alternative solution in his post FTC to CAM – Setting indexed property bag keys using CSOM

// Used to convert the list of property keys is required format for listing keys to be index
public static string GetEncodedValueForSearchIndexProperty(List keys)
{
    StringBuilder stringBuilder = new StringBuilder();
    foreach (string current in keys)
    {
        stringBuilder.Append(Convert.ToBase64String(Encoding.Unicode.GetBytes(current)));
        stringBuilder.Append('|');
    }
    return stringBuilder.ToString();
}
// Decode the IndexedPropertyKeys value so it's readable
public static List GetDecodeValueForSearchIndexProperty(string encodedValue)
{
     List decodedKeys = new List();
     string[] keys = encodedValue.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            
     foreach (string current in keys)
     {
          decodedKeys.Add(Encoding.Unicode.GetString(Convert.FromBase64String(current)));
     }

     return decodedKeys;
}

Update Property Bag – Add / Remove

string indexedPropertyKeys = web.AllProperties["vti_indexedpropertykeys"].ToString();

List decodedKeys = GetDecodeValueForSearchIndexProperty(indexedPropertyKeys);
decodedKeys.Add(key);     //decodedKeys.Remove(key);

web.AllProperties["vti_indexedpropertykeys"] = GetEncodedValueForSearchIndexProperty(decodedKeys);
web.Update();
clientContext.ExecuteQuery();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s