Rest API to pull all list items that uses a unique Content Type under a specific URL
-
08-01-2021 - |
Pregunta
I am on SharePoint Online environment, where i have a site(Lets call it Site A) https://xxxxx.com/sites/sitecollection/SiteA and multiple sub sites under Site A (Subsite B, Subsite C, Subsite D and so on....). The subsites are added every month and there is no specific number. https://xxxxx.com/sites/sitecollection/SiteA/SubsiteB(same thing goes with C,D,... ). So this is the hierarchy.
All the sites (Site A, Subsite B, Subsite C, Subsite D.....) have lists (List A, List B, List C, List D,....)respectively that are tagged with a unique Content Type, Lets Call it Content Type X.(I have created this content type, not a system generated one). What i mean is (Content type of List A, B, C and D..... are same, but the data is unique).
I am trying to pull all the list items under List A, List B, List C and List D and ....(that are tagged with this Content Type X) using Rest API.
If its just one list, you can use this "/_api/web/lists/getbytitle('List A')/items?$select=Title,URL,ContentType/Name&$expand=ContentType&$filter=ContentType eq 'Content Type X'".
But the challenging part is, I want to pull all list items from the List A, List B, List C,..... dynamically
Any suggestions will be helpful.
Thanks
Solución
The best way to get all items belonging to a content type under a site collection would be to use the Search REST API. Since data will already be indexed, it is the fastest way to get items.
You can set the Search API to use the Content Type and Path attributes which will restrict it fetch items accordingly.
The endpoint would be as below:
/_api/search/query?querytext='ContentTypeId:0x01005FC5182DF0BF554287F15C4D8B798E6* + path:https://sitecollectionurl/*'&selectproperties='Title,Path'
Now, to get the values of columns, add those columns Managed properties in the selectproperties
parameter.
You can get the ID of your content type using the following steps:
- Navigate to https://tenantname.sharepoint.com/_layouts/15/mngctype.aspx
- Locate the content type of the list
- Hover over the content type and copy the id after ManageContentType.aspx
Otros consejos
You need to get all lists from the site firstly, and create a variable for list title, set the variables as results you got.
Reference:
http://www.c-sharpcorner.com/UploadFile/anavijai/how-to-get-all-the-lists-in-sharepoint-2013-online-using-res/ http://www.c-sharpcorner.com/article/how-to-get-all-list-items-of-a-list-using-rest-api-in-sharepoint-online-and-offi/
Where do you want to put this data? If in an excel file, you can try to run the PowerShell script, here is a demo: https://sharepointmates.com/2017/07/07/get-all-lists-items-in-a-site-collection-using-power-shell-in-sharepoint-online/
I don't think you can use REST to query multiple lists in this manner. I would have a look at using CSOM instead.
Query multiple lists using javascript/CSOM
or maybe this longshot shortcut will help
Use search API:
var searchApi = '/_api/search/query?querytext=%27ContentType:"CT Name"'
var selectedParameter = '&trimduplicates=false&rowlimit=%27500%27&selectproperties=%27LastNameOWSTEXT,FirstNameOWSTEXT,Title%27';
var searchQuery = _spPageContextInfo.siteAbsoluteUrl +searchApi + ' %27' + selectedParameter
var query = "";
var myJsonString = "";
$.ajax({
url: searchQuery,
type: "GET",
headers: {
"Accept": "application/json;odata=verbose"
},
async: false,
success: function (data, textStatus, xhr)
{
var values = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
},
error: function (data) {
console.log(data);
}
});