如何以编程方式更改CRM中用户的业务单位?

时间:2015-01-21 15:54:32

标签: c# dynamics-crm dynamics-crm-4

我已完成以下操作,这不会导致错误,但不会更新我的用户业务部门:

QueryExpression query = new QueryExpression();
query.EntityName = "systemuser";
query.ColumnSet = new AllColumns();

ConditionExpression ce = new ConditionExpression();
ce.AttributeName = "businessunitid";
ce.Operator = ConditionOperator.Equal;
ce.Values = new string[] { My BU Guid };

ConditionExpression ce2 = new ConditionExpression();
ce2.AttributeName = "lastname";
ce2.Operator = ConditionOperator.Equal;
ce2.Values = new string[] { Users Lastname };

FilterExpression filter = new FilterExpression();
filter.Conditions = new ConditionExpression[] { ce, ce2 };
query.Criteria = filter;

try
{
    BusinessEntityCollection UserCollection = crmService.RetrieveMultiple(query);
    foreach (BusinessEntity be in UserCollection.BusinessEntities)
    {
        //Update users BU. Roles will need to be added back in.
        Guid newBu = new Guid(New BU Guid);
        systemuser su = (systemuser)be;
        su.businessunitid.Value = newBu;
        TargetUpdateSystemUser sysuserUpdate = new TargetUpdateSystemUser() { SystemUser = su };
        UpdateRequest userBUUpdate = new UpdateRequest() { Target = sysuserUpdate };
        crmService.Execute(userBUUpdate);
    }
catch
{
}

此时此操作返回1个用户并且不会抛出任何错误。但是,在检查CRM中的用户时,他们的BU没有改变。我也尝试过简单的crmService.Update(su),但这也不起作用。我还需要做什么?

1 个答案:

答案 0 :(得分:3)

您无法将业务单位更新为其他标准字段。

您需要使用SetBusinessSystemUserRequest

https://msdn.microsoft.com/en-us/library/bb929627.aspx