1

I have created one document library,in that library i created some folders. How can i retrieve all folders in a particular doc lib using java script.Any ideas?

2

Make a GET request to the following URL, It will return all folders

/_api/Web/GetFolderByServerRelativeUrl('" + libraryRelativeUrl + "')/Folders?$select=Name,ServerRelativeUrl

Using jQuery

function getFolders(libraryRelativeUrl) {

    var queryUrl = "/_api/Web/GetFolderByServerRelativeUrl('" + libraryRelativeUrl + "')/Folders?$select=Name,ServerRelativeUrl";
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + queryUrl,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function(data) {
            console.log(data.d.results);
        },
        error: function(error) {
            alert(JSON.stringify(error));
        }
    });
}

libraryRelativeUrl should be a relative URL. Let say library name is XXX, then value will be

var libraryRelativeUrl = _spPageContextInfo.webServerRelativeUrl + 'XXX';

Now call the function as below:

getFolders(libraryRelativeUrl);

No jQuery in your page!! No problem!!

Use XMLHttpRequest()

function getFolders(libraryRelativeUrl) {

    var queryUrl = "/_api/Web/GetFolderByServerRelativeUrl('" + libraryRelativeUrl + "')/Folders?$select=Name,ServerRelativeUrl";

    var xmlhttp = new XMLHttpRequest();

    xmlhttp.open("GET", _spPageContextInfo.webAbsoluteUrl + queryUrl);

    xmlhttp.setRequestHeader("Accept", "application/json;odata=verbose");

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == XMLHttpRequest.DONE) {
            if (xmlhttp.status == 200) {
                var response = xmlhttp.responseText;

                console.log(response.d.results);

            } else {
                alert('Error: ' + xmlhttp.statusText)
            }
        }
    }

    xmlhttp.send();

}
  • Using rest api i dont no how to retrieve,plz send me full code – chakri Feb 11 '16 at 7:42

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.