Add File Attachment to SharePoint List Item

We used to utilize the UiPathTeam.Sharepoint.Activities to manipulate SharePoint list items. We’re currently in an effort to migrate away from “Windows-legacy” compatibility automations. The UiPathTeam.Sharepoint.Activities package is not available for “Windows” compatible automations.

We’ve been able to replace the functionality we’ve used with other activities and with direct SharePoint API calls.

We’re struggling with attaching files to list items. We’ve had some success using the SharePoint API endpoint: POST “/_api/lists/GetByTitle(‘’)/items(”<List Item ID")/AttachmentFiles/add(FileName=‘<file name’) But we’re not quite there.

We successfully get a file attached to the list item using that endpoint, but the data is always corrupted in some way.

We’ve tried 2 primary ways.

The first is to use “File attachments” property of UiPath.Web.Activities.HttpClient. This is of type ICollection. We’re using “Path Exists” to get ILocalResource references to fill the collection. When we do this, we end up with files attached to the list item, but with an erroneous header and footer:

-----------8B43D14A-4F58-49DE-B617-EE104D985B62
Content-Disposition: form-data; name=“testfile”; filename=“testfile.png”
Content-Type: application/octet-stream

‰PNG

-----------8B43D14A-4F58-49DE-B617-EE104D985B62–

If I use a text editor to remove everything but the binary part, the file works correctly, but it does not work correctly with those headers/footers there.

The second method we’ve tried is to read the file data in with System.IO.File.ReadAllBytes, encoding the data as a string, and sending that file data as the body of the HTTP request. This also attaches a file to the SharePoint list item, but it is junk data. I expect I’m not encoding it properly.

Does anyone have any insights and/or has anyone had luck using other methods to attach files to SharePoint list items?

@glachnitt

Please cross verify here

Cheers

I appreciate the link ,but does that post clarify how to encode the file?

I see that it utilizes the restsharp library to create an HTTP request. That library takes a byte array of the file contents, but looking through the restsharp website, I’m not seeing any documentation on how the library actually encodes that byte array when it sends the http request.

I think you need to base64 encode the byte array before sending it, but I’ve not had any luck as of yet.

@glachnitt

Please check this it had details on how
To convert…i has more details…ignore them

Cheers

Again, thanks for your reply, but I’m not asking the method for encoding byte arrays into strings of whatever encoding or anything similar.

I’m asking if anyone knows what encoding we should be using with the SharePoint API command I listed above and how to use that with the HTTPClient and/or if anyone knows of an alternate method for attaching files to list items in SharePoint.

@glachnitt

Hopefully this csom might help you

Cheers

Have you had any luck completing this? I’ve been searching high and low for a solution to this.
I need to both download and upload attachments to sharepoint, but this far I can’t find a solution. We are still on legacy on some of the robots, but have transitioned to UiPath from UiPathTeam.

I’ve not had an opportunity to look into this since my last post. When I get back to it, I’m going to just use the invoke code activity to execute some .net code directly. I’ll likely try to use this as a basis: sharepoint server - Add attachment to a list item using REST API - SharePoint Stack Exchange

That is unless someone can give us some direction on getting the UiPath activities to work. I’d much prefer that.

I’ve not had need to download any file attachments from a SharePoint list item yet. I’m guessing you use a similar REST endpoint to get them though. Something like
GET {tenant}.sharepoint.com/_api/lists/GetByTitle(“”)/items(”<List Item ID")/AttachmentFiles I’ve not tried that yet, but I expect you could parse the attachment from the response to that request.

I finally gave up on trying to use native UiPath activities and went with Invoke Code. It is working for me so far to attach items to an existing SharePoint list item.

Here’s the C# code I’m using:

var fileName = System.IO.Path.GetFileName(in_FilePath);
var endpointUrl = string.Format("{0}/_api/web/lists/GetByTitle('{1}')/items({2})/AttachmentFiles/add(FileName='{3}')",in_WebUri,in_ListTitle,in_ItemId,fileName);

System.Net.Http.WinHttpHandler handler = new System.Net.Http.WinHttpHandler();
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(handler);

client.DefaultRequestHeaders.Add("X-RequestDigest", in_FormDigest);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", in_AccessToken);
using (var stream = System.IO.File.OpenRead(in_FilePath))
	{
		var response =  client.PostAsync(endpointUrl, new System.Net.Http.StreamContent(stream)).Result;
		out_APIResponseHeaders = response.Headers;
		out_APIResponseContent = response.Content;
		out_APIResponseStatusCode = response.StatusCode;
		out_APIResponseIsSuccessful = response.IsSuccessStatusCode;
		}

Here’s the arguments I’m using with that code:
image

  • in_FilePath is the path to the file you want to attach.
  • in_WebUri is the URL of the site that contains the list you want to modify E.G. https://(yourtenant).sharepoint.com/IT/Team/
  • in_ListTitle is the SharePoint list name
  • in_ItemId is the id of the list item. This should be a number (yes, I’m passing it as a string. Bad me. :slight_smile: )
  • in_FormDigest. This one’s harder. You have to make a post request to https://(yourtenant).sharepoint.com/_api/contextinfo. If done successfully, you’ll get a FormDigestValue in the response.
  • in_AccessToken This is an authorization bearer token for the api call. You get this from a POST to https://accounts.accesscontrol.windows.net//tokens/OAuth/2 I followed this site to figure it out Inside SharePoint 2013 OAuth Context Tokens | Microsoft Learn
1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.