I create multiple Lists using Content type in different sites.I want to get all Items from this Content type using Client context object(Caml Query) in C#.


There is no easy way for this.

If you try to query individual lists it will take a lot of time and will affect performance.

Your best bet would be to use the Search API.

Try and modify the below sample code. You can add the necessary filters as per your requirement.:

using Microsoft.SharePoint.Client; 
using Microsoft.SharePoint.Client.Search; 
using Microsoft.SharePoint.Client.Search.Query;

using (ClientContext clientContext = new ClientContext("https://sitecollectionurl"))
    KeywordQuery keywordQuery = new KeywordQuery(clientContext);
    keywordQuery.QueryText = "ContentTypeId:0x0100EEA1F5E3F2E1* path:'https://yoursitecollectionpath'";
    SearchExecutor searchExecutor = new SearchExecutor(clientContext);
    ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);

    foreach (var resultRow in results.Value[0].ResultRows) 
        Console.WriteLine("{0}: {1} ({2})", resultRow["Title"], resultRow["Path"], resultRow["Write"]); 

You can your custom columns as managed properties.

Replace the content type id and path with that of your respective id and url.


This may be possible using: GetSubwebsForCurrentUser (https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.web.getsubwebsforcurrentuser.aspx), using this CSOM query with a CAML query allows you to find all sites' lists that match a given filter.

I have used it often to find all lists in all sub sites containing items that for example the current user owns:

(Sorry JSOM code example not CSOM, but it can be translated / adapted to the MSDN example from the link above)

var myCtx = SP.ClientContext.get_current();
var root = myCtx.get_site().get_rootWeb(); 

// only need to fetch the first item returned from each list
var query = new SP.CamlQuery();
    '<View Scope="RecursiveAll"><Query>'
    + '<Where>'
    + '<And>'
    + '<Eq><FieldRef Name=\"' + listFilterUserId + '\" LookupId="TRUE"/><Value Type=\"Lookup\">' + _spPageContextInfo.userId + '</Value></Eq>'
    + '<Neq><FieldRef Name=\"' + listFilterStatusFld + '\" /><Value Type=\"Text\">' + listFilterStatusNot + '</Value></Neq>'
    + '</And>'
    + '</Where>'
    + '</Query><RowLimit>1000</RowLimit></View>');

var webs = root.getSubwebsForCurrentUser(query);

// make sure to load the lists data so we can check if the list exists in each site
myCtx.load(webs, "Include(Id, Lists, Title)");

myCtx.executeQueryAsync(function () {

    var includeCols = 'Include(Id,ParentList)';
    // push new SP.ListItemCollections into an array and tell the ctx to fetch them
    for (var i = 0; i < webs.get_data().length; i++) {
        var web = webs.get_data()[i], itms;

        var lists = web.get_lists().get_data();

        // Search only Lists with specific title and item count
        for (var j = 0; j < lists.length; j++) {
            var list = lists[j];
            if (list.get_title() == siteListTitle && list.get_itemCount() > 0) {
                itms = list.getItems(query);
                myCtx.load(itms, includeCols);
    var itemCollections = [];

    myCtx.executeQueryAsync(function () {
        console.log(itemCollections.length + ' lists found containing items');

        // work with list items in itemCollections here    

    }, genericFailHandler);
}, genericFailHandler);

Note that code sample is not tested, I just cobbled it together now from a number of methods in a library I have created.

However it should illustrate how you can modified the CAML query to identify the sites with getSubwebsForCurrentUser with lists containing the desired list items, and then those lists can be queried separately for items.

In the final executeQueryAsync called in that example multiple lists are retrieved in a single call, however you will find that requesting more than 10 (ish) itms = list.getItems(query) requests will fail. Also if you have a total of more than 256 items in your query it will fail.

Hopefully that leads you to your solution.

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.