Hi All,
In a following post we will
see about how to get the documents/Items inside the Folder in SharePoint
document library.
Below code is developed in
SharePoint hosted apps.In a below Example ,I get the Items from "Shared Documents"(Library Name) and "Folder1" is my folder inside the document Library.
'use strict';
var SPAppWebUrl;
var spHostUrl;
(function
() {
// This code runs when the DOM is ready
and creates a context object which is
// needed to use the SharePoint object
model
$(document).ready(function () {
spHostUrl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
SPAppWebUrl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
SP.SOD.executeFunc("sp.js", "SP.ClientContext", function () {
SP.SOD.executeFunc("sp.runtime.js", "SP.ClientContext",
function () {
$.getScript(spHostUrl + "/_Layouts/15/SP.RequestExecutor.js", function (data) {
MainFunction();
});
});
});
});
function MainFunction() {
$('#btnGet').on('click', function
() {
getFolderCount()
});
}
function getFolderCount() {
var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" +
"/web/getfolderbyserverrelativeurl('/Shared
Documents/Folder1')/files?" +
"@target='" + spHostUrl + "'";
var executor = new
SP.RequestExecutor(SPAppWebUrl);
executor.executeAsync({
url: url,
method: "GET",
headers: { "Accept":
"application/json;odata=verbose" },
// return data format
success: onQuerySuccess,
error: onQueryError
});
function onQuerySuccess(data) {
var
jsonObject = JSON.parse(data.body);
var
results = jsonObject.d.results;
//count inside the folder
alert(results.length);
if
(results.length !== null) {
//Looping
Jason data and get the documents
$.each(results, function (index) {
alert(results[index].Name);
alert(results[index].ServerRelativeUrl);
});
}
}
function onQueryError(error) {
alert("Error:
" + error.statusText);
}
}
function
getQueryStringParameter(urlParameterKey) {
var params = document.URL.split('?')[1].split('&');
var strParams = '';
for (var
i = 0; i < params.length; i = i + 1) {
var
singleParam = params[i].split('=');
if
(singleParam[0] == urlParameterKey)
return
decodeURIComponent(singleParam[1]);
}
}
})();
Thanks.
0 comments:
Post a Comment