Share via


SharePoint Online: Adding SharePoint List Item with Attachment

This article shows you how to add the SharePoint list item using CSOM Managed .NET assembly that works on SharePoint Online.

When searching some basic example using this approach, you'll find so many examples for SharePoint 2010. Those examples are also not showing the correct way of creating the list item along with attachment.

.NET remote call to SharePoint Online is almost different then SharePoint 2010 CSOM using Managed .NET assemblies.

Example, ListItem object will not work with column's display name , It always requires the internal name of the Field class.

So you have to ensure all these collections and properties are loaded before using them.

Here is very basic workaround to start and explore the CSOM Managed.NET assembly through Console Application for SharePoint Online.

You have to explicitly load the List's fields and call the update item before attaching the file. You can also tweak this code to read it from your local folder.

using (ClientContext context = new ClientContext("https://codethinker.sharepoint.com"))
 {
 try
 {
 context.Credentials = new  SharePointOnlineCredentials("youruserId@tenant.onmicrosoft.com", ConvertToSecureString("yourpassword"));
 Web web = context.Web;
 List list = web.Lists.GetByTitle("Friends");
 context.Load(list, f => f.Fields);
 
 context.ExecuteQuery();
 
 ListItemCreationInformation creationInfo = new  ListItemCreationInformation();
 ListItem item = list.AddItem(creationInfo);
 
 //Retrieving the Field Objects by using the field display name
 Field lastName = list.Fields.GetByTitle("LastName");
 Field firstName = list.Fields.GetByTitle("FirstName");
 Field mobile = list.Fields.GetByTitle("Mobile");
 
 //Querying and Loading all fields properties
 context.Load(lastName); context.Load(firstName); context.Load(mobile);
 
 //Executing the Query
 context.ExecuteQuery();
 
 
 //Assigning the field value
 item[lastName.InternalName] = "Last Name goes here";
 item[firstName.InternalName] = "First Name here";
 item[mobile.InternalName] = "Mobile Number";
 
 
 //Adding the new records
 item.Update();
 //Commiting the changes to record
 context.ExecuteQuery();
  
 //You can add the attachment after you inserted the item
 AttachmentCreationInformation attachment = new  AttachmentCreationInformation();
 
 attachment.ContentStream = GenerateStreamFromString("Hello its test");
 
 attachment.FileName = "Mytext.txt";
 
 item.AttachmentFiles.Add(attachment);
 
 //Commiting attachment changes.
 context.ExecuteQuery();
 
 
 Console.WriteLine("Created successfully");
 Console.ReadLine();
 }
 catch(Exception ex)
 {
 Console.WriteLine(ex.Message);
 Console.ReadLine();
 }
 
 }
 //Convert to Secure string method
 
 public static  SecureString ConvertToSecureString(string password)
 {
 unsafe
 {
 
 fixed (char* passwordChars = password)
 {
 var securePassword = new  SecureString(passwordChars, password.Length);
 securePassword.MakeReadOnly();
 return securePassword;
 }
 }
 }
 
 //Returning Stream to assign the AttachmentCreationInformation Class
 
 public static  Stream GenerateStreamFromString(string s)
 {
 MemoryStream stream = new  MemoryStream();
 StreamWriter writer = new  StreamWriter(stream);
 writer.Write(s);
 writer.Flush();
 stream.Position = 0;
 return stream;
 }