In this post, I will show you the way how to get the list attachments using SharePoint web services. There can be one or more attachment attached to the list item of any list in the site.
For example, I have one Dummy List in the site and I have one list item with two attachments. See the figure below.
Now write down the following code. DownloadListItems is the lists.asmx web service of SharePoint. Change the site url reference for your code.
private void button8_Click(object sender, EventArgs e)
{
DownloadListItems.Lists objLists = new EncryptAndDecrypt.DownloadListItems.Lists();
objLists.Credentials = System.Net.CredentialCache.DefaultCredentials;
objLists.Url = "{site-url}/_vti_bin/lists.asmx";
XmlNode node = objLists.GetAttachmentCollection("DummyList", "7");
DataSet ds = new DataSet();
using (XmlNodeReader reader = new XmlNodeReader(node))
{
ds.ReadXml(reader);
}
DataTable dtAttachment = ds.Tables[0];
for (int iCnt = 0; iCnt <= dtAttachment.Rows.Count - 1; iCnt++)
{
string sourceUrl = Convert.ToString(dtAttachment.Rows[iCnt]["Attachment_Text"]);
int strLastIndx = sourceUrl.LastIndexOf(@"/");
string FileName = sourceUrl.Substring(strLastIndx + 1);
using (WebClient client = new WebClient())
{
client.UseDefaultCredentials = true;
byte[] response = client.DownloadData(sourceUrl);
FileStream fStream = new FileStream(@"C:\DummyListAttachments\" + FileName, FileMode.Create, FileAccess.ReadWrite);
fStream.Write(response, 0, response.Length);
fStream.Close();
}
}
}
As you can see first we have taken the reference of the Lists.asmx service and then passed the credentials. Change it to your credentials according to your need. Then we called getattachmentcollection method, and simple got the attachments in the form of XMLNode.
We actually get path only, not the real attachments. For simplicity I have converted that XMlnode into to data table so that we get the Attachment table and attachment_xml column with values as direct path to the attachment with complete URL of the site.
And then finally we use WebClient method to pull those documents to our local drive.
Very simple and easy way to get the attachments from list item. What say?
5 comments:
Thanks for this superb post.. this really saved my time
Hi,
I am getting an "The type or namespace name 'DownloadListItems' could not be found (are you missing a using directive or an assembly reference?)" error.
I am trying this example from a machine which does not have Sharepoint 2010 installed on it, but trying to download all attachments from a list where sharepoint is installed on another server.
Can you guide me please.
Rgds,
Soni
Soni,
you need to have at-least Microsoft.SharePoint.dll to build project.
Thank you.
Hi,
Good work, Thanks.
I did run the same code for my SP list, the code works perfect for the list item that has more than one file attachments.
For the list items that has got only one file attachment am getting the error "Column 'Attachment_Text' does not belong to table Attachments."
Am getting this error only for the list item that has only one attachments for the rest it works like a charm.
Any thoughts on this error?
Thanks
Post a Comment