List Item Attachments
Data Sync does not include List Item Attachments this is because the SharePoint API would require a server round-trip per list item to get the meta data about this list and extra calls to get the file data.
Data Sync does however have some internal API calls on the SharePoint Client API connector that can be used via Project Automation to query for list item attachments and to add the attachments to an existing item.
API | Returns | Description |
---|---|---|
GetListItemAttachmentUrls(int itemid) | List<string> | List of attachment URLs |
GetSharePointListService() | Lists | SharePoint Lists.asmx Web Service reference |
GetSharePointWebService() | Webs | SharePoint Webs.asmx Web Service reference |
GetWebClient() | WebClient | Authenticated .NET Framework WebClient object |
GetListItemAttachment(string url) | byte[] | Downloads a byte array of the attachment file data |
GetClientContext() | ClientContext | SharePoint Client Object Model ClientContext object |
GetWebRequestHelper() | HttpWebRequestHelper | Authenticated HttpWebRequestHelper object. |
Example using the REST API
How to add an attachment to a List item from a file located on disk.
info
We are using the AfterAddItem
project automation items event to process list item attachments as item are ADDED to SharePoint. You will also likely need to implement the same for when items are UPDATED.
public override void AfterAddItem(object sender, DataCompareItemInvariant item, object identity)
{
var fileName = "C:\\Temp\\readme.txt";
var url = $"{DataSourceB.SharePointUrl}/_api/web/lists/GetByTitle('{DataSourceB.ListName}')/items({identity})/AttachmentFiles/add(FileName='{Path.GetFileName(fileName)}')";
var helper = DataSourceB.GetWebRequestHelper();
using(var wc = helper.GetWebClient())
{
wc.UploadData(url, "POST", File.ReadAllBytes(fileName));
}
}