The CreatorCon Call for Content is officially open! Get started here.

Sending attachment to sharepoint list item via REST API

priyankapalaska
Giga Contributor

Hi,

I am trying to send attachment from SN to sharepoint. Below are the SharePoint API details: 

REST API URL - https://<tenant>/_api/web/lists/getbytitle('PIPTracking')/items(135)/AttachmentFiles/add(FileName='Test')

method type - Post

 

headers: { 

                    "Accept": "application/json; odata=verbose", 

                    "content-type": "application/json; odata=verbose", 

         }

 

body: {data: buffer}

* buffer is the binary object of file as buffer

 

I have written a business rule: 

var target = new GlideRecord('sys_attachment');
target.addQuery('sys_id', 'dfa59a2d1bc2d0503c85ff7dcc4bcb25');
target.query();

if(target.next()) {

var sa = new GlideSysAttachment();
var binData = sa.getBytes(target);
var base64Data = GlideStringUtil.base64Encode(binData);
}


var body= {
"data":binData
};

//Send Attachments
var r1 = new sn_ws.RESTMessageV2(' PIP_ITBM API Sharepoint', 'Send Attachment');
r1.setEndpoint("https://<tenant>.com/sites/devpipcopy/_api/web/lists/getbytitle('PIPTracking')/items(135)/AttachmentFiles/add(FileName='135Attachment2')");
r1.setRequestHeader("Authorization","Bearer "+token);
//r1.setRequestBodyFromAttachment('dfa59a2d1bc2d0503c85ff7dcc4bcb25')
r1.setRequestBody(body);
var response = r1.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.print("httpStatus = "+httpStatus );

 

So here i am getting 200 response status, and even in sharepoint item list has attachmnet icon. But the actual file is not present in sharepoint. 

 

what is binary object of file as buffer? What i am suppose to pass in the data? I tried by passing base64Data as well, also tried with method setRequestBodyFromAttachment().

Please suggest if i miss something here. 

2 REPLIES 2

MrMuhammad
Giga Sage

try this.

var base64Data; 
var binData;

var target = new GlideRecord('sys_attachment');
target.addQuery('sys_id', 'dfa59a2d1bc2d0503c85ff7dcc4bcb25');
target.setLimit(1); 
target.query();

if (target.next()) {
     
    var StringUtil = new GlideStringUtil();
    var sa = new GlideSysAttachment();
    binData = sa.getBytes(target);
    base64Data = StringUtil.base64Encode(binData);
}


var body = {
    "data": binData
};

//Send Attachments
var r1 = new sn_ws.RESTMessageV2(' PIP_ITBM API Sharepoint', 'Send Attachment');
r1.setEndpoint("https://<tenant>.com/sites/devpipcopy/_api/web/lists/getbytitle('PIPTracking')/items(135)/AttachmentFiles/add(FileName='135Attachment2')");
r1.setRequestHeader("Authorization", "Bearer " + token);
r1.setRequestBodyFromAttachment('dfa59a2d1bc2d0503c85ff7dcc4bcb25')
//r1.setRequestBody(body);
var response = r1.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.print("httpStatus = " + httpStatus);
Regards,
Muhammad

jyeon110
Tera Expert

Hello,
were you able to send the attachment to the sharepoint through Rest API? If you did, can you share how you did it with "setRequestBodyFromAttachment" method?

 

thanks