fileattachment到SharePoint列表项目



在我的公司中,附加到SharePoint列表的所有文件都存储在一个文件夹中,并带有路径: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的所有字段值,但我不知道,该文件的此路径存储在哪里。

任何人都可以告诉我,如何将此文件附加到指定的ListItem?

文件夹http://SharePointListURL/Lists/ListName/Attachments/ItemID/不存在,因为如果您在Google中搜索有几个页面,您只需要将文件上传到此文件夹即可。

谢谢,Jan

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();
}

相关内容

  • 没有找到相关文章

最新更新