2

I have a scenario where i would like to fetch all the files from multiple folders using REST API. I would like to filter the data in such a way so i can ignore the folders and fetch only the files inside all the files. I have the CAML query to do the recursive all activity but when i integrate with REST API its throwing a Bad request error. I checked the Network tab in IE it seems like the query in my code is not returning any data from REST API and it is failing when i use "GetItems". Could someone please let me know how to integrate CAML query in REST API.

 var getContent = function (listTitle) {

                var deferred = $q.defer();

                if (listTitle == 'CandidateReports') {


  var query = appweburl + "_api/SP.AppContextSite("+ hostweburl +")/web/lists/GetByTitle('" + listTitle + "')/GetItems";



                var executor = new SP.RequestExecutor(appweburl);
                executor.executeAsync({

                    url: query,
                    method: "POST",
                    headers: {
                        "Accept": "application/json; odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                        "content-Type": "application/json;odata=verbose"
                    },
                    data: JSON.stringify({
                        query: {
                            __metadata: {
                                type: "SP.CamlQuery"
                            },
                            ViewXml: "<ViewFields><FieldRef Name='FileLeafRef' /> <FieldRef Name='Title' /> </ViewFields> <QueryOptions> <ViewAttributes Scope='RecursiveAll' /> </QueryOptions> <Where> <Eq> <FieldRef Name='FSObjType' /> <Value Type='Integer'>0</Value> </Eq>   </Where>"
                        }
                    }),
                    success: function (data, textStatus, xhr) {
                        console.log(data);
                        deferred.resolve(JSON.parse(data.body));
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        deferred.reject(JSON.parse(xhr.body).error);
                    }
                });

                return deferred.promise;

            };

            return {

                getContent: getContent

            };
1

The following example shows how to retrieve all of the files in a folder.

url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files
method: GET
headers:
    Authorization: "Bearer " + accessToken
    accept: "application/json;odata=verbose" or "application/atom+xml"

Does this help? Source

1

Am I crazy or should you need Get, not Post?

url: query,
                    method: "POST",
                    headers: {
0

try this... replace this

data: JSON.stringify({
                    query: {
                        __metadata: {
                            type: "SP.CamlQuery"
                        },
                        ViewXml: "<ViewFields><FieldRef Name='FileLeafRef' /> <FieldRef Name='Title' /> </ViewFields> <QueryOptions> <ViewAttributes Scope='RecursiveAll' /> </QueryOptions> <Where> <Eq> <FieldRef Name='FSObjType' /> <Value Type='Integer'>0</Value> </Eq>   </Where>"
                    }
                }),

with this

data: {
        query: {
                "__metadata": {
                    "type": "SP.CamlQuery"
                    },
                    "ViewXml": "<View scope='RecursiveAll'><ViewFields><FieldRef Name='FileLeafRef' /> <FieldRef Name='Title' /> </ViewFields> <Query> <Where> <Eq> <FieldRef Name='FSObjType' /> <Value Type='Integer'>0</Value> </Eq>   </Where></Query></View>"
                    }
                },

There is no need to strigify the header values. Also Including the "View" nodes in the caml is required

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.