How to Retrieve a List of Messages and Entities that Support Plug-ins

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:



   1:  //# [How to Retrieve a List of Messages and Entities that Support Plug-ins ]

   2:  using System;

   3:  using System.Collections;

   4:  using CrmSdk;

   5:  using MetadataServiceSdk;

   6:  using Microsoft.Crm.Sdk.Utility;

   7:   

   8:  namespace Microsoft.Crm.Sdk.HowTo

   9:  {

  10:     public class RetrieveSupportedMessages

  11:     {

  12:        public static bool Run(string crmServerUrl, string orgName)

  13:        {

  14:           bool success = true;

  15:           

  16:           // Store all Create, Retrieve, Update, and Delete sdk messages supported by the account entity for verification.

  17:           ArrayList crudMessagesForVerification = new ArrayList();

  18:   

  19:           try

  20:           {

  21:              // Set up the CRM Services.  

  22:              CrmService service = 

  23:                  Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(

  24:                  crmServerUrl, orgName);

  25:              service.PreAuthenticate = true;

  26:   

  27:              MetadataService metadataService =

  28:                  Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetMetadataService(

  29:                  crmServerUrl, orgName);

  30:              metadataService.PreAuthenticate = true;

  31:   

  32:              // Retrieve a list of all entities.

  33:              RetrieveAllEntitiesRequest allEntitiesRequest = 

  34:                  new RetrieveAllEntitiesRequest();

  35:              allEntitiesRequest.RetrieveAsIfPublished = true;

  36:              allEntitiesRequest.MetadataItems = MetadataItems.EntitiesOnly;

  37:   

  38:              // Execute the request.

  39:              RetrieveAllEntitiesResponse allEntitiesResponse =

  40:              (RetrieveAllEntitiesResponse)metadataService.Execute(allEntitiesRequest);

  41:   

  42:              // Create a query to get all related sdk messages. 

  43:              // An example SQL query that will be built for every entity:

  44:              // SELECT sdkmessage.name

  45:              // FROM   sdkmessage

  46:              // INNER JOIN sdkmessagefilter ON sdkmessagefilter.skdmessageid = sdkmessage.skdmessageid

  47:              // WHERE  sdkmessagefilter.primaryobjecttypecode = entity.LogicalName;

  48:              QueryExpression supportedMessagesQuery = new QueryExpression();

  49:              

  50:              // Iterate through the retrieved entities.

  51:              foreach (EntityMetadata entity in allEntitiesResponse.CrmMetadata)

  52:              {

  53:                 // Retrieve the supported message name for this entity.

  54:                 ColumnSet sdkMessageColumns = new ColumnSet();

  55:                 sdkMessageColumns.Attributes = new string[] { "name" };

  56:                 

  57:                 // Build the WHERE clause condition.

  58:                 ConditionExpression schemaNameCondition = new ConditionExpression();

  59:                 schemaNameCondition.AttributeName = "primaryobjecttypecode";

  60:                 schemaNameCondition.Operator = ConditionOperator.Equal;

  61:                 schemaNameCondition.Values = new object[1];

  62:                 schemaNameCondition.Values[0] = entity.LogicalName;

  63:                 

  64:                 // Create the WHERE clause filter.

  65:                 FilterExpression whereExpression = new FilterExpression();

  66:                 whereExpression.Conditions = 

  67:                     new ConditionExpression[] { schemaNameCondition };

  68:                 

  69:                 // Create the inner join link.

  70:                 LinkEntity innerJoinAccount = new LinkEntity();

  71:                 innerJoinAccount.JoinOperator = JoinOperator.Inner;

  72:                 innerJoinAccount.LinkCriteria = whereExpression;

  73:                 innerJoinAccount.LinkFromAttributeName = "sdkmessageid";

  74:                 innerJoinAccount.LinkFromEntityName =

  75:                     EntityName.sdkmessage.ToString();

  76:                 innerJoinAccount.LinkToAttributeName = "sdkmessageid";

  77:                 innerJoinAccount.LinkToEntityName =

  78:                     EntityName.sdkmessagefilter.ToString();

  79:   

  80:                 // Set the query properties.

  81:                 supportedMessagesQuery.EntityName = EntityName.sdkmessage.ToString();

  82:                 supportedMessagesQuery.ColumnSet = sdkMessageColumns;

  83:                 supportedMessagesQuery.LinkEntities = 

  84:                     new LinkEntity[] { innerJoinAccount };

  85:   

  86:                 // Retrieve all sdkmessage names for this entity.

  87:                 BusinessEntityCollection coll =

  88:                     service.RetrieveMultiple(supportedMessagesQuery);

  89:                 

  90:                 // Output the supported messages for this entity

  91:                 Console.WriteLine("============================================================================");

  92:                 Console.WriteLine("Entity: " + entity.LogicalName);

  93:                 if (coll.BusinessEntities.Length > 0)

  94:                 {

  95:                    Console.WriteLine("Supported Messages:");

  96:                 }

  97:                 else

  98:                 {

  99:                    Console.WriteLine("No Messages Supported.");

 100:                 }

 101:                 string sdkMessageName = string.Empty;

 102:                 foreach(BusinessEntity anSdkMessage in coll.BusinessEntities)

 103:                 {

 104:                    sdkMessageName = ((sdkmessage)anSdkMessage).name;

 105:                    Console.WriteLine("\t\t" + sdkMessageName);

 106:                    

 107:                    // Verify that the account entity supports create, retrieve, update, delete.

 108:                    if (entity.LogicalName == EntityName.account.ToString())

 109:                    {

 110:                       // Store all Create, Retrieve, Update, and Delete messages.

 111:                       if (sdkMessageName == "Create" || sdkMessageName == "Retrieve" ||

 112:                          sdkMessageName == "Update" || sdkMessageName == "Delete")

 113:                       {

 114:                          crudMessagesForVerification.Add(sdkMessageName);

 115:                       }

 116:                    }

 117:                 }

 118:              }

 119:   

 120:              #region check success

 121:   

 122:              // Validate that the 4 Create, Retrieve, Update, and Delete messages were found.

 123:              if (crudMessagesForVerification.Count != 4)

 124:              {

 125:                 success = false;

 126:              }

 127:   

 128:              #endregion

 129:           }

 130:           catch (System.Web.Services.Protocols.SoapException)

 131:           {

 132:              // Perform error handling here.

 133:              throw;

 134:           }

 135:           catch (Exception)

 136:           {

 137:              throw;

 138:           }

 139:   

 140:           return success;

 141:        }

 142:     }

 143:  }