In this SharePoint csom tutorial, we will discuss how to create a SharePoint workflow history list programmatically using CSOM (Client side object model).
By using CSOM code, we can create, update, delete lists, library and other components in SharePoint Online or SharePoint 2013/2016.
Create SharePoint Workflow History List Programmatically using CSOM
Open Visual Studio 2017/2019 then click on Files from the ribbon in the top left corner to create a new project.
Click on New and then on Project.
Choose Visual C# and then click on Windows Classic Desktop to choose Console App(.Net Framework).
Here you can provide a specific Name to the project and choose your location where to save the project.
By default, it will provide names and saved locations but it’s advisable to give specific name while creating Your project and choose your location. Click on OK to create a console app.
Clicking on OK a page Console window appears which looks like as below. Here we don’t find any other Display windows like other application platforms like a web application or normal Windows application.
Simply we need to write the code under a static void method to create a History list. Before that, we need to install the SharePoint dlls which are prerequisite as without SharePoint dlls it won’t recognize functions.
To add the dlls click on Solution Explorer where you can see References and Right Click to view more options then click on Manage NuGet Packages. you can look into the below image which illustrates.
Clicking on NuGet packages a window popups where you can see three options as “Browse” “Installed” and “Updates”, Initially when the page opens it would be on Installed you need to click on “Browser”, and then type to search for Microsoft.SharePoint.client.dll and check for latest versions and publisher as Microsoft.
After Clicking on “Install” You can see Solutions is getting installed for your project. Click on “Ok” and install other dll to use a namespace in the code.
Now search for Microsoft.SharePoint.Client.Runtime.dll is also a necessary dll that needs to be installed to run the csom code perfectly and connect to Sharepoint.
Look for the latest version and Microsoft as publisher and then click on Install.
Then on program.cs to perform the coding part.
Here you get to code the CSOM coding. Before starting to code add ut using statements as so that the class and functions won’t show error messages and get underlined. Then start coding under the main statement.
Below is full code with all using statements and C# coding to create a history list.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.Security;
using System.Threading.Tasks;
namespace consoleapplicationtocreatehistorylist
{
class Program
{
static void Main(string[] args)
{
var siteURL = “https://onlysharepoint2013.sharepoint.com/sites/sptraining/”;
using (ClientContext ctx = new ClientContext(siteURL))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new SharePointOnlineCredentials(“bibhudutta@*******.onmicrosoft.com”, GetSPOSecureStringPassword());
var web = ctx.Web;
ctx.ExecuteQuery();
ListCollection listCollection = ctx.Web.Lists;
var listName = “NewWorkflowHistory”;
var listTitle = “New Workflow History”;
ctx.Load(listCollection, lists => lists.Include(list => list.Title).Where(list => list.Title == listTitle));
ctx.ExecuteQuery();
if (listCollection.Count > 0)
{
}
else
{
List ourHistoryList;
ListCreationInformation creationInfo = new ListCreationInformation();
creationInfo.Title = listTitle;
creationInfo.Url = listName;
creationInfo.TemplateType = (int)ListTemplateType.WorkflowHistory;
ourHistoryList = ctx.Web.Lists.Add(creationInfo);
ourHistoryList.Update();
ctx.ExecuteQuery();
ctx.Load(ourHistoryList);
ctx.ExecuteQuery();
Console.WriteLine(ourHistoryList.Title);
Console.ReadLine();
}
}
}
private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in “*************”)
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}
}
}
After coding click on IISExpress (Google Chrome) or start button to run the code and a console window appears where you can see a message pop-ups as the field name which is passed in the above code.
Read some SharePoint csom tutorials below:
- Bind SharePoint List items in dropdown list programmatically using CSOM
- Create Custom List and Fields in SharePoint Online using PnP Core CSOM Library
- Create and delete subsites under SharePoint site collection programmatically using PnP CSOM
- Create Choice Field in SharePoint Online Using PnP CSOM Library
- Create Modern Team Site and Subsite using pnp csom programmatically in SharePoint Online
- Rename or hide/remove SharePoint List Title Column Programmatically using PnP CSOM
- SharePoint Server relative urls must start with SPWeb.ServerRelativeUrl csom
- Get all SharePoint subsites programmatically using PnP CSOM
- Delete All items from the SharePoint List using PnP core CSOM Library Programmatically
- Create Folder and Subfolder in SharePoint Document Library using PnP Core CSOM Library
Conclusion
In this SharePoint tutorial, we learned how to create a SharePoint workflow history list programmatically using CSOM (Client side object model).
Hello Everyone!! I am Bhawana a SharePoint MVP and having about 10+ years of SharePoint experience as well as in .Net technologies. I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools. Now exploring more in SharePoint 2016 🙂 Hope here I can contribute and share my knowledge to the fullest. As I believe “There is no wealth like knowledge and no poverty like ignorance”