Search code examples
c#soapdynamics-crm-2011dynamics-crmdynamics-crm-2013

Dynamics CRM : Retrieve multiple appointments for a systemuser


I'm a trying to get all the appointments for a systemuser with a RetrieveMultiple method and a query expression. Example :

WhoAmIRequest userRequest = new WhoAmIRequest();
WhoAmIResponse userResponse = (WhoAmIResponse)_serviceProxy.Execute(userRequest);

QueryExpression qe = new QueryExpression();
qe.EntityName = "systemuser";

...
slos.RetrieveMultiple(qe);

Can I retrieve all the appointments of a systemuser (owner, organizer, required attendee, optional attendee) from a systemuser entity ?

Or do I have to retrieve all the appointments of the CRM and add conditions to know if the user is owner, organizer, required or optional attendee ?

Finally, I'm using SOAP Logger, is it the best way to generate SOAP request ?


Solution

  • 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:

    // user is Organizer, Owner, required or optional Attendee
    ConditionExpression("participationtypemask", ConditionOperator.In, new int[] { 5, 6, 7, 9 }),