You'll need to use the relationship entity activitypointer
between appointment
and systemuser
. As you'll see in my examples, this makes things a little complicated.
There are at least 2 possible ways to build your desired query:
1) As you already figured, you can filter appointments by systemuserid:
var qe = new QueryExpression
{
EntityName = "appointment",
ColumnSet = new ColumnSet("subject"),
LinkEntities =
{
new LinkEntity
{
EntityAlias = "ap",
JoinOperator = JoinOperator.Inner,
Columns = new ColumnSet(false),
LinkFromEntityName = "appointment",
LinkFromAttributeName = "activityid",
LinkToEntityName = "activityparty",
LinkToAttributeName = "activityid",
LinkCriteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("partyid", ConditionOperator.Equal, userid),
},
},
},
},
};
2) You can query the systemuser by systemuserid and add the appointments as a linked entity (like a JOIN in a sql query):
var qe2 = new QueryExpression
{
EntityName = "systemuser",
ColumnSet = new ColumnSet(false),
LinkEntities =
{
new LinkEntity
{
EntityAlias = "ap",
Columns = new ColumnSet(false),
JoinOperator = JoinOperator.Inner,
LinkFromEntityName = "systemuser",
LinkFromAttributeName = "systemuserid",
LinkToEntityName = "activityparty",
LinkToAttributeName = "partyid",
LinkEntities =
{
new LinkEntity
{
EntityAlias = "a",
Columns = new ColumnSet("subject"),
JoinOperator = JoinOperator.Inner,
LinkFromEntityName = "activityparty",
LinkFromAttributeName = "activityid",
LinkToEntityName = "appointment",
LinkToAttributeName = "activityid",
},
},
},
},
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("systemuserid", ConditionOperator.Equal, userid),
},
},
};
Concerning the filter for the participation role, you'll have to add a condition on the participationtypemask
on the activitypointer
:
ConditionExpression("participationtypemask", ConditionOperator.In, new int[] { 5, 6, 7, 9 }),