Reputation: 2659
I am creating a custom page writing the HTML and javascript for a SharePoint site. I would like to embed document libraries inside my custom html I am writing in SharePoint designer.
I have nto found a way to easily embed document libraries in custom html but did stumble on some documentation for a rest api. I figured I could use this and write my own ajax app in the html for users to navigate the document library.
I am currently trying with this javascrip just to see if I can pull html or JSON for a document library contents:
<script type="text/javascript">
var folderUrl = "x/x/x/testDocumentLibrary/Forms/AllItems.aspx";
var url = _spPageContextInfo.webServerRelativeUrl + "/_api/Web/GetFolderByServerRelativeUrl('" + folderUrl + "')?$expand=Folders,Files";
$.getJSON(url,function(data,status,xhr){
for(var i = 0; i < data.Files.length;i++){
console.log(data.Files[i].Name);
}
for(var i = 0; i < data.Folders.length;i++){
console.log(data.Folders[i].Name);
}
});
</script>
I am not sure if I am using the right url for the folderUrl
variable.
In order to conduct some tests what is _spPageContextInfo.webServerRelativeURL
pulling? I am trying to see if I can work backwards and create the URL manually first with out the SP function calls.
Upvotes: 0
Views: 591
Reputation: 7059
The folderUrl
variable in your example code should end with the path to the library; everything up until /Forms/AllItems.aspx
, so /x/x/x/testDocumentLibrary
where /x/x/x/
is the server-relative path to the site on which the library resides.
The _spPageContextInfo
object provides two variations of server-relative URL, one for the current site (called a "web" in SharePoint jargon) and one for the current site collection (called a "site" in SharePoint jargon). Appropriately, these properties are labeled webServerRelativeURL
and siteServerRelativeURL
. Both of these are server-relative, meaning that they exclude the first part of the domain name. (Instead of https://constoso.com/sites/stackoverflow
they'll give you /sites/stackoverflow
.)
For a REST call, you probably want the absolute URL, not the server-relative URL. You can access the web and site absolute URLs through _spPageContextInfo
's properties webAbsoluteURL
and siteAbsoluteURL
.
If the list/library you're accessing is on the current site where your REST is running, use the webAbsoluteURL
property.
Upvotes: 1