简体   繁体   中英

Joining Appointment Entity's Required Attendees and Contact Entity in Dynamics CRM

I wanted to pull programmatically all the appointment entities and the full information of it's required attendees (which in my case always contact type).

What is the best way to join between Appointment Entity and Contact Entity based on the Appointment Entity "Required Attendees" attribute?

To get the 'Required Attendees', you need to make a join between the Appointment and the ActivityParty entity and then filter based on ParticipationTypeMask which is 5 for 'Required Attendees'. Further, you need to make a join between the ActivityParty and the Contact entity.

Your code could be similar to this:

//Create a query expression specifying the link entity alias 
//and the columns of the link entity that you want to return
QueryExpression qe = new QueryExpression();
qe.EntityName = "appointment";
qe.ColumnSet = new ColumnSet(true);

//LinkEntity for ActivityParty                
var activityParty = new LinkEntity()
{
    EntityAlias = "activityparty",
    JoinOperator = JoinOperator.Inner,
    Columns = new ColumnSet(true),
    LinkFromEntityName = "appointment",
    LinkFromAttributeName = "activityid",
    LinkToEntityName = "activityparty",
    LinkToAttributeName = "activityid",
    LinkCriteria = new FilterExpression
    {
        Conditions =
                   {
                       new ConditionExpression("participationtypemask", ConditionOperator.Equal, 5), //Required Attendees 
                       new ConditionExpression("partyobjecttypecode", ConditionOperator.Equal, 2),   // Contacts
                   }
    }
};

//LinkEntity for Contact                
var contact = new LinkEntity()
{
    EntityAlias = "contact",
    JoinOperator = JoinOperator.Inner,
    Columns = new ColumnSet(true),
    LinkFromEntityName = "activityparty",
    LinkFromAttributeName = "partyid",
    LinkToEntityName = "contact",
    LinkToAttributeName = "contactid",
};

activityParty.LinkEntities.Add(contact);
qe.LinkEntities.Add(activityParty);

//Get the appointments and the Linked Entities
var appointments = _orgService.RetrieveMultiple(qe);

foreach (var appointment in appointments.Entities)
{
    // Do something with the Contact Data 
    var fullname = ((AliasedValue)(appointment["contact.fullname"])).Value.ToString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM