How to get the Impersonation User Id(systemUserId ) in D365

public Guid getUserId(IOrganizationService service, string userName)
        {
            Guid systemUserId = Guid.Empty;
            try
            {
                QueryByAttribute queryByAttribute = new QueryByAttribute("systemuser");
                ColumnSet columns = new ColumnSet("systemuserid");
                queryByAttribute.AddAttributeValue("fullname", userName);

                EntityCollection retrieveUser = service.RetrieveMultiple(queryByAttribute);
                systemUserId = ((Entity)retrieveUser.Entities[0]).Id;
            }
            catch (FaultException<OrganizationServiceFault> e)
            {
                throw new Exception("Error: >>" + e.Message);
            }

            return systemUserId;
        }

   IOrganizationService systemService = serviceFactory.CreateOrganizationService(getUserId(service, “SYSTEM”));

Note:
Whenever we run the workflow on-demand, we get the UserId and IntiatingUserId as follows.
UserId: User running on-demand workflow.
InitiatingUserId: User running on-demand workflow.

And whenever any workflow get trigger automatically
UserId: Workflow owner
InitiatingUserId: Triggering user.


Comments

Popular posts from this blog

Basic Plugin Code in D365 using C#

CURD (Create, Update, Retrieve and Delete) Operation in D365 using Power Shell Script

Meta Data Using WebApiRequest