This sample demonstrates doing a simple JOIN to the activityparty entity using a query expression.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//# [Use a Join to Retrieve Activities by Participant ] | |
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; | |
} | |
} | |
} |