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 Answers
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– Batman2017-01-28 06:46:32 +00:00Commented Jan 28, 2017 at 6:46
SharePoint Search is the best way to do this in 2013.
Here are some walkthroughs:
http://www.sharepointnutsandbolts.com/2012/10/using-content-search-web-part-and.html
http://channel9.msdn.com/Events/SharePoint-Conference/2014/SPC402