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();
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();
Hello Kannan,
ReplyDeleteneed 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.