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.
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.
- using Microsoft.SharePoint.Client;
- 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.
- string login = "[email protected]";
- string password = "P@ssw0rd";
- var securePassword = new SecureString();
- foreach(char c in password)
- {
- securePassword.AppendChar(c);
- }
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,
- SecureString securePassword = new SecureString();)
b) Now initialize an instance of the ClientContext object for the specified SharePoint site.
- string siteUrl = "https://listdemo.sharepoint.com/sites/mysite";
- 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",
- 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.
- ListItem CreationInformationitemInfo = newListItemCreationInformation();
- ListItem myItem = myList.AddItem(itemInfo);
- myItem["Title"] = "New Item";
- myItem["Description"] = "Item Description";
d) Now update the New list item,
- myItem.Update();
- var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
- clientContext.Credentials = onlineCredentials;
- clientContext.ExecuteQuery();
Step 4: Here's the complete code,
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- using Microsoft.SharePoint.Client;
- using System.Security;
-
-
- namespace InsertItemsInSPList
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- string login = "[email protected]";
- string password = "givepasswordhere";
- var securePassword = newSecureString();
- foreach(char c in password)
- {
- securePassword.AppendChar(c);
- }
-
- string siteUrl = "https://listdemo.sharepoint.com/sites/mysite";
- ClientContextclientContext = new ClientContext(siteUrl);
- Microsoft.SharePoint.Client.ListmyList = clientContext.Web.Lists.GetByTitle("myproducts");
- ListItemCreationInformationitemInfo = newListItemCreationInformation();
- ListItemmyItem = myList.AddItem(itemInfo);
- myItem["Title"] = "My New Item";
- myItem["Description"] = "New Item Description";
- try {
- myItem.Update();
- var onlineCredentials = newSharePointOnlineCredentials(login, securePassword);
- clientContext.Credentials = onlineCredentials;
- clientContext.ExecuteQuery();
- Console.WriteLine("New Item inserted Successfully");
- } catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- Console.ReadLine();
- }
- }
- }
Step 5:Run the code and check the Console. You will see the output below,
Step 6:Open the SharePoint list and check the new item,
Read more articles on SharePoint: