Update SPListItem in workflow SharePoint

Posted On // Leave a Comment

Problem:

In workflow SharePoint context
SPListItem itemAssociateWithWorkflow =  this.workflowProperties.Item;
itemAssociateWithWorkflow["Your field name"] = "your value";
itemAssociateWithWorkflow.Update();
After update successfully, that item will be modified by “System Account” that means field Modified By has value System Account

Solve:

SPUser currentUser = GetCurrentUserInWorkflow(this.workflowProperties);
////impersonate real current user
using (SPSite site = new SPSite(this.workflowProperties.Web.Site.ID, currentUser.UserToken))
{
    using (SPWeb webCurrentUser = site.OpenWeb(this.workflowProperties.Web.ID))
    {
     SPListItem itemCurrentUser = webCurrentUser.Lists  [this.workflowProperties.ListId].GetItemById(this.workflowProperties.ItemId);
        itemCurrentUser["your field name"] = "your value";
        itemCurrentUser.Update();
    }
}

0 comments :

Post a Comment