将CRM用户复制到新组织时出错

时间:2014-05-09 08:23:25

标签: c# dynamics-crm-2011

我正在尝试将用户从一个组织复制到另一个组织,但是当创建发生时,我在行上收到此错误:

enter image description here

我正在使用的代码是

using (service2 = new OrganizationServiceProxy(organizationUri2, HomeRealmUri, Credentials, null))
{
    // If you use early bound entity classes, you have to include the line below. Not needed on late bound
    service2.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

    QueryExpression queryExpression = new QueryExpression("systemuser");
    queryExpression.ColumnSet = new ColumnSet(true);

    var users = service2.RetrieveMultiple(queryExpression);
    try
    {
        foreach (var user in users.Entities)
        {
            QueryExpression queryExpression2 = new QueryExpression("systemuser");
            queryExpression2.Criteria.AddCondition("firstname", ConditionOperator.Equal, user.Attributes["firstname"].ToString());
            queryExpression2.Criteria.AddCondition("lastname", ConditionOperator.Equal, user.Attributes["firstname"].ToString());
            var oldUsers = service.RetrieveMultiple(queryExpression2);
            if (oldUsers.Entities.Count > 0)
                continue;
            Entity systemuser = new Entity("systemuser");
            systemuser.Attributes["firstname"] = user.Attributes["firstname"].ToString();
            systemuser.Attributes["lastname"] = user.Attributes["lastname"].ToString();
            systemuser.Attributes["domainname"] = user.Attributes["domainname"].ToString();
            systemuser.Attributes["businessunitid"] = new EntityReference("businessunit", new Guid("F0EF93AE-25F9-E211-BE5F-00155D020C16"));
            Console.WriteLine("Creating " + user.Attributes["firstname"].ToString() + " " +  user.Attributes["lastname"].ToString());
            service.Create(systemuser);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.StackTrace);
        Console.WriteLine(e.InnerException.Message);
    }
    //Run();
}

为什么会这样?

0 个答案:

没有答案