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

How can you change the Business Unit of a user in CRM programmatically?

我已经完成了以下操作,没有出现任何错误,但没有更新我的用户业务部门:

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
{
}

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

您不能像其他标准字段一样更新业务单位。

您需要使用 SetBusinessSystemUserRequest

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