0

I have an requirement of develop an app for sp2013 to read different site collection's list data and show in a one place the items that are created by me on separate list in separate site collections.

2

SharePoint Search Query APIs is a way to go for accessing list data across site collections.

Examples

Assume we need to retrieve Task items, then the following query could be used:

contentclass:STS_ListItem AND ContentType:Task

to return all the task items.

JavaScript REST example

The following example demonstrates how to utilize Search Query API using REST endpoint:

function searchTaskItems(webUrl,success, failure) {
    var url = webUrl + "/_api/search/query?querytext='contentclass:STS_ListItem AND ContentType:Task'";
    $.ajax({
        url: url,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            success(data.d.query);
        },
        error: function (data) {
            failure(data);
        }
    });
}


//print tasks
searchTaskItems(_spPageContextInfo.webAbsoluteUrl,
  function(query){
      var resultsCount = query.PrimaryQueryResult.RelevantResults.RowCount;
      for(var i = 0; i < resultsCount;i++) {
          var row = query.PrimaryQueryResult.RelevantResults.Table.Rows.results[i];
          var taskName = row.Cells.results[3].Value;
          console.log(taskName);
      }   
  },
  function(error){
    console.log(JSON.stringify(error));
  }
);

JSOM Example

The same example but using JSOM:

SP.SOD.executeFunc('SP.search.js', 'Microsoft.SharePoint.Client.Search.Query.KeywordQuery', function() {

  var context = SP.ClientContext.get_current();
  var site = context.get_site();
  var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context); 
  keywordQuery.set_queryText("contentclass:STS_ListItem AND ContentType:Task"); 
  var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);  
  var results = searchExecutor.executeQuery(keywordQuery); 
  context.executeQueryAsync(
    function(){
        for(var idx in results.get_value().ResultTables[0].ResultRows) {
            var taskTitle = results.get_value().ResultTables[0].ResultRows[idx].Title;
            console.log(taskTitle);    
        }
    }, 
    function(sender,args){
        //error handling goes here...
    });

});    

References

  • Your example doesn't show across site collections though. Will this still work? – Batman Jan 28 '17 at 6:46

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.