D365 CRM: Update user setting using C# SDK API



  • Sometimes you need to update a specific configuration of the users, for example the default time zone of the system. By default, these settings are Personal, so every user needs to be notified to change this by hand. But you can do it using an entity in the API called UserSettings. Let’s take this example: you need to update the default time zone of all users in your CRM system. To do that you need to retrieve all the active users first and for each one you need to update his/her time zone:

    1.    ColumnSet myColumnSet = new ColumnSet();
    2.    myColumnSet.AddColumn("fullname");
    3.    myColumnSet.AddColumn("systemuserid");
    4.    myColumnSet.AddColumn("businessunitid");
    5.    
    6.    QueryExpression query = new QueryExpression();
    7.    query.EntityName = "systemuser";
    8.    query.ColumnSet = myColumnSet;
    9.     
    10.    ConditionExpression condition = new ConditionExpression();
    11.    condition.AttributeName = "isdisabled";
    12.    condition.Values.Add(false);
    13.    condition.Operator = ConditionOperator.Equal;
    14.    query.Criteria.AddCondition(condition);
    15.     
    16.    // Get all the active users
    17.    EntityCollection results = Service.RetrieveMultiple(query);
    18.    if (results != null && results.Entities.Count > 0)
    19.    {
    20.       foreach (Entity item in results.Entities)
    21.       {
    22.            // Generate the setting entity
    23.            Entity settings = new Entity("usersettings");
    24.            settings["systemuserid"] = item.Id;
    25.            settings["timezonecode"] = 4; // Pacifit Time (US & Canada) GMT -08:00
    26.            // Generate the request and response of the Update User Setting
    27.            UpdateUserSettingsSystemUserRequest settingsUpdReq = new UpdateUserSettingsSystemUserRequest();
    28.            UpdateUserSettingsSystemUserResponse settingsUpdRsp = new UpdateUserSettingsSystemUserResponse();
    29.            settingsUpdReq.Settings = settings;
    30.            settingsUpdReq.UserId = item.Id;
    31.            settingsUpdRsp = (UpdateUserSettingsSystemUserResponse)Service.Execute(settingsUpdReq);
    32.       }
    33.    }

    The complete reference documentation about this entity:
    https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/entities/usersettings?view=op-9-1 

    Enjoy it!


  • Comments



Add a Comment