Sunday, 1 September 2013

Impersonating User plugin code mscrm 2011



            #region Impersonate user
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            Guid userId = new Guid();
            QueryExpression qep = new QueryExpression();
            qep.EntityName = "systemuser";
            qep.ColumnSet = new ColumnSet();
            qep.ColumnSet.Columns.Add("systemuserid");
            ConditionExpression conExp = new ConditionExpression();
            conExp.AttributeName = "fullname";
            conExp.Operator = ConditionOperator.Equal;
            conExp.Values.Add("SYSTEM");
            FilterExpression fep = new FilterExpression();
            fep.Conditions.Add(conExp);
            qep.Criteria.AddFilter(fep);
            EntityCollection enCol = service.RetrieveMultiple(qep);
            foreach (Entity team in enCol.Entities)
            {
                userId = (Guid)team["systemuserid"];
            }
            service = factory.CreateOrganizationService(userId);
            #endregion

No comments:

Post a Comment