
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; } }}