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


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);

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";
        requesturi = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFileByServerRelativeUrl('/Shared%20Documents/document.docx')/$value";

[please try with angularJs

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

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.