Thursday, February 5, 2015

Updating list item with attachment using CSOM

ClientContext clientContext = new ClientContext(siteUrl);
List oList = clientContext.Web.Lists.GetByTitle(listName);
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem oListItem = oList.AddItem(itemCreateInfo);
oListItem["Title"] = "Sample Text";
oListItem.Update();
clientContext.ExecuteQuery();
if (FileUploadControl.HasFiles)
{
foreach (var file in FileUploadControl.PostedFiles)
        {
        byte[] contents = new byte[Convert.ToInt32(file.ContentLength)];
                Stream fStream = file.InputStream;
                fStream.Read(contents, 0, Convert.ToInt32(file.ContentLength));
                fStream.Close();
                MemoryStream mStream = new MemoryStream(contents);
                AttachmentCreationInformation aci = new AttachmentCreationInformation();
                aci.ContentStream = mStream;
                aci.FileName = file.FileName;
                Attachment attachment = oListItem.AttachmentFiles.Add(aci);
                clientContext.Load(attachment);
                clientContext.ExecuteQuery();
}
        FileUploadControl.Dispose();
}
clientContext.Dispose();

2 comments:

  1. Every time I visit your blog it really completes my day, and hey its not a joke. I am telling the truth. Thank you for always inspiring us and for writing a very touching article.

    zandra

    www.n8fan.net

    ReplyDelete

How to launch a document by clicking a link to the file?

Word: <a href='ms-word:ofe|u|path/to/web/word/document.docx'>Link to document</a> Excel: <a href='ms-excel:o...