The following code displays a list of messages and entities that support plug-ins. This sample code can be found in the following file in the SDK download:
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
//# [How to Retrieve a List of Messages and Entities that Support Plug-ins ] | |
using System; | |
using System.Collections; | |
using CrmSdk; | |
using MetadataServiceSdk; | |
using Microsoft.Crm.Sdk.Utility; | |
namespace Microsoft.Crm.Sdk.HowTo | |
{ | |
public class RetrieveSupportedMessages | |
{ | |
public static bool Run(string crmServerUrl, string orgName) | |
{ | |
bool success = true; | |
// Store all Create, Retrieve, Update, and Delete sdk messages supported by the account entity for verification. | |
ArrayList crudMessagesForVerification = new ArrayList(); | |
try | |
{ | |
// Set up the CRM Services. | |
CrmService service = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(crmServerUrl, orgName); | |
service.PreAuthenticate = true; | |
MetadataService metadataService = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetMetadataService(crmServerUrl, orgName); | |
metadataService.PreAuthenticate = true; | |
// Retrieve a list of all entities. | |
RetrieveAllEntitiesRequest allEntitiesRequest = new RetrieveAllEntitiesRequest(); | |
allEntitiesRequest.RetrieveAsIfPublished = true; | |
allEntitiesRequest.MetadataItems = MetadataItems.EntitiesOnly; | |
// Execute the request. | |
RetrieveAllEntitiesResponse allEntitiesResponse = (RetrieveAllEntitiesResponse)metadataService.Execute(allEntitiesRequest); | |
// Create a query to get all related sdk messages. | |
// An example SQL query that will be built for every entity: | |
// SELECT sdkmessage.name | |
// FROM sdkmessage | |
// INNER JOIN sdkmessagefilter ON sdkmessagefilter.skdmessageid = sdkmessage.skdmessageid | |
// WHERE sdkmessagefilter.primaryobjecttypecode = entity.LogicalName; | |
QueryExpression supportedMessagesQuery = new QueryExpression(); | |
// Iterate through the retrieved entities. | |
foreach (EntityMetadata entity in allEntitiesResponse.CrmMetadata) | |
{ | |
// Retrieve the supported message name for this entity. | |
ColumnSet sdkMessageColumns = new ColumnSet(); | |
sdkMessageColumns.Attributes = new string[] { "name" }; | |
// Build the WHERE clause condition. | |
ConditionExpression schemaNameCondition = new ConditionExpression(); | |
schemaNameCondition.AttributeName = "primaryobjecttypecode"; | |
schemaNameCondition.Operator = ConditionOperator.Equal; | |
schemaNameCondition.Values = new object[1]; | |
schemaNameCondition.Values[0] = entity.LogicalName; | |
// Create the WHERE clause filter. | |
FilterExpression whereExpression = new FilterExpression(); | |
whereExpression.Conditions = new ConditionExpression[] { schemaNameCondition }; | |
// Create the inner join link. | |
LinkEntity innerJoinAccount = new LinkEntity(); | |
innerJoinAccount.JoinOperator = JoinOperator.Inner; | |
innerJoinAccount.LinkCriteria = whereExpression; | |
innerJoinAccount.LinkFromAttributeName = "sdkmessageid"; | |
innerJoinAccount.LinkFromEntityName = EntityName.sdkmessage.ToString(); | |
innerJoinAccount.LinkToAttributeName = "sdkmessageid"; | |
innerJoinAccount.LinkToEntityName = EntityName.sdkmessagefilter.ToString(); | |
// Set the query properties. | |
supportedMessagesQuery.EntityName = EntityName.sdkmessage.ToString(); | |
supportedMessagesQuery.ColumnSet = sdkMessageColumns; | |
supportedMessagesQuery.LinkEntities = new LinkEntity[] { innerJoinAccount }; | |
// Retrieve all sdkmessage names for this entity. | |
BusinessEntityCollection coll = service.RetrieveMultiple(supportedMessagesQuery); | |
// Output the supported messages for this entity | |
Console.WriteLine("============================================================================"); | |
Console.WriteLine("Entity: " + entity.LogicalName); | |
if (coll.BusinessEntities.Length > 0) | |
{ | |
Console.WriteLine("Supported Messages:"); | |
} | |
else | |
{ | |
Console.WriteLine("No Messages Supported."); | |
} | |
string sdkMessageName = string.Empty; | |
foreach(BusinessEntity anSdkMessage in coll.BusinessEntities) | |
{ | |
sdkMessageName = ((sdkmessage)anSdkMessage).name; | |
Console.WriteLine("\t\t" + sdkMessageName); | |
// Verify that the account entity supports create, retrieve, update, delete. | |
if (entity.LogicalName == EntityName.account.ToString()) | |
{ | |
// Store all Create, Retrieve, Update, and Delete messages. | |
if (sdkMessageName == "Create" || sdkMessageName == "Retrieve" || | |
sdkMessageName == "Update" || sdkMessageName == "Delete") | |
{ | |
crudMessagesForVerification.Add(sdkMessageName); | |
} | |
} | |
} | |
} | |
#region check success | |
// Validate that the 4 Create, Retrieve, Update, and Delete messages were found. | |
if (crudMessagesForVerification.Count != 4) | |
{ | |
success = false; | |
} | |
#endregion | |
} | |
catch (System.Web.Services.Protocols.SoapException) | |
{ | |
// Perform error handling here. | |
throw; | |
} | |
catch (Exception) | |
{ | |
throw; | |
} | |
return success; | |
} | |
} | |
} |