Ads

Friday, 17 February 2017

REST to Get all folders from root site

Hope you have idea how to create SharePoint App.
If you are using NAPA tool then replace below codes in APP.js file.
So i am just focusing on the REST script part to do the activities

I recommend to go through REST for SP document libraries before proceed.

HTML file in Default.aspx 
<div>
        <p>
            <b>Retrive Folders</b>
            <br />
            <select style="height:500px; width:510px" multiple="multiple" id="allFolders"></select>
        </p>
</div
----------------------
JS script in app.js (Paste the below methods in app.js file) Refer here
-----------------------
function getFolders() {
    var executor;
    // Initialize the RequestExecutor with the app web URL.
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync({
        url: appweburl + "/_api/SP.AppContextSite(@target)/web/folders?@target='" + hostweburl + "'",
        method: "GET",
        headers: {
            "Accept": "application/json; odata=verbose"
        },
        success: FoldersSuccessHandler,
        error: FoldersErrorHandler
    });
}  

//Populate the selectFolders control after retrieving all of the folders.
function FoldersSuccessHandler(data) {
    var jsonObject = JSON.parse(data.body);
    var allFolders = document.getElementById("allFolders");
    if (allFolders.hasChildNodes()) {
        while (allFolders.childNodes.length >= 1) {
            allFolders.removeChild(allFolders.firstChild);
        }
    }
    var results = jsonObject.d.results;
    for (var i = 0; i < results.length; i++) {
        var Option = document.createElement("option");
        Option .value = results[i].Name;
        Option .innerText = results[i].Name;
        allFolders.appendChild(Option );
    }
}
function FoldersErrorHandler(data, errorCode, errorMessage) {
    alert("Could not retrieve  Folders: " + errorMessage);
}




No comments:

Post a Comment

Ads