Tuesday, 10 July 2012

Code to Reassigning and Publishing Workflow in CRM 2011

In this article , I am going to explain how to unpublished workflow and assigned to new user
and again changing workflow state draft to published

public static void AssignAndPublishWorkflow(ServerConnection.Configuration serverConfig,
Guid workflowid,Guid userid)
             using(OrganizationServiceProxy serviceProxy = new            OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credntials, serverConfig.DeviceCredentials))
                 IOrganization service = (IOrganization)serviceProxy;

                 //Unpublish the workflow
                 SetStateRequest unpubReq = new SetStateRequest();
                 unpubReq.EntityMoniker = new EntityReference("workflow",workflowid);
                 unpubReq.State = new OprionSetValue(0); //draft state
                 unpubReq.Status = new OprionSetValue(1); //draft status
                 SetStateResponse unpubResp = (SetStateResponse)service.Excute(unpubReq);

                 //assign the workflow to the new User
                 AssignRequest assignReq = new AssignRequest();
                 assignReq.Target = new EntityReference("workflow",workflowid);
                 assignReq.Assignee = new EntityReference("systemuser",userid);
                 AssignResponse assignResp = (AssignResponse)service.Execute(assignReq);

                 //impersonate the new userid
                 serviceProxy.CallerId = userid;

                 //publish the workflow
                 SetStateRequest pubReq = new SetStateRequest();
                 pubReq.EntityMoniker = new EntityReference("workflow",workflowid);
                 pubReq.State = new OprionSetValue(1); //published state
                 pubReq.Status = new OprionSetValue(2); //published status
                 SetStateResponse pubResp = (SetStateResponse)service.Excute(pubReq);

No comments:

Post a Comment

Note: only a member of this blog may post a comment.