在我的公司中,附加到sharepoint列表的所有文件都存储在一个文件夹中,其中包含Path:https://SharePointListURL/Dokuments/
将一个文件上传到此文件夹没有问题。
private void UploadFile(string FileName, System.Uri SharePointURL, string ListName, string SubFolder)
{
#region PreChecks
if (SharePointURL.ToString().Substring(SharePointURL.ToString().Length - 1, 1) != "/") { SharePointURL = new System.Uri(SharePointURL.ToString() + "/"); }
if(SubFolder.Substring(0,1) == "/") { SubFolder = SubFolder.Substring(1, SubFolder.Length - 1); }
if(SubFolder.Substring(SubFolder.Length - 1, 1) != "/") { SubFolder = SubFolder + "/"; }
if (!System.IO.File.Exists(FileName)) { throw new System.IO.FileNotFoundException(); }
#endregion
#region Sharepoint connection
Microsoft.SharePoint.Client.ClientContext cC = new Microsoft.SharePoint.Client.ClientContext(SharePointURL) { Credentials = System.Net.CredentialCache.DefaultCredentials };
Microsoft.SharePoint.Client.List SPList = cC.Web.Lists.GetByTitle(ListName);
#endregion
#region Define file stream
byte[] FileContent = System.IO.File.ReadAllBytes(FileName);
Microsoft.SharePoint.Client.FileCreationInformation fci = new Microsoft.SharePoint.Client.FileCreationInformation();
#endregion
#region Define FileCreationInformation
fci.Overwrite = true;
fci.Url = SubFolder + System.IO.Path.GetFileName(FileName);
fci.Content = FileContent;
#endregion
#region Uploading file
SPList.RootFolder.Files.Add(fci);
cC.ExecuteQuery();
#endregion
}
现在,我想将这些文件链接到目标SharePoint.ListItems。我搜索了ListItem的所有FieldValues但我不知道存储附加文件的路径。
有人可以告诉我,如何将这些文件附加到指定的ListItem?
文件夹http://SharePointListURL/Lists/ListName/Attachments/ItemID/
不存在,因为如果您在google中搜索有多个页面解释,则只需将文件上传到此文件夹即可。
谢谢你, 扬
答案 0 :(得分:0)
SharePoint 2010
看起来我错过了它是SharePoint 2010。
以下是您如何做到这一点的绝佳答案:https://stackoverflow.com/a/11685304/1498401
基本上,无法使用SharePoint 2010 CSOM,您必须使用Lists.asmx webservice。
SharePoint 2013
要从文档库https://SharePointListURL/Dokuments/
上传文件作为http://SharePointListURL/Lists/ListName/
列表中listitem的附件,请使用AttachmentCreationInformation
ClientContext context = new ClientContext("https://SharePointListURL");
var library = context.Web.Lists.GetByTitle("Documents");
var list = context.Web.Lists.GetByTitle("Test");
var file = library.GetItemById(1).File;
var data = file.OpenBinaryStream();
context.Load(file);
context.ExecuteQuery();
var item = list.GetItemById(1);
context.Load(item);
context.ExecuteQuery();
var attachment = new AttachmentCreationInformation();
attachment.FileName = file.Name;
using (MemoryStream ms = new MemoryStream())
{
data.Value.CopyTo(ms);
attachment.ContentStream = ms;
item.AttachmentFiles.Add(attachment);
context.ExecuteQuery();
}