Introduction

This blog is mainly focused on SharePoint
I will use this to store and share all the tips and tricks about SharePoint and the related products
List of all posts

Inpersonate a workflow action by code


Inpersonate a workflow action
Even if you use the impersonate step in your worflow the action will run as administrator,
which means that if you copy an item it will have the administrator as owner even if you change the author property during the process, so this will not trigger a new workflow on item created.
The option is to impersonate your custom action, this partial code shows how to do it.

Partial code
       protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                using (SPWeb web = (SPWeb)(__Context.Web))
                {
                    Guid listGuid = new Guid(__ListId);
                    SPList myList = web.Lists[listGuid];
                    SPListItem myItem = myList.GetItemById(__ListItem);
                    SPUserToken spusertoken = web.AllUsers[WorkflowUser].UserToken;
                    using (SPSite destSite = new SPSite(DestinationListUrl, spusertoken))
                    {
                        using (SPWeb destWeb = destSite.OpenWeb())
                        {
                            SPList destList = destWeb.GetList(ProcessStringField(executionContext, DestinationListUrl));
                            SPUser SPAuthor = destWeb.AllUsers[WorkflowUser];                         
                            ProcessActivity(service, myItem, destList, SPAuthor);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                return ActivityExecutionStatus.Faulting;
            }
            return ActivityExecutionStatus.Closed;
        }

No comments:

Post a Comment

by Category