![](/img/trans.png)
[英]C# - Add a File as Attachment to Microsoft.SharePoint.Client.ListItem (SharePoint 2010)
[英]C# SharePoint Client List Attachment
我正在嘗試將文件附加到共享點上的列表中。
我可以通過編程方式創建列表項,但無法附加附件。 要么它給我一個401不允許錯誤(因為我可以完全訪問列表),要么executeQuery永遠掛起,直到超時。
這是我當前的代碼:(WPF應用程序)
ClientContext clientContext = new ClientContext("http://<SITE-URL>/sites/Team-Place/<TeamPlace-ID>/");
clientContext.RequestTimeout = int.MaxValue;
FileStream sr = new FileStream("Test.pdf", FileMode.Open);
byte[] contents = new byte[sr.Length];
sr.Read(contents, 0, (int)sr.Length);
SP.List List = clientContext.Web.Lists.GetByTitle("<List Title>");
if (List != null)
{
CamlQuery camlQuery = CamlQuery.CreateAllItemsQuery();
SP.ListItemCollection Collection = List.GetItems(camlQuery);
clientContext.Load(List);
clientContext.Load(List.Fields);
clientContext.Load(Collection);
clientContext.ExecuteQuery();
foreach (var x in List.Fields)
Debug.AppendText(x.InternalName + "\n");
ListItemCreationInformation creationInfo = new ListItemCreationInformation();
SP.ListItem Item = List.AddItem(creationInfo);
Item["Title"] = "Test";
Item["Modell"] = "Test";
Item["Seriennummer"] = "testserial";
Item["Ger_x00e4_te_x002d_Typ"] = "Laptop";
Item.Update();
clientContext.ExecuteQuery();
clientContext.Load(Item);
clientContext.ExecuteQuery();
var attInfo = new AttachmentCreationInformation();
attInfo.FileName = "Test.pdf";
attInfo.ContentStream = sr;
var att = Item.AttachmentFiles.Add(attInfo);
Item.Update();
clientContext.Load(att);
clientContext.Load(Item);
clientContext.ExecuteQuery();
//System.Diagnostics.Debug.WriteLine(att.ServerRelativeUrl);
Item.Update();
clientContext.ExecuteQuery();
/*
* Not working pice of S#@*
string attachmentPath = string.Format("/Lists/Inventur_MOBI/Attachments/{0}/{1}", Item.Id, "Test.pdf");
SP.File.SaveBinaryDirect(clientContext, attachmentPath, sr, false);
*/
}
else
Debug.AppendText("List not found");
我確實“檢查”了一些東西。 SaveBinardDirect方法給了我一個401不允許,附件給出了超時。 我們有一個Sharepoint 2013。
有人有主意嗎?
關於BlueFire
您的代碼有兩個問題。
首先,使用FileStream
讀取文件,然后將其與AttachmentCreationInformation
對象一起使用。 更改為:
byte[] contents = null;
using (FileStream sr = new FileStream("Test.pdf", FileMode.Open))
{
contents = new byte[sr.Length];
sr.Read(contents, 0, (int)sr.Length);
}
//...
using (MemoryStream ms = new MemoryStream(contents))
{
var attInfo = new AttachmentCreationInformation();
attInfo.FileName = "Test.pdf";
attInfo.ContentStream = ms;
// ...
}
其次,在創建新的ListItem
對象之后,請再次獲取它,以保護您免於保存沖突。 采用:
ListItem newItem = List.AddItem(creationInfo);
newItem["Title"] = "Test";
// ...
newItem.Update();
clientContext.Load(newItem, i => i.Id);
clientContext.ExecuteQuery();
var item = List.GetItemById(newItem.Id);
using (MemoryStream ms = new MemoryStream(contents))
{
// ...
var att = item.AttachmentFiles.Add(attInfo);
item.Update();
clientContext.ExecuteQuery();
}
哦,對於401 Unathorized,您需要將憑據傳遞給ClientContext:
clientContext.Credentials = new NetworkCredential("user", "password", "domain");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.