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. |
How to add an attachment to a List item from a file located on disk.
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 helper = DataSourceB.GetWebRequestHelper();
var fileName = "C:\\Temp\\readme.txt";
var url = helper.CombinePaths(DataSourceB.SharePointUrl, string.Format("/_api/web/lists/GetByTitle('{0}')/items({1})/AttachmentFiles/add(FileName='{2}')", DataSourceB.ListName, identity, Path.GetFileName(fileName)));
using(var wc = helper.GetWebClient())
{
wc.UploadData(url, "POST", File.ReadAllBytes(fileName));
}
}