Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

Show/Hide Views based on User Security Role

(0) ShareShare
ReportReport
Posted on by 50

Hello All, did anyone had success using this plugin?

I found it on this site https://crm365blog.wordpress.com/2018/07/15/show-hide-views-based-on-user-security-role/

I followed all the steps that's required for the plugin. 

using System;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
 
namespace FilterViewsByRole
{
    public class FilterViews : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            //IOrganizationService crmService = serviceFactory.CreateOrganizationService(context.UserId);
            IOrganizationService crmService = serviceFactory.CreateOrganizationService(null);
 
            try
            {
                if (context.InputParameters.Contains("Query") && context.InputParameters["Query"] is QueryExpression)
                {
                    QueryExpression qe = (QueryExpression)context.InputParameters["Query"];
                    if (qe.EntityName == "savedquery")
                    {
                        if (qe.Criteria != null)
                        {
                            if (qe.Criteria.Conditions != null)
                            {
                                string roleName = "System Administrator";
                                bool userRoleFound = VerifyIfUserHasRole(roleName, context.UserId, crmService);
                                if(userRoleFound)
                                {
                                    ConditionExpression queryCondition = new ConditionExpression("name", ConditionOperator.NotEqual, "Authorized Contact");
                                    qe.Criteria.Conditions.Add(queryCondition);
                                }
                            }
                        }
                    }
                }
            }
            catch (InvalidPluginExecutionException ex)
            {
                throw ex;
            }
        }
 
        public bool VerifyIfUserHasRole(string roleName, Guid userId, IOrganizationService crmService)
        {
            bool userRoleFound = false;
            string fetchXmlString = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
                  <entity name='role'>
                    <attribute name='name' />
                    <attribute name='roleid' />
                    <link-entity name='systemuserroles' from='roleid' to='roleid' visible='false' intersect='true'>
                      <link-entity name='systemuser' from='systemuserid' to='systemuserid' alias='ab'>
                        <filter type='and'>
                            <condition attribute='systemuserid' operator='eq' value='" + userId + @"' />
                        </filter>
                      </link-entity>
                    </link-entity>
                  </entity>
                </fetch>";
            EntityCollection entColl = crmService.RetrieveMultiple(new FetchExpression(fetchXmlString));
            if (entColl != null && entColl.Entities != null && entColl.Entities.Count > 0)
            {
                int count = entColl.Entities.Select(x => x.GetAttributeValue<string>("name")).Where(y => y.ToString().ToUpper().Trim() == roleName.ToUpper()).Count();
                userRoleFound = count > 0;
            }
            return userRoleFound;
        }
    }
}

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans