Monday, July 1, 2013

Update User Workplace Areas Programmatically

This will probably fall into the category of something you'll never need to do, especially when the personalization of the workplace is set to be discontinued with "Orion".

Removal of Workplace and Personalization Options for New Customers

I saw someone had posted a question in one of the forums on how to do this so I thought I would take a stab at it.

The settings in question can be found under: File -> Options -> Workplace tab, it allows to to customize which entity groupings are displayed in the left navigation while looking at the Workplace view.
The options are a bit different based on the platform*, Online users get the Sales and Service areas by default and can additionally add the Marketing and Tools areas. On Premise users have options for Sales, Service, Marketing and Scheduling.
*UR13 On Premise and UR12 Online

The code snippet below updates the UserProfile field UserSettingsBase table. The selected areas are stored as a single string based on the selected areas. Unfortunately trying to turn on or off an area that isn't available based on Online/On Premise won't work. You can try but the setting appears to be ignored.

void UpdateUserWorkplace(IOrganizationService service, Guid userId, 
    bool sfa, bool cs, bool ma, bool sm, bool tools)
{
    RetrieveUserSettingsSystemUserRequest request = 
        new RetrieveUserSettingsSystemUserRequest
    {
        EntityId = userId,
        ColumnSet = new ColumnSet("userprofile")
    };
    RetrieveUserSettingsSystemUserResponse response =
        (RetrieveUserSettingsSystemUserResponse)service.Execute(request);
    UserSettings setting = (UserSettings)response.Entity;
    setting.UserProfile = null;

    if (sfa)
        setting.UserProfile += "SFA%2c";
    if (cs)
        setting.UserProfile += "CS%2c";
    if (ma)
        setting.UserProfile += "MA%2c";
    if (sm)
        setting.UserProfile += "SM%2c";
    if (tools)
        setting.UserProfile += "Tools%2c";

    service.Update(setting);
}