20

I have tried below api to upload file to library folder.

 var url = String.format(
            "{0}/_api/web/GetFolderByServerRelativeUrl('/testDocumentList/testFolder')/Files/Add(url='{1}', overwrite=true)",
            _spPageContextInfo.webAbsoluteUrl, fileName);

I get error Server relative urls must start with SPWeb.ServerRelativeUrl

3 Answers 3

34

Try removing the / character from the beggining of the parameter of GetFolderByServerRelativeUrl function. Change it to: 'testDocumentList/testFolder'.

Your updated code should look like this:

var url = String.format(
            "{0}/_api/web/GetFolderByServerRelativeUrl('testDocumentList/testFolder')/Files/Add(url='{1}', overwrite=true)",
            _spPageContextInfo.webAbsoluteUrl, fileName);
1
  • This worked for me too. The URL is expected to resolve with relation to the site URL, which may not be a naked domain name. As such a relative folder, it must not start with a slash, as I found out today. Commented Apr 25 at 7:28
4

In my case, I was having this error in SharePoint Online. My site collection URL contains "/sites". So to fix this issue I have to pass URL as follow:

var requesturi= "";
    if((_spPageContextInfo.webAbsoluteUrl) && (_spPageContextInfo.webAbsoluteUrl.indexOf("sites")>0))
    {
        requesturi = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativeUrl('"+_spPageContextInfo.webServerRelativeUrl+"/Shared%20Documents/document.docx')/$value";
    }
    else
    {
        requesturi = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativeUrl('/Shared%20Documents/document.docx')/$value";
    }
0

[please try with angularJs

Please try with angularJs & => fileUrl = 'subsite01/Pages/file-0001.aspx'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.