Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Great.MMS.RetrieveMultiple {
- public class TeamBasedViews: IPlugin {
- /// <summary>
- /// Triggers on Retrieve Multiple of Saved Query
- /// </summary>
- private readonly XmlDocument doc = new XmlDocument();
- /// <summary>
- /// Reading Unsecured configuration
- /// </summary>
- /// <param name="unsecuredString"></param>
- /// <param name="securedString"></param>
- /// <param name="service"></param>
- public TeamBasedViews(string unsecuredString, string securedString) {
- try {
- if (String.IsNullOrWhiteSpace(unsecuredString) || String.IsNullOrWhiteSpace(securedString)) {
- doc.LoadXml(unsecuredString);
- }
- } catch (Exception e) {
- throw e;
- }
- }
- public void Execute(IServiceProvider serviceProvider) {
- try {
- #region Service declaration
- ITracingService tracingService = (ITracingService) serviceProvider.GetService(typeof (ITracingService));
- if (tracingService == null)
- throw new InvalidPluginExecutionException("Tracing service not created");
- IExecutionContext context = (IExecutionContext) serviceProvider.GetService(typeof (IExecutionContext));
- tracingService.Trace("1");
- tracingService.Trace("Message Name:{0}", context.MessageName);
- IOrganizationServiceFactory factory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof (IOrganizationServiceFactory));
- IOrganizationService organizationService = factory.CreateOrganizationService(context.UserId);
- #endregion
- if (context.Depth == 1 && context.Mode == 0 && context.MessageName == "RetrieveMultiple") {
- if (context.InputParameters.Contains("Query") && context.InputParameters["Query"] is FetchExpression) {
- tracingService.Trace("Context contains Query Parameter");
- /// Begin edits
- var fetchExpression = context.InputParameters["Query"] as FetchExpression;
- var fetchXmlToQueryExpressionRequest = new FetchXmlToQueryExpressionRequest() {
- FetchXml = fetchExpression.Query
- };
- var fetchXmlToQueryExpressionResponse = (FetchXmlToQueryExpressionResponse) organizationService.Execute(fetchXmlToQueryExpressionRequest);
- QueryExpression objQueryExpression = fetchXmlToQueryExpressionResponse.Query; // get the Query Expression in Saved Query
- /// End edits
- ///QueryExpression objQueryExpression = (QueryExpression)context.InputParameters["Query"]; // get the Query Expression in Saved Query
- if (objQueryExpression.EntityName == "savedquery") {
- tracingService.Trace("Criteria on SavedQuery");
- if (objQueryExpression.Criteria != null && objQueryExpression.Criteria.Conditions != null) {
- FilterExpression fe1 = new FilterExpression(LogicalOperator.And);
- List < ConditionExpression > ces = objQueryExpression.Criteria.Conditions.ToList();
- ConditionExpression ce = ces.FirstOrDefault(x => x.AttributeName == "returnedtypecode"); // get the ConditionExpression of returnedtypecode
- var viewlist = new List < string > ();
- var notviewlist = new List < string > ();
- if (null != ce) {
- if (ce.Values.Count > 0) {
- string etc = ce.Values[0].ToString();
- tracingService.Trace("returnedtypecode: " + etc);
- XmlNodeList entityNodes = doc.SelectNodes("/ Entities / Entity[@value =" + etc + "]"); // get the Entity node of returnedtypecode
- if (entityNodes.Count > 0) {
- foreach(XmlNode nodeEntity in entityNodes) {
- XmlNodeList teamNodes = nodeEntity.SelectNodes("Team"); // get the Team nodes in nodeEntity
- foreach(XmlNode team in teamNodes) {
- if (team.Attributes["name"].Value != null && team.Attributes["value"].Value != null) {
- // var teamname = team.Attributes["name"].Value;
- var teamid = new Guid(team.Attributes["value"].Value);
- if (IsTeamMember(teamid, context.InitiatingUserId, organizationService)) // Check whether logged in User is part of Team or not
- {
- if (team.ChildNodes.Count > 0) {
- foreach(XmlNode viewnodes in team.ChildNodes) {
- if (viewnodes.Attributes["name"].Value != null && viewnodes.Attributes["value"].Value != null) {
- // var viewname = viewnodes.Attributes.Item(0).Value;
- var viewid = new Guid(viewnodes.Attributes["value"].Value);
- XmlElement xview = viewnodes as XmlElement;
- if (xview.HasAttribute("allow")) // check whether view node has allow atribute
- {
- if (viewnodes.Attributes["allow"] != null && viewnodes.Attributes["allow"].Value != null) {
- string allowvalue = viewnodes.Attributes["allow"].Value;
- if (allowvalue == "0") // add to not viewable list
- {
- if (!notviewlist.Contains(viewid.ToString()))
- notviewlist.Add(viewid.ToString());
- if (viewlist.Contains(viewid.ToString()))
- viewlist.Remove(viewid.ToString());
- } else if (allowvalue == "1") // add to vieable list
- {
- if (!notviewlist.Contains(viewid.ToString())) {
- if (!viewlist.Contains(viewid.ToString()))
- viewlist.Add(viewid.ToString());
- }
- }
- }
- } else {
- //// cview node doesn’t have allow atribute
- if (!viewlist.Contains(viewid.ToString()))
- viewlist.Add(viewid.ToString());
- }
- }
- }
- }
- }
- }
- }
- if (viewlist.Count == 0) // if Viewable list count is 0
- {
- XmlNodeList defaultview = nodeEntity.SelectNodes("Defaultview"); // get the default views
- if (defaultview.Count > 0) {
- foreach(XmlNode defaultviewid in defaultview) {
- if (defaultviewid.Attributes["name"].Value != null && defaultviewid.Attributes["value"].Value != null) {
- var viewid = new Guid(defaultviewid.Attributes["value"].Value);
- if (!viewlist.Contains(viewid.ToString()))
- viewlist.Add(viewid.ToString());
- }
- }
- }
- }
- }
- }
- }
- }
- if (viewlist.Count > 0) // if viewable countis greater than 0, then add condition
- {
- ConditionExpression queryCondition2 = new ConditionExpression("savedqueryid", ConditionOperator.In, viewlist);
- fe1.Conditions.Add(queryCondition2);
- }
- if (notviewlist.Count > 0) // if non-viewable countis greater than 0, then add condition
- {
- ConditionExpression queryCondition2 = new ConditionExpression("savedqueryid2", ConditionOperator.NotIn, notviewlist);
- fe1.Conditions.Add(queryCondition2);
- }
- objQueryExpression.Criteria.AddFilter(fe1);
- tracingService.Trace("Filtered Successfully");
- }
- }
- }
- }
- } catch (Exception updateExec) {
- throw updateExec;
- }
- }
- /// <summary>
- /// IsTeamMember is used to check whether the user is part of Team
- /// </summary>
- /// <param name="teamID"></param>
- /// <param name="userID"></param>
- /// <param name="service"></param>
- /// <returns></returns>
- public static bool IsTeamMember(Guid teamID, Guid userID, IOrganizationService service) {
- QueryExpression query = new QueryExpression("team");
- query.ColumnSet = new ColumnSet("teamid");
- query.Criteria.AddCondition(new ConditionExpression("teamid", ConditionOperator.Equal, teamID));
- LinkEntity link = query.AddLink("teammembership", "teamid", "teamid");
- link.LinkCriteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, userID));
- var results = service.RetrieveMultiple(query);
- if (results.Entities.Count > 0) {
- return true;
- } else {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment