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 CRM (Archived)

Hide Queues Plugin Error

(0) ShareShare
ReportReport
Posted on by 140
Hi, I am a newbie to CRM and I am trying to get this code to work. This plugin is supposed filter the queues view based on the roles assigned to the user. I am getting an error in the foreach loop for the ConditionExpression. Below is the error message. "Collection was modified; enumeration operation may not execute." ************************************************************** public class HideQueues : IPlugin { public void Execute(IPluginExecutionContext context) { string strErrorMsg = String.Empty; List roles = new List(); try { if ((context.IsExecutingInOfflineMode == true) || (context.Mode != 0) || (context.PrimaryEntityName != EntityName.queue.ToString()) || (context.MessageName != "RetrieveMultiple") || (context.Stage != 10)) { return; } // Check that the InputParameters property bag is not NULL if (context.InputParameters.Properties != null) { foreach (PropertyBagEntry item in context.InputParameters.Properties) { // Check that a QueryExpression object exists in the property bag if (item.Name == "Query") { QueryExpression objQueryExpression = (QueryExpression)item.Value; // Get each ConditionExpression from the QueryExpression foreach (ConditionExpression objConditionExpression in objQueryExpression.Criteria.Conditions) { //Is the ConditionExpression the one we want to replace 'queuetypecode = 1' if (objConditionExpression.AttributeName == "queuetypecode" && objConditionExpression.Operator == ConditionOperator.Equal && objConditionExpression.Values[0].ToString() == "1") { //delete the ConditionExpression objQueryExpression.Criteria.Conditions.Remove(objConditionExpression); roles = CheckUserRoles(context); //Replace it with a new FilterExpression objQueryExpression.Criteria.Filters.Add(NewFilterExpression(context, roles)); } } } } } } private List CheckUserRoles(IPluginExecutionContext context) { ICrmService service = context.CreateCrmService(true); XmlDocument objXmlDoc = new XmlDocument(); List roles = new List(); // Fetch users who are in the business unit of the Sales Engineer and have the system user role AE StringBuilder sb = new StringBuilder(); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); sb.Append(""); string result = service.Fetch(sb.ToString()); objXmlDoc.LoadXml(result); XmlNodeList nl = objXmlDoc.GetElementsByTagName("result"); foreach (XmlNode n in nl) { XmlNode name = n["name"]; XmlNode systemuserid = n["aa.systemuserid"]; if (name != null) { if (name.InnerText == "Team Leader") roles.Add(name.InnerText); if (name.InnerText == "Application Engineers") roles.Add(name.InnerText); } } return roles; } } *********************************************************** After searching for this error I found that the code breaks when it tries to delete the ConditionExpression on which it loops through. I don't know how to fix this code. Any help is greatly appreciated. Thanks!! Raj.

*This post is locked for comments

I have the same question (0)
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello, Raj.

     Your Code is unreadable. Can you format it in some way to make it readable?

  • Raj Chatrathy Profile Picture
    140 on at
    Let me try this: public class HideQueues : IPlugin { public void Execute(IPluginExecutionContext context) { string strErrorMsg = String.Empty; List roles = new List(); try { if ((context.IsExecutingInOfflineMode == true) || (context.Mode != 0) || (context.PrimaryEntityName != EntityName.queue.ToString()) || (context.MessageName != "RetrieveMultiple") || (context.Stage != 10)) { return; } // Check that the InputParameters property bag is not NULL if (context.InputParameters.Properties != null) { foreach (PropertyBagEntry item in context.InputParameters.Properties) { // Check that a QueryExpression object exists in the property bag if (item.Name == "Query") { QueryExpression objQueryExpression = (QueryExpression)item.Value; // Get each ConditionExpression from the QueryExpression foreach (ConditionExpression objConditionExpression in objQueryExpression.Criteria.Conditions) { //Is the ConditionExpression the one we want to replace 'queuetypecode = 1' if (objConditionExpression.AttributeName == "queuetypecode" && objConditionExpression.Operator == ConditionOperator.Equal && objConditionExpression.Values[0].ToString() == "1") { //delete the ConditionExpression objQueryExpression.Criteria.Conditions.Remove(objConditionExpression); roles = CheckUserRoles(context); //Replace it with a new FilterExpression objQueryExpression.Criteria.Filters.Add(NewFilterExpression(context, roles)); } } } } } }
  • Raj Chatrathy Profile Picture
    140 on at
    That Didnt work.....: Please bare with me. Today is DAY 1 for me in this form!!!!! foreach (ConditionExpression objConditionExpression in objQueryExpression.Criteria.Conditions) { //Is the ConditionExpression the one we want to replace 'queuetypecode = 1' if (objConditionExpression.AttributeName == "queuetypecode" && objConditionExpression.Operator == ConditionOperator.Equal && objConditionExpression.Values[0].ToString() == "1") { //delete the ConditionExpression objQueryExpression.Criteria.Conditions.Remove(objConditionExpression); roles = CheckUserRoles(context); //Replace it with a new FilterExpression objQueryExpression.Criteria.Filters.Add(NewFilterExpression(context, roles)); } }
  • Raj Chatrathy Profile Picture
    140 on at
    I am unable to make this code readable. Is there any way I can send you the code (email?).. Thanks for your help.!! Raj.
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

     You can send it using email through my email.

  • Raj Chatrathy Profile Picture
    140 on at

    Unbelievable.... Punishment for using google CHROME instead of IE. Anyways if you look at the bold in code below, the foreach is based on objConditionExpression which is getting modified inside the loop..Thanks!!

       public void Execute(IPluginExecutionContext context)
            {
                string strErrorMsg = String.Empty;
                List<string> roles = new List<string>();

                try
                {
                    if ((context.IsExecutingInOfflineMode == true) || (context.Mode != 0) || (context.PrimaryEntityName != EntityName.queue.ToString()) || (context.MessageName != "RetrieveMultiple") || (context.Stage != 10))
                    {
                        return;
                    }

                    // Check that the InputParameters property bag is not NULL
                    if (context.InputParameters.Properties != null)
                    {
                        foreach (PropertyBagEntry item in context.InputParameters.Properties)
                        {

                            // Check that a QueryExpression object exists in the property bag
                            if (item.Name == "Query")
                            {
                                QueryExpression objQueryExpression = (QueryExpression)item.Value;

                                // Get each ConditionExpression from the QueryExpression
                                foreach (ConditionExpression objConditionExpression in objQueryExpression.Criteria.Conditions)
                                {
                                    //Is the ConditionExpression the one we want to replace 'queuetypecode = 1'
                                    if (objConditionExpression.AttributeName == "queuetypecode" && objConditionExpression.Operator == ConditionOperator.Equal && objConditionExpression.Values[0].ToString() == "1")
                                    {
                                        //delete the ConditionExpression
                                        objQueryExpression.Criteria.Conditions.Remove(objConditionExpression);

                                        roles = CheckUserRoles(context);

                                        //Replace it with a new FilterExpression
                                        objQueryExpression.Criteria.Filters.Add(NewFilterExpression(context, roles));
                                    }
                                }
                            }
                        }
                    }
                }
                catch (System.Web.Services.Protocols.SoapException ex)
                {
                    strErrorMsg = "Platform error occurred in HideQueues.Execute. Error: " + ex.Message + " " + ex.Detail.OuterXml + " Source: " + ex.Source;
                    CRMHelper.WriteToLog(strErrorMsg, 2);
                }
                catch (System.Exception ex)
                {
                    strErrorMsg = "General error occurred in HideQueues.Execute. Error: " + ex.Message + " Source: " + ex.Source;
                    CRMHelper.WriteToLog(strErrorMsg, 2);
                }
            }

     

     private List<string> CheckUserRoles(IPluginExecutionContext context)
            {
                ICrmService service = context.CreateCrmService(true);
                XmlDocument objXmlDoc = new XmlDocument();
                List<string> roles = new List<string>();

                // Fetch users who are in the business unit of the Sales Engineer and have the system user role AE
                StringBuilder sb = new StringBuilder();
                sb.Append("<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>");
                sb.Append("<entity name='role'>");
                sb.Append("<attribute name='name'/>");
                sb.Append("<attribute name='businessunitid'/>");
                sb.Append("<attribute name='roleid'/>");
                sb.Append("<order attribute='name' descending='false'/>");
                sb.Append("<link-entity name='systemuserroles' from='roleid' to='roleid' visible='false' intersect='true'>");
                sb.Append("<link-entity name='systemuser' from='systemuserid' to='systemuserid' alias='aa'>");
                sb.Append("<attribute name='systemuserid'/>");
                sb.Append("<filter type='and'>");
                sb.Append("<condition attribute='systemuserid' operator='eq' value='" + context.InitiatingUserId.ToString() + "'/>");
                sb.Append("</filter>");
                sb.Append("</link-entity>");   
                sb.Append("</link-entity>");
                sb.Append("</entity>");
                sb.Append("</fetch>");

                string result = service.Fetch(sb.ToString());
                objXmlDoc.LoadXml(result);

                XmlNodeList nl = objXmlDoc.GetElementsByTagName("result");
                foreach (XmlNode n in nl)
                {
                    XmlNode name = n["name"];
                    XmlNode systemuserid = n["aa.systemuserid"];
                    if (name != null)
                    {
                        if (name.InnerText == "Team Leader")
                            roles.Add(name.InnerText);
                        if (name.InnerText == "Application Engineers")
                            roles.Add(name.InnerText);
                    }
                }

                return roles;
            }
        }

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

     Try to use instead code

    foreach (ConditionExpression objConditionExpression in objQueryExpression.Criteria.Conditions)
    {
        if (objConditionExpression.AttributeName == "queuetypecode" && objConditionExpression.Operator == ConditionOperator.Equal && objConditionExpression.Values[0].ToString() == "1")
        {
            objQueryExpression.Criteria.Conditions.Remove(objConditionExpression);
            roles = CheckUserRoles(context);
            objQueryExpression.Criteria.Filters.Add(NewFilterExpression(context, roles));
        }
    }

     

    following

     List<ConditionExpression> conditions = new List<ConditionExpression>();

    foreach (ConditionExpression objConditionExpression in objQueryExpression.Criteria.Conditions)
    {
        if (objConditionExpression.AttributeName == "queuetypecode" && objConditionExpression.Operator == ConditionOperator.Equal && objConditionExpression.Values[0].ToString() == "1")
        {
            conditions.Add(objConditionExpression);
        }
    }

    foreach(ConditionExpression objConditionExpression in conditions)
    {
            objQueryExpression.Criteria.Conditions.Remove(objConditionExpression);
    }

     

  • Raj Chatrathy Profile Picture
    140 on at
    Thank you so much Andriy that fixed my problem... Have a great day!! Raj.

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 CRM (Archived)

#1
Varsha deshpande Profile Picture

Varsha deshpande 5

#2
JS-09031509-0 Profile Picture

JS-09031509-0 3

#3
Ciprian  P Profile Picture

Ciprian P 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans