Getting default Workflow History List in SharePoint
When it is needed to add Workflow Association programmatically, then it requires instance of Workflow History List. There is 2 possibilities: list already exists or you should create it.
private static SPList GetHistoryList(SPWeb web)
{
string historyListName = SPResource.GetString(
"DefaultWorkflowHistoryListName",
new object[0]);
SPList historyList = web.GetListByName(historyListName);
if (historyList != null)
{
return historyList;
}
// create list if there is no such list yet
Guid listGuid = web.Lists.Add(
historyListName,
string.Empty,
SPListTemplateType.WorkflowHistory);
historyList = web.Lists.GetList(listGuid, false);
historyList.Hidden = true;
historyList.Update();
return historyList;
}
Here is in use extension method for SPWeb which returns null if list not found (default methods throwing Exception in this case). Here is the code of this extension method:
public static SPList GetListByName(this SPWeb web, string listName)
{
try
{
return SPUtility.GetSPListFromName(web, Guid.Empty, null, null,
listName);
}
catch
{
return null;
}
}