我正在尝试使用以下代码下载SharePoint列表附件。我收到一个例外情况" Column' Attachment_Text'不属于表格附件"。请帮忙解决这个问题。
private static void fnDownloadattachment(string id)
{
Reference.Lists myservice = new Reference.Lists();
myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
myservice.Url = "http://XXXXX/_vti_bin/Lists.asmx";
XmlNode node = myservice.GetAttachmentCollection("listname", id);
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();
}
}
答案 0 :(得分:1)
此代码仅在您有一个附件时不起作用。
找到变通方法的最佳方法是在只有一个附件时查看DataTable,并查看列的名称而不是“Attachment_Text”。
你可以这样做:
foreach (DataColumn column in dtAttachment.Columns)
{
Console.Write(column.ColumnName);
}
执行此操作后,只需将“Attachment_Text”更改为必要的列名称。