SharePoint REST using parameters
Programming against SharePoint as an endpoint, the SharePoint REST interface is getting more important, especially now the SharePoint Framework is being released.
Also with popular JavaScript frameworks like Angular and React, the SharePoint REST interface gets more and more appealing.
Working with the REST Api in SharePoint Online, you can experience an issue if your REST call url exceeds the max. length. I found a question on stackexchange on this,
with an answer that could require you to do an extra REST call to get an ID. After reading this, I had an epiphany, couldn't you use parameters, as seen in the REST Api documentation in code like "/_api/SP.AppContextSite(@target)/web/lists?@target='",
and use this with GetFolderByServerRelativeUrl and GetFileByServerRelativeUrl?
So I went and tried this out, and it worked!
I have checked this with an example on my own Office 365 tenant, where I created a site with a long url, having a subsite with a long url, and a folder with a long name.
With te following REST call resulting in an:
The length of the URL for this request exceeds the configured maxUrlLength value.
https://[mytenantname].sharepoint.com/sites/samplesitewithalongurlnameasthiscouldgiveissueswithrestcalls/asubsitewithalongurltotestrestonlongurls/_api/web/GetFolderByServerRelativeUrl('/sites/samplesitewithalongurlnameasthiscouldgiveissueswithrestcalls/asubsitewithalongurltotestrestonlongurls/Shared%20Documents/alongfoldernametotestissueswithrestcallsonsharepoint')
I now applied the usage of a parameter to get to the following url:
https://[mytenantname].sharepoint.com/sites/samplesitewithalongurlnameasthiscouldgiveissueswithrestcalls/asubsitewithalongurltotestrestonlongurls/_api/web/GetFolderByServerRelativeUrl(@p)?@p='/sites/samplesitewithalongurlnameasthiscouldgiveissueswithrestcalls/asubsitewithalongurltotestrestonlongurls/Shared Documents/alongfoldernametotestissueswithrestcallsonsharepoint'
Now the url is shortened, as querystring parameters are not part of the url. The result is, that the REST call succeeded, with the expected result.
It looks like you could use parameters, whenever you have parentheses ( ) in the SharePoint REST Api url. I have not tested this extensively, though I think this is
a build in feature of the REST Api. I thought I had not seen any examples of using this feature with GetFolderByServerRelativeUrl, GetFileByServerRelativeUrl or some other common
REST methods, but I think this is the way to go when using the SharePoint REST Api. I think using parameters with the SharePoint REST Api, you can prevent the most common issues with long REST Api call urls.
Looking at the answers at stackexchange again, I see, when scrolling the answer by AJ (user46907) that this is exactly what I am describing here, it seems I have overlooked this answer before, oops.
Well, I think it does require more attention to start using parameters to prevent issues.