I have a script that uses the rest api and I am wanting to upload a file to a site collection document library that has the /sites/sitecollectionname layout. By doing : https://mysite.sharepoint.com/sites/collectioname/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')/Files/add(url='$filename')
This sends an error back saying bad request, but when I use :
https://mysite.sharepoint.com/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')/Files/add(url='$filename') this works and uploads the file. Is there any particular way to do this?
And how do you add it to a folder/subfolder in that document library?
-
See if this helps you: blogs.msdn.com/b/uksharepoint/archive/2013/04/20/…Akhoy– Akhoy2015-07-31 09:19:41 +00:00Commented Jul 31, 2015 at 9:19
1 Answer
You will get a Bad request eerror, if your REST API url exceeds the length 255 excluding the querystring parameters.
So reduce the length of url by mentioning the folder and file name as parameter like
https://mysite.sharepoint.com/sites/collectioname/_api/web/GetFolderByServerRelativeUrl(@folderurl)/Files/add(url=@filename)?@folderurl='/sites/collectioname/doclibrary/folder'&@filename='filename.ext'
make sure the folderurl also contains sitecollection relative url like
/sites/collectioname/doclibrary/folder
use
_spPageContextInfo.webServerRelativeUrl
to get the web relative url
use
_spPageContextInfo.siteServerRelativeUrl
to get the site collection relative url if you are using js
-
the character length does not exceed 255jamesd– jamesd2015-07-31 14:03:42 +00:00Commented Jul 31, 2015 at 14:03
-
Do you have any special characters in your filename?Shekar Reddy– Shekar Reddy2015-07-31 16:21:29 +00:00Commented Jul 31, 2015 at 16:21
-
I do not have any special characters in the file name, as I said it works when its the site collection mysite.sharepoint.com but not mysite.sharepoint.com/sites/migrationsjamesd– jamesd2015-07-31 16:31:24 +00:00Commented Jul 31, 2015 at 16:31
-
well, I tried it just now in my sp onlline site like https:// xyz.sharepoint.com/sites/sitecolname/_api/web/GetFolderByServerRelativeUrl('/sites/sitecolname/libname/folder') and it is working. Did you include the site collection relative url in "GetFolderByServerRelativeUrl" method? the root site collection do not need the /sites/sitecolname because / itself represents root sitecollection, make sure it does not exceed the 255 char lengthShekar Reddy– Shekar Reddy2015-07-31 16:43:41 +00:00Commented Jul 31, 2015 at 16:43