18

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

KumarV
  • 2,680
  • 12
  • 53
  • 98

3 Answers3

29

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);
Damjan Tomic
  • 3,646
  • 3
  • 19
  • 18
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";
    }
love thakker
  • 1,136
  • 1
  • 16
  • 41
0

[please try with angularJs

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

Anh
  • 1
  • 1