Echilon
Echilon

Reputation: 10264

Listing Activities via Web Services

I'm trying to reproduce the Activities page in Microsoft CRM 4.0 via web services. I can retrieve a list of activities, and I believe I need to use ActivityPointers to retrieve the entities but have so far been unsuccessful. Would I need to loop through every single entity returned from the first query to retrieve the ActivityPointer for it? And if so, how would I then get the "Regarding" field or Subject of the activity (eg: email).

The code to retrieve the activities is:

    var svc = GetCrmService();
    var cols = new ColumnSet();
    cols.Attributes = new[] { "activityid", "addressused", "scheduledstart", "scheduledend", "partyid", "activitypartyid", "participationtypemask", "ownerid" };
    var query = new QueryExpression();
    query.EntityName = EntityName.activityparty.ToString();
    query.ColumnSet = cols;

    LinkEntity link = new LinkEntity();
    //link.LinkCriteria = filter;
    link.LinkFromEntityName = EntityName.activitypointer.ToString();
    link.LinkFromAttributeName = "activityid";
    link.LinkToEntityName = EntityName.activityparty.ToString();
    link.LinkToAttributeName = "activityid";
    query.LinkEntities = new[] {link};

    var activities = svc.RetrieveMultiple(query);
    var entities = new List<ICWebServices.activityparty>();
    RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse) svc.Execute(request);
    //var pointers = new List<activitypointer>();
    foreach (activityparty c in activities.BusinessEntities)
    {
        entities.Add(((activityparty)c));
       //the entities don't seem to contain a link to the email which they came from
    }

Upvotes: 0

Views: 1046

Answers (1)

devin
devin

Reputation: 194

Not sure if I understand your problem, but the field "activityid" in the activitypointer object is the same activityid as the underlying activity (email, task, phonecall, etc). The regardingobjectid is the link to the regarding entity.

Heres what you need to get the equivalent of the Activities page

        ColumnSet cols = new ColumnSet()
        {
           Attributes = new string[] { "subject", "regardingobjectid", "regardingobjectidname", "regardingobjectidtypecode", "activitytypecodename", "createdon", "scheduledstart", "scheduledend"  }
        };
        ConditionExpression condition = new ConditionExpression()
        {
           AttributeName = "ownerid",
           Operator = ConditionOperator.Equal,
           Values = new object[] { CurrentUser.systemuserid.Value } //CurrentUser is an systemuser object that represents the current user (WhoAmIRequest)
        };
        FilterExpression filter = new FilterExpression()
        {
           Conditions = new ConditionExpression[] { condition },
           FilterOperator = LogicalOperator.And
        };
        QueryExpression query = new QueryExpression()
        {
           EntityName = EntityName.activitypointer.ToString(),
           ColumnSet = cols,
           Criteria = filter
        };

        BusinessEntityCollection activities = svc.RetrieveMultiple(query);

        foreach (activitypointer activity in activities)
        {
           //do something with the activity

           //or get the email object
           email originalEmail = (email)svc.Retrieve(EntityName.email.ToString(), activity.activityid.Value, new AllColumns());

        }

Upvotes: 1

Related Questions