0

I've been using a CAML Query for getting all files in a document library in SharePoint 2013.

I used PnP with PowerShell for this purpose, but as I would like to get all files on Linux. I think it's feasable with curl with REST API but I cannot find the correct URL syntax. I would like to get at least all documents server relative urls and names (FileDirRef, FileRef, etc.).

I used this query taken from sharepoint-rest-api-recursively-fetch-all-folders-in-a-document-library, but I don't think it returns what I expect or I might not know how to parse it right.

$ curl -n "https://<site url>/_api/web/lists/GetByTitle('document library name')/items?$filter=FSObjType%20eq%201" -H "Accept: application/json;odata=verbose"

-n takes credentials from .netrc file.

I would like to send <View Scope='RecursiveAll'><Query><Where><IsNotNull><FieldRef Name='File_x0020_Type' /></IsNotNull></Where></Query></View> (which returns the right answer with Get-PnPListItem -List <library name> -Query "$camlQuery") with curl if possible.

jgran
  • 103
  • 4

1 Answers1

0

You use the SP.List.getItems method.

POST http://<sitecollection>/<site>/_api/web/lists(listid)/getItems(query)

Here's some example code that demonstrates the use of this method. I don't use curl but I expect you should be able to adapt this sample for use with it.

var viewXml = {
    ViewXml: "<View>" +
        "<Query>" +
        "<Where><Eq>" +
        "<FieldRef Name='Category' LookupId='True' />" +
        "<Value Type='Lookup'>1</Value>" +
        "</Eq></Where>" +
        "</Query>" +
        "</View>"
}

var call = jQuery.ajax({ url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Products')/GetItems(query=@v1)?" + "@v1=" + JSON.stringify(viewXml), type: "POST", dataType: "json", headers: { Accept: "application/json;odata=verbose", "X-RequestDigest": jQuery("#__REQUESTDIGEST").val() } }); call.done(function (data, textStatus, jqXHR) { var message = jQuery("#message"); message.text("Beverages"); message.append("<br/>"); jQuery.each(data.d.results, function (index, value) { message.append(value.Title); message.append("<br/>"); }); }); call.fail(function (jqXHR, textStatus, errorThrown) { var response = ""; try { var parsed = JSON.parse(jqXHR.responseText); response = parsed.error.message.value; } catch (e) { response = jqXHR.responseText; } alert("Call failed. Error: " + response); });

Rob Windsor
  • 12,648
  • 25
  • 39
  • Thanks. I have seen such a query, but I would like the final URL if possible (that I can hopefully pass as an argument to curl). What is the query in http://<sitecollection>/<site>/_api/web/lists(listid)/getItems(query)? What is @v1 used twice? – jgran Feb 18 '22 at 13:21
  • Could the command be `curl -X POST "https:////_api/web/lists(listid)/GetItems('query=@v1')?@v1={"ViewXml":""}', if only escaped correctly? – jgran Feb 18 '22 at 13:30
  • Sorry, I've never used curl. I'm not sure how to format the HTTP request when using it. – Rob Windsor Feb 18 '22 at 14:52
  • Thanks. how-can-i-use-caml-queries-with-the-rest-api gives as an example http://[site]/_api/Web/lists/getByTitle('[List]')/GetItems(<Query><Where><Eq><FieldRef%20Name='Sector'%20/><Value%20Type='TaxonomyFieldType'>Value</Value></Eq></Where></Query></View>"} with POST, but X-RequestDigest seems required. – jgran Feb 18 '22 at 15:02