C# SharePoint 客户端列表附件
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");
我做了 "censor" 一些事情。 SaveBinardDirect 方法给我一个不允许的 401 并且附件给出了超时。
我们有一个 Sharepoint 2013。
有人有想法吗?
问候
蓝火
你的代码有两个问题。
首先,使用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");
我正在尝试将文件附加到我们共享点上的列表。
我可以通过编程方式创建列表项,但无法附加附件。 它要么给我一个 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");
我做了 "censor" 一些事情。 SaveBinardDirect 方法给我一个不允许的 401 并且附件给出了超时。 我们有一个 Sharepoint 2013。
有人有想法吗?
问候 蓝火
你的代码有两个问题。
首先,使用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");