Search code examples
node.jssharepointsharepoint-2010sharepoint-2013

Sharepoint request digest value cross-domain


I'm building a webapplication on NodeJS and ExpressJs where i am trying to upload documents to a Sharepoint Online library through the Sharepoint REST Api.

I'm able to authenticate and create and update list items. I'm using node-sp-auth to authenticate and express-request to make http requests.

In order to access the Sharepoint REST api i'm using the FormDigestValue in my header.

Header:

Headers['Content-Type'] = 'application/json;odata=verbose';
Headers['X-RequestDigest'] = FormDigestValue;
Headers['Accept'] = "application/json;odata=verbose";
Headers['X-HTTP-Method'] = "POST";

This is working fine when i try to create/update list items in a custom list. But when i try to upload a document i receive the following error:

body: '{"error":{"code":"-1, Microsoft.SharePoint.SPException","message":
{"lang":"en-US","value":"There is no app context to execute this request."}}}' } }

The url i am using for upload:

siteurl + "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle(@TargetLibrary)/RootFolder/Files/add(url=@TargetFileName,overwrite='true')?" +
        "@TargetSite='" + siteurl + "'" +
        "&@TargetLibrary='" + libraryName + "'" +
        "&@TargetFileName='" + fileName + "'";

The post request:

request.post({
    url: url,
    type: "POST",
    headers: uploadHeaders,
    binaryStringRequestBody: true,
    body: fileData,
})

Any idea what i am doing wrong?


Solution

  • use this format instead for the post:

    url: url,
    type: 'POST',
    data: fileData,
    processData: false,
    headers: { 
        'Accept':'application/json;odata=verbose',
        'X-RequestDigest': digest,
        'Content-Length': fileData.byteLength 
    }
    

    If you don't have a RequestDigest already call _api/SP.AppContextSite(@TargetSite)/contextinfo