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

Comments

  1. Hello Kannan,
    need small help, i am very new to SharePoint,
    I need to add the list items like first name, last name along with attachments using javascript, Could you please help me with the code.

    ReplyDelete

Post a Comment

Popular posts from this blog

IRM and the Object Model

This content database has a schema version which is not supported in this farm

Activate and Deactivate Feature through PowerShell