Dynamics 365 Check User has Send As Privilege programatically


Business Requirement :- Many a times in our project we get the requirement to check if logged user has Send As privilege allowed in his personal setting programatically. We can use 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!

Comments

Popular posts from this blog

Dynamics 365 Sample Code to set null value for all data types using c#

CRM dynamics Hiding Add Existing Subgrid (+) Button