Friday, 17 May 2013

MS Dynamics CRM 2011 Workflows not sending emails - Set Allow other Microsoft Dynamics CRM users to send E-mail on your behalf privilege programmatically

Today I had an issue where a workflow would fail to complete, oddly it was failing on a Send Email Activity, with the following error message:
You cannot send e-mail as the selected user. The selected user has not allowed this or you do not have sufficient privileges to do so.Contact your system administrator for assistance.
The issue is caused by users not having send as privileges set. Note that this will only occur if the sender is different from the owner of the email, which will be the case if you have workflows owned by the service account sending emails on user's behalf, a common enough business scenario.

Here's the method I used to allow this:

private void SetSendAsSetting(IOrganizationService service, Entity user, bool state)
        UpdateUserSettingsSystemUserRequest updateRequest = new UpdateUserSettingsSystemUserRequest();
        UpdateUserSettingsSystemUserResponse updateResponse = new UpdateUserSettingsSystemUserResponse();
        Entity userSettings = new Entity("usersettings");        
        //Ensure that help language is set
        userSettings.Attributes["helplanguageid"] = 1033;
        userSettings.Attributes["issendasallowed"] = state;
        updateRequest.Settings = userSettings;
        updateRequest.UserId = userId;
        updateResponse = (UpdateUserSettingsSystemUserResponse)service.Execute(updateRequest);
    catch (Exception ex)

1 comment: