Dynamics 365 Check User Has Send As Privilege Programatically

Business Requirement

Many times in our project we get the requirement to check if logged user has to Send As privilege allowed in his personal setting programmatically. We can use the below code snippet to check the logged in user personal settings from plugin code.

public bool CheckUserSendAsPrivilege(IOrganizationService service, Guid userGuid) {
    try {
        if (userGuid != Guid.Empty) {
            QueryExpression queryUserSettings = new QueryExpression(UserSettings.EntityLogicalName);
            queryUserSettings.NoLock = true;
            queryUserSettings.Criteria.AddCondition(UserSettings.systemuseridAttribute, ConditionOperator.Equal, userGuid);
            queryUserSettings.ColumnSet = new ColumnSet(UserSettings.issendasallowedAttribute);
            EntityCollection ecUserSettings = null;
            ecUserSettings = service.RetrieveMultiple(queryUserSettings);
            if (ecUserSettings != null && ecUserSettings.Entities.Count > 0) {
                UserSettings objUserSettings = (UserSettings) ecUserSettings[0];
                if (objUserSettings != null && objUserSettings.Attributes.Contains(UserSettings.issendasallowedAttribute)) {
                    bool isSendAsAllowed = objUserSettings.GetAttributeValue < bool > (UserSettings.issendasallowedAttribute);
                    if (isSendAsAllowed == true) {
                        return true;
                    }
                }
            }
        }
    } catch (Exception ex) {
        throw ex;
    }
    return false;
}

Make sure you pass IOrganizationService of system user/service account context from plugin. Personal settings of all users stored in usersettings entity in Dynamics 365.

Hope you will find this helpful!