2013년 1월 31일 목요일

how to sharepoint list file download


how to sharepoint list file download


Hi All,

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?

http://www.sharepointkings.com/2010/05/download-attachments-from-list-item.html

댓글 없음: