I'm doing a recommendation engine and I need to access the Enterprise Keywords of a document and search for other documents having at least 1 matching keyword.

So for example I have Document A which has the following Enterprise Keywords:

  1. Keyword X
  2. Keyword Y
  3. Keyword Z

Then I need to search the whole Site collection for documents having at least one of those keywords. If it's possible to search for all of those 3 tags with an OR and returning all the documents that have at least one match ordered by number of matches, that would be great. If not, then I can do it one tag per query.

I cannot find how to query only for Enterprise Keywords.

So, which is the proper and fastest way of doing this? (The site is not using FAST).

Thank you.

2 Answers 2


Without development you can try add a static query to the content search result webpart: tags:"Keyword X" or tags:"Keyword Y" or tags:"Keyword Z" - tags should be the managed property for the Enterprise Keyword column.

With development you have to use the KeywordQueryClass, see: http://msdn.microsoft.com/en-us/library/ms544561%28office.12%29.aspx

ServerContext context = ServerContext.GetContext(HttpContext.Current);    

using (KeywordQuery keywordQuery = new KeywordQuery(context))
        keywordQuery.ResultTypes = ResultType.RelevantResults;
        keywordQuery.EnableStemming = true;
        keywordQuery.TrimDuplicates = true;
        keywordQuery.StartRow = 0;

        keywordQuery.QueryText = string.Format(CultureInfo.InvariantCulture, "tags:\"{0} OR tags:{1} OR tags:{2}\"", "Keyword A", "Keyword B","Keyword C");
        keywordQuery.SelectProperties.Add("tags"); //add the properties you want

        ResultTableCollection resultsCollection = keywordQuery.Execute();

        ResultTable resultsTable = resultsCollection[ResultType.RelevantResults];  }
  • And where exactly does that query go? SPQuery?
    – empz
    Commented Feb 26, 2013 at 22:24
  • My answer was for the searchbox or for a content result webpart with static query. Commented Feb 28, 2013 at 8:37
  • I have to do it programatically.
    – empz
    Commented Feb 28, 2013 at 17:59
  • I updated my answer. Commented Mar 1, 2013 at 14:45

This is what I ended up doing:

var file = Web.GetFile(fileId);
var tags = new TaxonomyFieldValueCollection(file.Item.Properties["TaxKeyword"].ToString());

var query = new SPSiteDataQuery();

// build the query
var sbQuery = new StringBuilder();
sbQuery.Append("<Where><In><FieldRef Name='TaxKeyword' /><Values>");
foreach (var tag in tags)
    sbQuery.AppendFormat("<Value Type='Text'>{0}</Value>", tag.Label);

string sViewFields = "<FieldRef Name='UniqueId' /><FieldRef Name='FileRef' /><FieldRef Name='AverageRating' />";

// Search in doclibs only
query.Lists = "<Lists ServerTemplate='101' MaxListLimit='0' />";
query.Query = sbQuery.ToString();
query.ViewFields = sViewFields;
// Search in all webs of the site collection
query.Webs = "<Webs Scope='SiteCollection' />";

// Perform the query
var table = Web.GetSiteData(query);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.