1

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?

1

1 Answer 1

0

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

4
  • the character length does not exceed 255
    – jamesd
    Jul 31, 2015 at 14:03
  • Do you have any special characters in your filename? 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/migrations
    – jamesd
    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 length Jul 31, 2015 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.