Adding Items Into A SharePoint list Programmatically Using C#

In this article we will to learn how to programmatically insert items into a SharePoint list using C#. Here, I am writing the code in a console application.

Step 1: Go to File, New, Project, Templates, then Visual C#, Windows and select the Console Application,

You will get the Project structure like below and open the Program.cs file.

code

Step 2: Go to Solution Explorer, select ‘References’, then right click and add the following references,

‘Microsoft.SharePoint.Client’, ‘System.Security’, ‘Microsoft.SharePoint.Client.Runtime’. Will see the use of these later in this article.

a) Refer the namespaces in the code at the namespace list at the starting of the program.

  1. using Microsoft.SharePoint.Client;  
  2. using System.Security;  
b) We require these namespaces to utilize ClientContext, ListItemCreationInformation, ListItem, SharePointOnlineCredentials, SecureString classes which we are going to use in this code.

Step 3: Now let’s start the implementation

a) To insert a list item from console we first need to connect with the SharePoint site. For that we need valid login and secure password.
  1. string login = "admin@listdemo.onmicrosoft.com"//give your username here  
  2. string password = "P@ssw0rd"//give your password  
  3. var securePassword = new SecureString();  
  4. foreach(char c in password)  
  5. {  
  6.     securePassword.AppendChar(c);  
  7. }  
1. Here we used the SecureString() class which is similar to a string object and stores a text value that should be kept confidential. The use of a SecureString is to secure a user's password to use it as a credential while starting a new process.

2. The value of the SecureString is automatically encrypted when the instance is initialized. Note: Can also instantiate it like,
  1. SecureString securePassword = new SecureString();)  
b) Now initialize an instance of the ClientContext object for the specified SharePoint site.
  1. string siteUrl = "https://listdemo.sharepoint.com/sites/mysite";  
  2. ClientContext clientContext = new ClientContext(siteUrl);  
1. The use ofClientContext class is to return context information about current web application, site collection and sites etc. To know more about ClientContext please refer this link.

c) Start getting list and creating list item objects,

1. Get the list from web called "myproducts",
  1. Microsoft.SharePoint.Client.ListmyList= clientContext.Web.Lists.GetByTitle("myproducts");  
2. To specify the properties of the new list item create an abject of ListItemCreationInformation and pass it as parameter to the method called AddItem of the List class.
  1. ListItem CreationInformationitemInfo = newListItemCreationInformation();  
  2. ListItem myItem = myList.AddItem(itemInfo);  
  3. myItem["Title"] = "New Item";  
  4. myItem["Description"] = "Item Description";  
d) Now update the New list item,
  1. myItem.Update();  
  2. var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);  
  3. clientContext.Credentials = onlineCredentials;  
  4. clientContext.ExecuteQuery();  
Step 4: Here's the complete code, 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. using Microsoft.SharePoint.Client;  
  8. using System.Security;  
  9.   
  10.   
  11. namespace InsertItemsInSPList  
  12. {  
  13.     class Program  
  14.     {  
  15.         static void Main(string[] args)  
  16.         {  
  17.   
  18.             string login = "admin@listdemo.onmicrosoft.com"//give your username here  
  19.             string password = "givepasswordhere"//give your password  
  20.             var securePassword = newSecureString();  
  21.             foreach(char c in password)  
  22.             {  
  23.                 securePassword.AppendChar(c);  
  24.             }  
  25.   
  26.             string siteUrl = "https://listdemo.sharepoint.com/sites/mysite";  
  27.             ClientContextclientContext = new ClientContext(siteUrl);  
  28.             Microsoft.SharePoint.Client.ListmyList = clientContext.Web.Lists.GetByTitle("myproducts");  
  29.             ListItemCreationInformationitemInfo = newListItemCreationInformation();  
  30.             ListItemmyItem = myList.AddItem(itemInfo);  
  31.             myItem["Title"] = "My New Item";  
  32.             myItem["Description"] = "New Item Description";  
  33.             try {  
  34.                 myItem.Update();  
  35.                 var onlineCredentials = newSharePointOnlineCredentials(login, securePassword);  
  36.                 clientContext.Credentials = onlineCredentials;  
  37.                 clientContext.ExecuteQuery();  
  38.                 Console.WriteLine("New Item inserted Successfully");  
  39.             } catch (Exception e)  
  40.             {  
  41.                 Console.WriteLine(e.Message);  
  42.             }  
  43.             Console.ReadLine();  
  44.         }  
  45.     }  
  46. }  
Step 5:Run the code and check the Console. You will see the output below,

output

Step 6:Open the SharePoint list and check the new item,

new item

 

Read more articles on SharePoint: