web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
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;
        }
    }
}
I have the same question (0)

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Pallavi Phade Profile Picture

Pallavi Phade 102 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 55

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 55 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans