I want to fetch Document Library List in my application using java-script/Jquery How can I fetch SharePoint Document Library record and show in my application.
-
Q.1.Do you want each document name in your library or you are fetching one document based on some condition? Q.2. Does your library have folders in it?– Mayank SrivastavaFeb 20, 2017 at 12:02
-
Q.1. I want each doc name.(List record). Q2. No any folder only doc.– AdminFeb 20, 2017 at 12:20
-
If you need any help regarding my answer, feel free to ask– Mayank SrivastavaFeb 20, 2017 at 20:18
1 Answer
These are the two easy ways of doing it
- Using REST Services
- Using JavaScript Object Model
1. Using REST
Using REST services, you can make a REST call to the document library and simply get all the items in an array.
Here the REST call will be: http://*your site*/_api/web/GetFolderByServerRelativeUrl(LibName )/Files
When the call is completed, the results are stored in FilesData
array which you can use in your application.
Eg. var name = FilesData.d.results[i].Name
would get you the name and var URL = FilesData.d.results[i].ServerRelativeUrl
will get you the URL of the document. So if you want the document to be downloadable from your application simply store it in a variable as var DownloadLink = <a href='" + URL + "'>name</a>
Note: You can also get other properties like created, modified, created by, modified by, etc.
var LibName = "Documents";
$("#btn").click(function(){ //btn is the id of the button, you can call this function on any event
StoreInArray();
});
function StoreInArray(){
var CallLibrary = GetListItems();
$.when(CallLibrary).then(function (FilesData) {
for (var i = 0; i < FilesData.d.results.length; i++) {
var name = FilesData.d.results[i].Name;
var url = FilesData.d.results[i].ServerRelativeUrl;
}
});
}
function GetListItems() {
var siteURL = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('"+LibName+"')/Files";
return $.ajax({
url: siteURL,
async: false,
method: "GET",
headers: { "Accept": "application/json;odata=verbose" }
});
}
2. Using JavaScript Object Model
See the msdn link How to: Retrieve List Items Using JavaScript
-
Thanks for reply Mayank. But I want to access sharepoint site in my other application not sharepoint how can I access this in my other application.?– AdminFeb 21, 2017 at 6:01
-
-
-
1. Using REST : I get this error when i run "Uncaught ReferenceError: _spPageContextInfo is not defined"– AdminFeb 21, 2017 at 6:49
-
try giving full url of sharepoint site instead of it. var siteURL = "http://<your site name>/_api/web/GetFolderByServerRelativeUrl('"+LibName+"')/Files Feb 21, 2017 at 6:53