Use a Join to Retrieve Activities by Participant
Collapse the table of content
Expand the table of content
This documentation is archived and is not being maintained.

Use a Join to Retrieve Activities by Participant

Dynamics
banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

This sample demonstrates doing a simple JOIN to the activityparty entity using a query expression.

This sample code can be found in the following files in the SDK download:

Server\HowTo\CS\Query\RetrieveActivitiesByParticipant.cs
Server\HowTo\VB\Query\RetrieveActivitiesByParticipant.vb

For more information about the helper methods in the Microsoft.Crm.Sdk.Utility.CrmServiceUtility namespace, see Utility Sample Code.

Example

[C#]
using System;
using CrmSdk;
using Microsoft.Crm.Sdk.Utility;

namespace Microsoft.Crm.Sdk.HowTo
{
      /// <summary>
      /// This sample shows how to retrieve all activities where the user is a participant.
      /// </summary>
      public class RetrieveActivitiesByParticipant
      {
            static void Main(string[] args)
            {
                  // TODO: Change the server URL and Organization to match your CRM Server and CRM Organization
                  RetrieveActivitiesByParticipant.Run("http://localhost:5555", "CRM_SDK");
            }

            public static bool Run(string crmServerUrl, string orgName)
            {
                  // Set up the CRM Service.
                  CrmService service = CrmServiceUtility.GetCrmService(crmServerUrl, orgName);

                  #region Setup Data Required for this Sample

                  bool success = false;

                  #endregion

                  try
                  {
                        // Get the user information.
                        WhoAmIRequest userRequest = new WhoAmIRequest();
                        WhoAmIResponse user = (WhoAmIResponse) service.Execute(userRequest);

                        // Create the ConditionExpression.
                        ConditionExpression condition = new ConditionExpression();

                        // Set the condition for the retrieval to retrieve all activities that belong to the current user.
                        condition.AttributeName = "partyid";
                        condition.Operator = ConditionOperator.Equal;
                        condition.Values = new string [] {user.UserId.ToString()};

                        // Build the filter based on the condition.
                        FilterExpression filter = new FilterExpression();
                        filter.FilterOperator = LogicalOperator.And;
                        filter.Conditions = new ConditionExpression[] {condition};

                        // Create a LinkEntity to link the activity participant to the activity.
                        LinkEntity link = new LinkEntity();

                        // Set the properties of the LinkEntity.
                        link.LinkCriteria = filter;

                        // Set the linking entity to be the activity.
                        link.LinkFromEntityName = EntityName.activitypointer.ToString();

                        // Set the attribute being linked to to be the activityid.
                        link.LinkFromAttributeName = "activityid";

                        // Set the entity being linked to to be the activityparty.
                        link.LinkToEntityName = EntityName.activityparty.ToString();
                  
                        // Set the attribute linking to the activityparty to be the activityid.
                        link.LinkToAttributeName = "activityid";
                  
                        // Create the query.
                        QueryExpression query = new QueryExpression();

                        // Set the properties of the query.
                        query.EntityName = EntityName.activitypointer.ToString();
                        // Be aware that using AllColumns may adversely affect
                        // performance and cause unwanted cascading in subsequent 
                        // updates. A best practice is to retrieve the least amount of 
                        // data required.
                        query.ColumnSet = new AllColumns();
                        query.LinkEntities = new LinkEntity[] {link};

                        // Create the request object.
                        RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();

                        // Set the properties of the request object.
                        retrieve.Query = query;
            
                        // Execute the request.
                        RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse) service.Execute(retrieve);

                        #region check success

                        if ((retrieved.BusinessEntityCollection.EntityName.ToLower().Equals("activitypointer")))
                        {
                              success = true;
                        }

                        #endregion
                  }
                  catch (System.Web.Services.Protocols.SoapException)
                  {
                        // Add your error handling code here.
                  }

                  return success;
            }
      }
}
[Visual Basic .NET]
Imports System

Imports CrmSdk
Imports Microsoft.Crm.Sdk.Utility

Namespace Microsoft.Crm.Sdk.HowTo
      
      ' <summary>
      ' This sample shows how to retrieve all activities where the user is a participant.
      ' </summary>
      Public Class RetrieveActivitiesByParticipant

            Sub Main()

                  ' TODO: Change the server URL and Organization to match your CRM Server and CRM Organization
                  RetrieveActivitiesByParticipant.Run("http://localhost:5555", "CRM_SDK")

            End Sub

            Public Shared Function Run(ByVal crmServerUrl As String, ByVal orgName As String) As Boolean
                  
                  ' Set up the CRM Service.
                  Dim service As CrmService =  CrmServiceUtility.GetCrmService(crmServerUrl,orgName)
                  
                  '---- Setup Data Required for this Sample ------------------------------
                  Dim success As Boolean = False
                  '-----------------------------------------------------------------------

                  Try
                        ' Get the user information.
                        Dim userRequest As New WhoAmIRequest()
                        Dim user As WhoAmIResponse = CType(service.Execute(userRequest), WhoAmIResponse)

                        ' Create the ConditionExpression.
                        Dim condition As New ConditionExpression()

                        ' Set the condition for the retrieval to retrieve all activities belonging to the current user.
                        condition.AttributeName = "partyid"
                        condition.[Operator] = ConditionOperator.Equal
                        condition.Values = New String() {user.UserId.ToString()}

                        ' Build the filter based on the condition.
                        Dim filter As New FilterExpression()
                        filter.FilterOperator = LogicalOperator.And
                        filter.Conditions = New ConditionExpression() {condition}

                        ' Create a LinkEntity to link the activity participant to the activity.
                        Dim link As New LinkEntity()

                        ' Set the properties of the LinkEntity.
                        link.LinkCriteria = filter

                        ' Set the linking entity to be the activity.
                        link.LinkFromEntityName = EntityName.activitypointer.ToString()

                        ' Set the attribute being linked to to be the activityid.
                        link.LinkFromAttributeName = "activityid"

                        ' Set the entity being linked to to be the activityparty.
                        link.LinkToEntityName = EntityName.activityparty.ToString()

                        ' Set the attribute linking to the activityparty to be the activityid.
                        link.LinkToAttributeName = "activityid"

                        ' Create the query.
                        Dim query As New QueryExpression()

                        ' Set the properties of the query.
                        query.EntityName = EntityName.activitypointer.ToString()
                        ' Be aware that using AllColumns may adversely affect 
                        ' performance and cause unwanted cascading in subsequent 
                        ' updates.  A best practice is to retrieve the least amount of 
                        ' data required.
                        query.ColumnSet = New AllColumns()
                        query.LinkEntities = New LinkEntity() {link}

                        ' Create the request object.
                        Dim retrieve As New RetrieveMultipleRequest()

                        ' Set the properties of the request object.
                        retrieve.Query = query

                        ' Execute the request.
                        Dim retrieved As RetrieveMultipleResponse = CType(service.Execute(retrieve), RetrieveMultipleResponse)

                        '---- check success ----------------------------------------------------
                If retrieved.BusinessEntityCollection.EntityName.ToLower().Equals("activitypointer") Then
                    success = True
                End If
                '-----------------------------------------------------------------------
            Catch e As System.Web.Services.Protocols.SoapException
                        ' Add your error handling code here.
                  End Try

                  Return success
            End Function 'Run
      End Class 'RetrieveActivitiesByParticipant
End Namespace 'Microsoft.Crm.Sdk.HowTo

See Also

Reference


© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2016 Microsoft