2017-05-30 66 views
0

在我的公司中,附加到SharePoint列表的所有文件都存储在一个文件夹中,路径为:https://SharePointListURL/Dokuments/将一个文件上传到此文件夹是没有问题的。将文件附加到Sharepoint列表项目

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/不存在,因为如果你在谷歌搜索有几个页面解释,你只需要上传文件到这个文件夹。

谢谢 扬

+0

确定以列表项,它似乎没有人可以帮助我在这里。在许多文章中,附件很容易被ListItem.Attachments使用。此成员不能使用Microsoft.SharePoint.Client.dll,但它应该在Microsoft.SharePoint.dll(服务器上的dll)中可用。我无法访问此服务器的libarary。它最终有可能解决我的问题吗?有人知道吗? – CJens

回答

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/列表使用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(); 
} 
+0

你好,非常感谢 - 这似乎很有趣。在引用版本4.30319中的库Microsoft.SharePoint.Client.dll,但在那里我找不到AttachmentCreationInformation() - 我刚刚得到CreateValueObject。有谁知道,为什么我的SP2010库中缺少这个功能? – CJens

+0

好的,我的坏。它,'2010版,所以不会有AttachmentCreationInformation,我会更新我的答案 – tinamou

+0

好的,得到了​​错误的版本。现在,AttachmentCreationInformation可用。 – CJens

相关问题