I was working on SharePoint 2013 REST API to fetch the list of folders from a document library specifically a discussion List. The usual lists/getByTitle endpoint gets the list of folders but without the URL of the folder.
When I checked the REST API examples at MSDN, one thing which caught my attention was getFolderByServerRelativeUrl endpoint. Unfortunately the examples provided in the MSDN is not correct. As per the MSDN, we need to pass the document library name as a parameter.
When we try this, we get 400 error (Bad request). So what is the correct way of making it work?
You need to pass the relative URL of the document library not just only the document library name.
Example: /sites/SiteA/Shared Documents/
This works as expected!
When I checked the REST API examples at MSDN, one thing which caught my attention was getFolderByServerRelativeUrl endpoint. Unfortunately the examples provided in the MSDN is not correct. As per the MSDN, we need to pass the document library name as a parameter.
executor.executeAsync({
url: "/_api/SP.AppContextSite(@target)/web
/getfolderbyserverrelativeurl('/Shared Documents')
?@target='' ",
method: "GET",
headers: { "accept": "application/json; odata=verbose" },
success: successHandler,
error: errorHandler
});
When we try this, we get 400 error (Bad request). So what is the correct way of making it work?
You need to pass the relative URL of the document library not just only the document library name.
url: "/_api/SP.AppContextSite(@target)/web
/getfolderbyserverrelativeurl('/[Managed Path]/[Site]/Shared Documents')
Example: /sites/SiteA/Shared Documents/
This works as expected!
We will get System.Argument exception if we directly type in the url(/_api/web/getfolderbyserverrelativeurl('/Shared Documents/')) as given in MSDN examples. The odata path states ServerRelativeUrl which includes the managed path/sitecollectionname . Sadly some of the examples in MSDN are misleading.
ReplyDeleteYes Karvannan.
Delete