Friday, November 27, 2009

Downloading Attachments from a List

So I was asked to get all attachments from a List. Now, there are several articles on the web that show you how to leverage the Object Model, so I'm not going to get into that. What I was asked to do was to connect to a remote SharePoint instance and bring down all attachments found in a list.

My first inclination was to leverage the Object Model, but after spending an hour on it, I remembered, I can't connect to a remote instance via the Object Model. So I was left with Web Services. The solution wasn't bad at all either. Once you've connected to Lists service, you can call the GetAttachmentCollection method to return attachments per list item. I'm assuming that everyone knows how to grab List ids and ListItem ids via Web services, so I'll skip that tutorial.

Here's the code:

XmlNode attachmentColl = wsListObj.GetAttachmentCollection(listIDStringWithBrackets, listItemIDString);

for (int i = 0; i < attachmentColl.ChildNodes.Count; i++)
{
string[] attachmentArr = attachmentColl.ChildNodes[i].InnerXml.Split(new char[] { '/' });

System.Net.WebRequest file = System.Net.WebRequest.Create(attachmentColl.ChildNodes[i].InnerXml);

CopyFileToFolder(file.GetResponse().GetResponseStream(),
attachmentArr[attachmentArr.Length - 1]);

file = null;
}

No comments:

Post a Comment