Thursday, December 29, 2011

Workflow related content field contains old item title

We have the following workflow:

  1. User enters new item without Title and workflow starts
  2. New Title is built and set in workflow
  3. Task is created

image

Related content field in task should contain new Title but it contains the empty one. You can create empty workflow action which has [PersistOnClose] attribute which commits all pending changes but it doesn’t help in this case. So we have created custom event receiver for Tasks list. It solves the problem.

public override void ItemAdding(SPItemEventProperties properties)
{
try
{
string listIdText = properties.AfterProperties["WorkflowListId"] as string;
string itemIdText = properties.AfterProperties["WorkflowItemId"] as string;
string workflowLink = properties.AfterProperties["WorkflowLink"] as string;
if (!string.IsNullOrEmpty(listIdText) &&
!string.IsNullOrEmpty(itemIdText) &&
!string.IsNullOrEmpty(workflowLink))
{
Guid listId = new Guid(listIdText);
int itemId = Convert.ToInt32(itemIdText);

SPList list = properties.Web.Lists[listId];
SPListItem item = list.GetItemById(itemId);

SPFieldUrlValue url = new SPFieldUrlValue(workflowLink);
url.Description = item.Title;

properties.AfterProperties.ChangedProperties.Add("WorkflowLink", url.ToString());
}
}
catch (Exception ex)
{
Trace.WriteLine("WorkflowTaskEventReceiver.ItemAdding: " + ex.Message);
}
base.ItemAdding(properties);
}


Links:
About workflow batches and commit pending changes action

No comments:

Post a Comment