在CRM Online中以编程方式更改FullName(2011)

时间:2013-12-30 23:43:24

标签: c# dynamics-crm-2011 dynamics-crm-online

我正在尝试更改Dynamics CRM 2011在线帐户中现有CRM系统用户的“FullName”字段。我已经在设置中进行了更改,以便将所有未来用户更新为“Last,First”格式......所以这是为了更改现有用户。

我读到最好的方法是使用CRM SDK以编程方式执行此操作。当我执行实际的更新命令时,我从SDK收到一个未指定的错误:其他信息:无法修改属性IsLicensed。

虽然我正在查询实体对象SystemUsers的所有列,但我只是更改了FullName字段。有没有其他人有这方面的经验?我的代码如下,我将其作为控制台应用程序运行,以逐步执行每个SystemUser。


    static void Main(string[] args)
    {
        string connStr = ConfigurationManager.ConnectionStrings["CRMOnline"].ToString();
        CrmConnection conn = CrmConnection.Parse(connStr);
        conn.DeviceCredentials = DeviceIdManager.LoadOrRegisterDevice();
        using (OrganizationService svc = new OrganizationService(conn))
        {
            QueryExpression qry = new QueryExpression();
            qry.ColumnSet = new ColumnSet(true);   // get all columns
            qry.EntityName = CRMO.SystemUser.EntityLogicalName;   // get entity object SystemUser
            qry.Criteria.AddCondition(new ConditionExpression("calendarid", ConditionOperator.NotNull));   // but non-builtin users
            EntityCollection col = svc.RetrieveMultiple(qry);  // executes query

            foreach (Entity ent in col.Entities)
            { 
                Console.WriteLine();
                Console.WriteLine("Current Fullname: " + ent.Attributes["fullname"].ToString()); 
                Console.Write("Change? y/N: ");
                string ans = Console.ReadLine();
                if (ans.ToLower() == "y")
                {
                    Console.Write("New Name: ");
                    string newname = Console.ReadLine();
                    if (newname != "")
                    { 
                        ent.Attributes["fullname"] = newname;
                        svc.Update(ent);  // fails here with SDK error:  "Additional information: The property IsLicensed cannot be modified." 
                    } 
                }  
            }
            Console.WriteLine();
            Console.WriteLine("--- Done ---");
            Console.ReadLine(); 
        }  
    }

2 个答案:

答案 0 :(得分:2)

Crm SDK的规则28,不要通过执行select来执行更新,select会返回比您计划更新的字段更多的字段。即使实体未更改,实体的属性集合中的任何字段也将更新。相反,在本地实例化一个新实体,设置id以及要更新的任何属性并更新它。

在旁注中,you can't update the full name of a System User.您必须更新各个部分。所以你的代码看起来应该是这样的:

static void Main(string[] args)
{
    string connStr = ConfigurationManager.ConnectionStrings["CRMOnline"];
    CrmConnection conn = CrmConnection.Parse(connStr);
    conn.DeviceCredentials = DeviceIdManager.LoadOrRegisterDevice();
    using (OrganizationService svc = new OrganizationService(conn))
    {
        QueryExpression qry = new QueryExpression();
        qry.ColumnSet = new ColumnSet("firstname", "lastname", "fullname");   // get only what is needed for performance reasons
        qry.EntityName = CRMO.SystemUser.EntityLogicalName;   // get entity object SystemUser
        qry.Criteria.AddCondition(new ConditionExpression("calendarid", ConditionOperator.NotNull));   // but non-builtin users
        EntityCollection col = svc.RetrieveMultiple(qry);  // executes query

        foreach (Entity ent in col.Entities)
        { 
            Console.WriteLine();
            Console.WriteLine("Current Fullname: " + ent["fullname"].ToString()); 
            Console.Write("Update? Y/N: ");
            string ans = Console.ReadLine();
            if (ans.ToLower() == "y")
            {
                // Create a new entity, setting the id and whatever attributes that need to be updated
                var updateEntity = new Entity { Id = ent.Id };
                updateEntity["firstname"] = ent["firstname"];
                updateEntity["lastname"] = ent["lastname"];
                svc.Update(updateEntity);
            }  
        }
        Console.WriteLine();
        Console.WriteLine("--- Done ---");
        Console.ReadLine(); 
    }  
}

注意:

  • 仅检索您实际需要的列
  • 创建仅包含您要更新的字段的更新实体
  • 请记住,FullName只读

This may also be helpful

答案 1 :(得分:0)

这是其他人阅读此内容可以使用此解决方案来更改CRM Online中的FullName。

所以在我的情况下,我需要将现有CRM用户的FullName从“First Last”更改为“Last,First”,我能够执行常规的Office 365管理功能来完成此操作。

首先,我更改了CRM设置中的格式>系统设置为“姓氏,名字”。

然后,对于我需要更改的每个用户,我使用了Office 365管理中心并编辑了他们的许可证。从用户取消分配CRM许可证,然后单击“保存”。等待一两分钟以使更改生效。接下来,返回到相同的用户管理并将CRM许可证重新分配给用户,单击“保存”。等几分钟,您将看到CRM中的FullName格式正确。