Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Plugin: QueryExpression to Grab Users to Populate in Email

Posted on by 180

I am trying to use a QueryExpression to search for all users with a particular title to then populate into an email.

I know how to populate an activity party into an email, but I am having difficulty grabbing multiple users (maybe use a list?) and then getting them into the activity party.

Can you please help?

QueryExpression query = new QueryExpression
{      
     EntityName = "systemuser",
     ColumnSet = new ColumnSet("systemuserid")
};

     query.Criteria.AddCondition("title", ConditionOperator.Equal, "Developer");

     EntityCollection Developerusers = service.RetrieveMultiple(query);
                               
     foreach (Entity users in Developerusers.Entities)
     {
           //Code Here to grab all users
           //email.Attributes.Add("to", DeveloperParty);
     }

*This post is locked for comments

  • Mike Karls Profile Picture
    Mike Karls 180 on at
    RE: Plugin: QueryExpression to Grab Users to Populate in Email

    Gopalan you are the best! I wouldn't have been able to do it without you! Thank you again!!!!!!

  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Plugin: QueryExpression to Grab Users to Populate in Email

    Please modify the code as below:




    EntityCollection ccUserParty = new EntityCollection(); ccUserParty.EntityName = "systemuser"; foreach (Entity users in entityCollection) { Entity ccParty = new Entity("activityparty"); EntityReference ccUser = new EntityReference("systemuser", users.Id); ccParty.Attributes.Add("partyid", ccUser); ccUserParty.Entities.Add(ccParty); } if (ccUserParty.Entities.Count > 0) { email.Attributes.Add("cc", ccUserParty); }
  • Mike Karls Profile Picture
    Mike Karls 180 on at
    RE: Plugin: QueryExpression to Grab Users to Populate in Email

    I am almost there!!! Thank you all for your help so far!

    The code below works for when the result is one user, when it returns more than one user I get the error "item with the same key has already been added". The last line "email.Attributes.Add("cc", ccUserParty); needs some way to handle more than one ccUserParty being passed.





    Entity email = new Entity(); email.LogicalName = "email"; QueryExpression query = new QueryExpression() { Distinct = false, EntityName = "systemuser", ColumnSet = new ColumnSet("fullname", "systemuserid", "firstname"), Criteria = { Filters = { new FilterExpression { FilterOperator = LogicalOperator.And, Conditions = { new ConditionExpression("title", ConditionOperator.Equal, "Developer"), new ConditionExpression("isdisabled", ConditionOperator.Equal, "0") }, }, } } }; DataCollection<Entity> entityCollection = service.RetrieveMultiple(query).Entities; EntityCollection tocc = new EntityCollection(); //Works for only one user, if there is more than 2 it errors out with "item with same key has already been added foreach (Entity users in entityCollection) { Entity ccParty = new Entity("activityparty"); EntityReference ccUser = new EntityReference("systemuser", users.Id); ccParty.Attributes.Add("partyid", ccUser); EntityCollection ccUserParty = new EntityCollection(); ccUserParty.EntityName = "systemuser"; ccUserParty.Entities.Add(ccParty); //this is where it errors email.Attributes.Add("cc", ccUserParty); }


     

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin: QueryExpression to Grab Users to Populate in Email

    Change ColumnSet = new ColumnSet("systemuserid") to ColumnSet = new ColumnSet(false).

    and to get the system user's id, you can use users.Id inside foreach loop

  • Suggested answer
    Rajat Awasthi Profile Picture
    Rajat Awasthi 675 on at
    RE: Plugin: QueryExpression to Grab Users to Populate in Email

    Hi Mike ,

    After you get Developerusers . Write below code in your foreach.

    QueryExpression query = new QueryExpression

    {      

        EntityName = "systemuser",

        ColumnSet = new ColumnSet("systemuserid")

    };

        query.Criteria.AddCondition("title", ConditionOperator.Equal, "Developer");

        EntityCollection Developerusers = service.RetrieveMultiple(query);

        EntityCollection tocc = new EntityCollection();

    foreach (Entity users in Developerusers.Entities)

        {

    Entity ccParty = new Entity("activityparty");

                           ccParty.Attributes["partyid"] = new EntityReference(systemuser, systemuserid);

                           tocc.Entities.Add(ccParty);

    }

    Please mark as verified if this helps you.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Plugin: QueryExpression to Grab Users to Populate in Email

    Hiii, 

    Try this to populate users in from queryexpression. The output will be a List<Guid>. And use the List<Guid> to set your CC.

    List<Guid> list = new List<Guid>();
    
    foreach(Entity users in Developerusers.Entities)
    {
    
    
    Guid userId = (Guid)users[0].Attributes["systemuserid"];
    
    list.add(userId);
     
                
    }
    
    
     

  • Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Plugin: QueryExpression to Grab Users to Populate in Email

    Please check how many records in the DeveloperUser collection.

  • Mike Karls Profile Picture
    Mike Karls 180 on at
    RE: Plugin: QueryExpression to Grab Users to Populate in Email

    HI Gopalan, thank you for assisting.

    I have the "to" sending to my email address and the "cc" to the Developerusers EntityCollection

    The email sends but there is no one listed on the "cc".  There are 3 users that have the title "Developer" within my CRM.

    I believe there might be an issue with the EntityCollection or EntityReference. How do I get the code to add All users from the QueryExpression?

    
    
    //Get List of Developers                                                                                             
    QueryExpression query = new QueryExpression
    {
        EntityName = "systemuser",
        ColumnSet = new ColumnSet("systemuserid")
    };
    query.Criteria.AddCondition("title", ConditionOperator.Equal, "Developer");
    
    EntityCollection Developerusers = service.RetrieveMultiple(query);
    
    Developerusers.EntityName = "systemuser";
    
    
    //foreach (Entity users in Developerusers.Entities)
    //{
        //Code Here to grab all users                                  
        
        Entity cc1 = new Entity("activityparty");
        
        cc1["partyid"] = new EntityReference("systemuser", Developerusers.Entities.Last().Id);
        
        email.Attributes.Add("cc", cc1);
    //}



  • Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Plugin: QueryExpression to Grab Users to Populate in Email

    Hi

    Please change the code to add cc as below (and do the same for to as well)

    Entity cc1 = new Entity("activityparty");
    cc1["partyid"] = new EntityReference("systemuser", Developerusers.Entities.Last().Id);
    email["cc"] = new Entity[] { cc1 };


  • Mike Karls Profile Picture
    Mike Karls 180 on at
    RE: Plugin: QueryExpression to Grab Users to Populate in Email

    Guido, thank you for the response! I believe I am close but I am receiving an error on my EntityReference. Can you please help close the loop! Thank you

                                   QueryExpression query = new QueryExpression

                                   {

                                       EntityName = "systemuser",

                                       ColumnSet = new ColumnSet("systemuserid")

                                   };

                                   query.Criteria.AddCondition("title", ConditionOperator.Equal, "Developer");

                                   error = "entity collection";

                                   EntityCollection Developerusers = service.RetrieveMultiple(query);

                                   foreach (Entity users in Developerusers.Entities)

                                   {

                                       //Code Here to grab all users

                                       //email.Attributes.Add("to", DeveloperParty);

                                       error = "EntityReference";

                                       EntityReference AEparty = (EntityReference)Developerusers.Entities.Last()["partyid"];

                                       email.Attributes.Add("cc", AEparty);

                                   }

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans