Friday 16 August 2013

SharePoint 2013 - Querying Property Bags with Search

Property bags in SharePoint are a great way to store simple data with list items or webs. There are a few blog posts about making property bag entries searchable like this one but I couldn't find anything about being able to query them. This means that if you have a web with a property bag key and value set, the web would be returned when you search for that value (along with anywhere else it appears) but you can't search for webs where your key is set to your value. To allow this you need to set up a managed property associated to your property bag key.

  1. Set a property bag key/value pair using powershell as in the link above or using the object model like:
    myWeb.AllProperties[“myspecialkey”] = “myawesomevalue";
  2. Set it to be indexed and then update the web:
    myWeb.IndexedPropertyKeys.Add(“myspecialkey”);
    myWeb.Update();
  3. Carry out a full crawl by going to Search Administration in Central Administration, selecting Content Sources and selecting Start Full Crawl from the drop down menu of your content source.
  4. Once this has completed, go to Search Schema in Search Administration and select Crawled Properties. Search for your key (“myspecialkey”) and make sure it was indexed. If it was, you will be able to search for your value ("myawesomevalue") from your SharePoint site but this will return any results that match (e.g. web titles, list items, document metadata), you won’t be able to search for only results where your key is set to a value.
  5. To enable searching for results where your key is set to a value you need to add a managed property.
    1. In Search Administration go to Search Schema and click New Managed Property
    2. Enter a property name for the new managed property (the same name as your key is easiest)
    3. In “Mappings to crawled properties” click “Add a Mapping” and search and add your property bag key
    4. Make sure Queryable is selected. This is what lets you get only results where your key is equal to a value. If you want your key and value to be returned in the search results (when querying using one of the APIs), make sure Retrievable is enabled
    5. Select any other characteristics that you require and click OK
  6. After another full crawl you can search for key=value or key:value from a site and you should only get back results where your key is equal to your value. If you add this key to any other webs, they will be indexed when the next crawl runs without having to make any further changes in Search Administration.