ServiceNow SharePoint Document Library Integration - Send attachments via REST API - Unable to open attachment in SharePoint

Ashwin26
Tera Contributor

Hello,

We have been struggling to send attachments to a SharePoint Document Library from ServiceNow via REST API integration.

In our Flow Designer flow, we receive the OAuth token from SharePoint and then try to send a request to the SharePoint document library endpoint. While the request seems to go through and we see the document in the SharePoint document library folder location as expected, we are unable to open the file and the file seems to not have come through correctly, since the size shows very less (15 bytes as an example)

Below is the code we are using to pass the attachment content (binary) to the SharePoint endpoint, and have tried some other combinations as well (the request is erroring out at ServiceNow if we use the Content-Length header, hence removed to allow the request to go through). Endpoint is per SharePoint documentation.

Note that we are not using the Integration Hub MS SharePoint spoke, but our own flow.

Not sure where/what is missing.


------

//Get attachments
var target = new GlideRecord('sys_attachment');
target.addQuery('table_sys_id', <sys_id>);
target.query();

while(target.next()) {
var fileName = encodeURIComponent(target.file_name);
//var fileName = target.file_name;
var StringUtil = new GlideStringUtil();
var sa = new GlideSysAttachment();
var binData = sa.getContent(target);
var base64Data = sa.getContentBase64(target);


var uriPart = "GetFolderByServerRelativeUrl('<SharePoint Document Library/Folder>')/Files/add(url='" + fileName + "',overwrite=true)";
var uri = '<SharePoint Site URL>' + encodeURIComponent(uriPart);

//Build the message
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('post');
request.setEndpoint(uri);
//request.setEndpoint(urlRawReplace);
//request.setEndpoint(inputs.site_url + "/_api/web/folders"); //Test only
request.setRequestHeader('Authorization','Bearer ' + inputs.access_token);
request.setRequestHeader('Accept','application/json;odata=verbose');
//request.setRequestHeader('Accept', 'application/json, */*');
//request.setRequestHeader('Content-Type','application/json');
request.setRequestHeader('Content-Type','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//request.setRequestHeader('Content-Type','application/xlsx');
//request.setRequestHeader('Accept-Encoding', 'gzip, deflate, br');
//request.setRequestHeader('Content-Length','300');
request.setRequestHeader('X-RequestDigest','{form_digest_value}');
//request.setRequestHeader('X-RequestDigest',"{form_digest_value}");
request.setRequestBodyFromAttachment(target.sys_id);

//body
var body_params = {
"data":base64Data
};
request.setRequestBody(body_params);

//Execute
var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

var respObj = new global.JSON.parse(responseBody);
var respSharePointURL = respObj.d.LinkingUrl;

-------

Thanks

 

2 REPLIES 2

Sukraj Raikhraj
Kilo Sage

What's the file format type?  SN does ignored attachment if the format type is not matching up, for ex: if word is specify but the file is pdf, it will failed. 

community.servicenow.com/community?id=community_question&sys_id=40206f39db1727404abd5583ca961915

Thanks Sukraj,

 

This occurs for any file type - .txt/pdf/docx/xlsx

The Content Type header was tried with all the combinations (based on the file type that gets generated in the sys_attachment table, plus the rest), and these work well when sent from Postman.