Create SharePoint Workflow History list using csom programmatically

This SharePoint tutorial explains, how to create a workflow history list using csom programmatically in SharePoint Online. Basically, we will use here .Net managed object model (C#.Net) code in SharePoint Online.

This particular example, I have created a console application using visual studio 2017 and connecting to a SharePoint Online site.

Create workflow history list programmatically in SharePoint

To work with .Net managed object model (csom) code in SharePoint, we need to add the below two dlls, which you can download from the NuGet package.

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

You can also check how to create a workflow history list using PowerShell or designer in SharePoint Online.

Here we are taking both the list title (display name) and name (URL). And we are checking if the list exists with the title. If not exists then we are creating the history list. We need to choose the list template as ListTemplateType.WorkflowHistory whose value in 140.

Here the username and password, I have saved in the config file.

public static void CreateWorkflowHistoryList(string siteURL)
{
using (ClientContext ctx = new ClientContext(siteURL))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
var web = ctx.Web;
ctx.ExecuteQuery();
ListCollection listCollection = ctx.Web.Lists;
var listName = "OurWorkflowHistory";
var listTitle = "Our 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();
}
}
}

private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings["SPOPassword"])
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings["SPOAccount"];
}
catch
{
throw;
}
}

Once you run the code, you can see the history list got created successfully like below:

Create workflow history list programmatically in SharePoint

You may like following SharePoint csom tutorials:

I hope this will be helpful to create a workflow history list using csom (.Net client object model code ) programmatically in SharePoint 2013/2016/Online.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>