CreateHistoryEvent(Int32, Object, SPMember, String, String, String)
CreateHistoryEvent(SPWeb, Guid, Int32, SPMember, TimeSpan, String, String, String)
second one is static so do not misinterpret parameters as I did first time :)
To create history event via function is helpful when you don't work directly with workflow code and so you can't use logToHistoryList activity.
This can be helpful when onTaskCreated activity doesn't work for example.(It can happen when you use replicator activity to creates tasks.) Microsoft describe solution for this here: http://support.microsoft.com/kb/970548 Obviously it is bug but it wasn't solve yet.
In brief: You need to create event receiver for Task list and then you can create workflow history event for example.
Here is one way how to achieve this:
using (SPSite site = new SPSite("http://sharepointserver/"))
{
using (SPWeb web = site.OpenWeb())
{
//Gets list where workflow runs
SPList workflowList = web.Lists["workflowList"];
//Loop through item in workflow list
foreach (SPListItem item in workflowList.Items)
{
//Find out which workflow item creates the task
if (properties.ListItem["Workflow Item ID"].ToString() == item["ID"].ToString())
{
try
{
//Log to the history list
//Use the foreach loop if there is just one workflow
//otherwise you should find correct workflow
foreach (SPWorkflow wf in item.Workflows)
{
wf.CreateHistoryEvent((int)SPWorkflowHistoryEventType.TaskCreated, 0, wf.AuthorUser, "", "Task was succesfully assigned to.", "");
}
//Work is done. Get out from loops
return;
}
catch (Exception ex)
{
//Some exception handling
}
}
}
}
}
Ok this is all. As I describe above this is just one of the ways how to write this code, so if you know better one do not hesitate to comment it. About bug with onTaskCreate activity I want to post later so this post is more focused on useful using of CreateHistoryEvent method.
Happy coding :)
No comments:
Post a Comment