I have multiple subsites and each subsite has a list called "Articles". Each has two content types. "News Articles" and "Success Stories". I would like to search based on the content types above using csom search api.

This is my code so far, but I don't get anything back.

var queryText = "ContentTypeId:0x010066FA3DE3E334C841B418C82CC475A227";
        var clientContext = new SP.ClientContext("http://dev:5050");
        var contextSite = clientContext.get_site();
        var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext); 
        var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);  
        var results = searchExecutor.executeQuery(keywordQuery); 
        clientContext.executeQueryAsync(onQuerySuccess, onQueryError);

        function onQuerySuccess()
            var message = $("#message");
            message.text("Search results fro \"" + queryText + "\"");
            message.append("<br />");
            var rows = results.m_value.ResultTables[0].ResultRows;
            $.each(rows, function(index, value)
                message.append(value.Title + ": " + value.Path);
                message.append("<br />");

How can I search based on multiple content types? Or based on specific list name?


I found out that by place an asterisk at the end of the content type id like so: 0x010066FA3DE3E334C841B418C82CC475A227*, it works. Now, my question is: Why do I have to place this asterisk at the end?


Answering your updated question, as to why you need an asterisk for it to work:

Because 0x010066FA3DE3E334C841B418C82CC475A227 is an ID of a site level content type. When you add a site content type to a list, SharePoint makes a local copy on the list itself.

From this article on MSDN (emphasis mine):

In addition, SharePoint Foundation makes a local copy of the site content type and adds the copy to the content type collection on the list. The new list content type is a child of the site content type. The value of the Id property for the list content type is different from the value of the Id property for its parent site content type, but otherwise the two content types are initially the same.

List level child content types have IDs that start with the parent site content type ID, plus two zeros (00), and then another GUID. (See this article on MSDN explaining content type IDs and their construction.)

So essentially, no list items that are of your content type will have that ID, because items are in lists, and once the content type is copied to the list, it gets a new ID at the list level.

So when you add the asterisk in your search query, you are effectively searching for all items who's content type ID starts with the ID specified, which means all child content types of the content type who's ID you are specifying.

Which is, of course, exactly what you want.

| improve this answer | |
  • yes, that makes sense and I don't know why I had forgotten this fact. – Imir Hoxha Aug 25 '15 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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