How to Create SharePoint Workflow History List Programmatically using CSOM

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.

how to create workflow history list in sharepoint 2013
how to create workflow history list in sharepoint 2013

Click on New and then on Project.

sharepoint workflow list
sharepoint workflow list

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.

create workflow history list programmatically
create workflow history list programmatically

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.

sharepoint create workflow history list programmatically
sharepoint create workflow history list programmatically

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.

sharepoint 2013 workflow history list missing
sharepoint 2013 workflow history list missing

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.

sharepoint create history programmatically csom
sharepoint create history programmatically csom

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.

how to create workflow history list in sharepoint 2013 csom
how to create workflow history list in sharepoint 2013 csom

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.

sharepoint get workflow history list programmatically
sharepoint get workflow history list programmatically

Then on program.cs to perform the coding part.

create history list using csom in sharepoint online
create history list using csom in sharepoint online

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.

csom sharepoint 2013
csom sharepoint 2013

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.

create history list using csom sharepoint online
create history list using csom sharepoint online

Read some SharePoint csom tutorials below:

Conclusion

In this SharePoint tutorial, we learned how to create a SharePoint workflow history list programmatically using CSOM (Client side object model).

>