Guest User

Untitled

a guest
Aug 25th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.47 KB | None | 0 0
  1. namespace Great.MMS.RetrieveMultiple {
  2.  
  3.   public class TeamBasedViews: IPlugin {
  4.     /// <summary>
  5.     /// Triggers on Retrieve Multiple of Saved Query
  6.     /// </summary>
  7.     private readonly XmlDocument doc = new XmlDocument();
  8.     /// <summary>
  9.     /// Reading Unsecured configuration
  10.     /// </summary>
  11.     /// <param name="unsecuredString"></param>
  12.     /// <param name="securedString"></param>
  13.     /// <param name="service"></param>
  14.     public TeamBasedViews(string unsecuredString, string securedString) {
  15.       try {
  16.         if (String.IsNullOrWhiteSpace(unsecuredString) || String.IsNullOrWhiteSpace(securedString)) {
  17.           doc.LoadXml(unsecuredString);
  18.         }
  19.       } catch (Exception e) {
  20.         throw e;
  21.       }
  22.     }
  23.  
  24.     public void Execute(IServiceProvider serviceProvider) {
  25.       try {
  26.         #region Service declaration
  27.         ITracingService tracingService = (ITracingService) serviceProvider.GetService(typeof (ITracingService));
  28.         if (tracingService == null)
  29.           throw new InvalidPluginExecutionException("Tracing service not created");
  30.         IExecutionContext context = (IExecutionContext) serviceProvider.GetService(typeof (IExecutionContext));
  31.         tracingService.Trace("1");
  32.         tracingService.Trace("Message Name:{0}", context.MessageName);
  33.         IOrganizationServiceFactory factory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof (IOrganizationServiceFactory));
  34.         IOrganizationService organizationService = factory.CreateOrganizationService(context.UserId);
  35.  
  36.         #endregion
  37.  
  38.         if (context.Depth == 1 && context.Mode == 0 && context.MessageName == "RetrieveMultiple") {
  39.           if (context.InputParameters.Contains("Query") && context.InputParameters["Query"] is FetchExpression) {
  40.             tracingService.Trace("Context contains Query Parameter");
  41.  
  42.             /// Begin edits
  43.  
  44.             var fetchExpression = context.InputParameters["Query"] as FetchExpression;
  45.             var fetchXmlToQueryExpressionRequest = new FetchXmlToQueryExpressionRequest() {
  46.               FetchXml = fetchExpression.Query
  47.             };
  48.             var fetchXmlToQueryExpressionResponse = (FetchXmlToQueryExpressionResponse) organizationService.Execute(fetchXmlToQueryExpressionRequest);
  49.  
  50.             QueryExpression objQueryExpression = fetchXmlToQueryExpressionResponse.Query; // get the Query Expression in Saved Query
  51.  
  52.             /// End edits
  53.  
  54.             ///QueryExpression objQueryExpression = (QueryExpression)context.InputParameters["Query"]; // get the Query Expression in Saved Query
  55.  
  56.             if (objQueryExpression.EntityName == "savedquery") {
  57.               tracingService.Trace("Criteria on SavedQuery");
  58.               if (objQueryExpression.Criteria != null && objQueryExpression.Criteria.Conditions != null) {
  59.                 FilterExpression fe1 = new FilterExpression(LogicalOperator.And);
  60.                 List < ConditionExpression > ces = objQueryExpression.Criteria.Conditions.ToList();
  61.                 ConditionExpression ce = ces.FirstOrDefault(x => x.AttributeName == "returnedtypecode"); // get the ConditionExpression of returnedtypecode
  62.                 var viewlist = new List < string > ();
  63.                 var notviewlist = new List < string > ();
  64.                 if (null != ce) {
  65.                   if (ce.Values.Count > 0) {
  66.                     string etc = ce.Values[0].ToString();
  67.                     tracingService.Trace("returnedtypecode: " + etc);
  68.                     XmlNodeList entityNodes = doc.SelectNodes("/ Entities / Entity[@value =" + etc + "]"); // get the Entity node of returnedtypecode
  69.                     if (entityNodes.Count > 0) {
  70.                       foreach(XmlNode nodeEntity in entityNodes) {
  71.                         XmlNodeList teamNodes = nodeEntity.SelectNodes("Team"); // get the Team nodes in nodeEntity
  72.                         foreach(XmlNode team in teamNodes) {
  73.                           if (team.Attributes["name"].Value != null && team.Attributes["value"].Value != null) {
  74.                             // var teamname = team.Attributes["name"].Value;
  75.                             var teamid = new Guid(team.Attributes["value"].Value);
  76.                             if (IsTeamMember(teamid, context.InitiatingUserId, organizationService)) // Check whether logged in User is part of Team or not
  77.                             {
  78.                               if (team.ChildNodes.Count > 0) {
  79.                                 foreach(XmlNode viewnodes in team.ChildNodes) {
  80.                                   if (viewnodes.Attributes["name"].Value != null && viewnodes.Attributes["value"].Value != null) {
  81.                                     // var viewname = viewnodes.Attributes.Item(0).Value;
  82.                                     var viewid = new Guid(viewnodes.Attributes["value"].Value);
  83.                                     XmlElement xview = viewnodes as XmlElement;
  84.                                     if (xview.HasAttribute("allow")) // check whether view node has allow atribute
  85.                                     {
  86.                                       if (viewnodes.Attributes["allow"] != null && viewnodes.Attributes["allow"].Value != null) {
  87.                                         string allowvalue = viewnodes.Attributes["allow"].Value;
  88.                                         if (allowvalue == "0") // add to not viewable list
  89.                                         {
  90.                                           if (!notviewlist.Contains(viewid.ToString()))
  91.                                             notviewlist.Add(viewid.ToString());
  92.                                           if (viewlist.Contains(viewid.ToString()))
  93.                                             viewlist.Remove(viewid.ToString());
  94.                                         } else if (allowvalue == "1") // add to vieable list
  95.                                         {
  96.                                           if (!notviewlist.Contains(viewid.ToString())) {
  97.                                             if (!viewlist.Contains(viewid.ToString()))
  98.                                               viewlist.Add(viewid.ToString());
  99.                                           }
  100.                                         }
  101.                                       }
  102.                                     } else {
  103.                                       //// cview node doesn’t have allow atribute
  104.                                       if (!viewlist.Contains(viewid.ToString()))
  105.                                         viewlist.Add(viewid.ToString());
  106.                                     }
  107.                                   }
  108.                                 }
  109.                               }
  110.                             }
  111.                           }
  112.                         }
  113.                         if (viewlist.Count == 0) // if Viewable list count is 0
  114.                         {
  115.                           XmlNodeList defaultview = nodeEntity.SelectNodes("Defaultview"); // get the default views
  116.                           if (defaultview.Count > 0) {
  117.                             foreach(XmlNode defaultviewid in defaultview) {
  118.                               if (defaultviewid.Attributes["name"].Value != null && defaultviewid.Attributes["value"].Value != null) {
  119.                                 var viewid = new Guid(defaultviewid.Attributes["value"].Value);
  120.                                 if (!viewlist.Contains(viewid.ToString()))
  121.                                   viewlist.Add(viewid.ToString());
  122.  
  123.                               }
  124.                             }
  125.                           }
  126.  
  127.                         }
  128.                       }
  129.                     }
  130.                   }
  131.                 }
  132.                 if (viewlist.Count > 0) // if viewable countis greater than 0, then add condition
  133.                 {
  134.                   ConditionExpression queryCondition2 = new ConditionExpression("savedqueryid", ConditionOperator.In, viewlist);
  135.                   fe1.Conditions.Add(queryCondition2);
  136.                 }
  137.                 if (notviewlist.Count > 0) // if non-viewable countis greater than 0, then add condition
  138.                 {
  139.                   ConditionExpression queryCondition2 = new ConditionExpression("savedqueryid2", ConditionOperator.NotIn, notviewlist);
  140.                   fe1.Conditions.Add(queryCondition2);
  141.                 }
  142.                 objQueryExpression.Criteria.AddFilter(fe1);
  143.                 tracingService.Trace("Filtered Successfully");
  144.               }
  145.             }
  146.           }
  147.         }
  148.       } catch (Exception updateExec) {
  149.         throw updateExec;
  150.       }
  151.     }
  152.  
  153.     /// <summary>
  154.     /// IsTeamMember is used to check whether the user is part of Team
  155.     /// </summary>
  156.     /// <param name="teamID"></param>
  157.     /// <param name="userID"></param>
  158.     /// <param name="service"></param>
  159.     /// <returns></returns>
  160.     public static bool IsTeamMember(Guid teamID, Guid userID, IOrganizationService service) {
  161.       QueryExpression query = new QueryExpression("team");
  162.       query.ColumnSet = new ColumnSet("teamid");
  163.       query.Criteria.AddCondition(new ConditionExpression("teamid", ConditionOperator.Equal, teamID));
  164.       LinkEntity link = query.AddLink("teammembership", "teamid", "teamid");
  165.       link.LinkCriteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, userID));
  166.       var results = service.RetrieveMultiple(query);
  167.       if (results.Entities.Count > 0) {
  168.         return true;
  169.       } else {
  170.         return false;
  171.       }
  172.     }
  173.   }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment