0

in my company, all files attached to a sharepoint list are stored in one folder with the Path: https://SharePointListURL/Dokuments/ The upload of one file to this folder is no problem.

    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
    }

Now, I want to link this files to their target SharePoint.ListItems. I searched all the FieldValues of the ListItem but I don't know, where this path to the attached file is stored.

Can anybody tell me, how I can attach this files to the specified ListItem?

The folders http://SharePointListURL/Lists/ListName/Attachments/ItemID/ do not exist, because if you search in google there are several pages explaining, you just have to upload the file to this folders.

Thank you, Jan

CJens
  • 39
  • 8
  • Ok, it seem that nobody can help me here. In many articles, there is the Attachment very easy by ListItem.Attachments. This member is not available using the Microsoft.SharePoint.Client.dll but it should be available in the Microsoft.SharePoint.dll (the dll on the server). I cannot access this server-libarary. Is it in the end possible to solve my problem? Does anybody know? – CJens May 30 '17 at 17:20

1 Answers1

0

SharePoint 2010

Looks like that I have missed that it's a SharePoint 2010.

Here is great answer of how you could do this: https://stackoverflow.com/a/11685304/1498401

Basically it can't be done with SharePoint 2010 CSOM and you have to to use Lists.asmx webservice.

SharePoint 2013

To upload file from your document library https://SharePointListURL/Dokuments/ as an attachment to listitem in your http://SharePointListURL/Lists/ListName/ list use 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();
}
tinamou
  • 2,282
  • 3
  • 24
  • 28
  • Hello, thank you very much - this seems to be quite interesting. In referenced the Library Microsoft.SharePoint.Client.dll in Version 4.30319, but there I cannot find AttachmentCreationInformation() - I just got CreateValueObject. Does anybody know, why this is missing in my SP2010-library? – CJens May 31 '17 at 06:01
  • ok, my bad. it,' 2010 version so there won't be AttachmentCreationInformation, i will update my answer – tinamou May 31 '17 at 06:12
  • Ok, got the wrong version. Now, AttachmentCreationInformation are available. – CJens May 31 '17 at 07:19
  • No, it's not your fault - I had to update the library. – CJens May 31 '17 at 07:20