web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Register plugin programatically in CRM Online OR 2011?

(0) ShareShare
ReportReport
Posted on by 130

Hi - is there any way to register plugins with a .NET application in current versions of CRM?

Why has the source code to the Plugin Registration Tool been removed from the SDK?  I thought it used to be there but not now - I thought that might give me some clues.

Using the tool is too time consuming and prone to human error to reproduce exactly.  I want to specify script or code to do this in a reproducible way for consistent deployment to production and test environments and so any advice would be much appreciated.

*This post is locked for comments

I have the same question (0)
  • Simon Miles Profile Picture
    5 on at

    Hi,

    It was very much doable in CRM 4.0 but MSDN doesn't say its not possible in CRM 2011.

    So, the key entity types used to register plug-ins and images are: PluginAssembly, PluginType, SdkMessageProcessingStep, and SdkMessageProcessingStepImage. The key entity types used to register custom workflow activities are PluginAssembly and PluginType. Use these entities with the create, update, retrieve, and delete operations. For sample code showing the use of these classes, see the Plug-in Registration tool source code. Here is the complete URL:

    msdn.microsoft.com/.../gg309620.aspx

  • unizap Profile Picture
    on at

    We had worked on a requirement where we developed a class to register/delete plugin steps programmatically.

    The code pasted here will only let you create/delete steps. you may modify the code to add plugin registration logic as well.

    ---CODE-------

    public class CrmPluginStep

       {

           #region Data Members

           private int customizationLevel;

           private Boolean deleteAsyncOperationIfSuccessful;

           private Entity step = new Entity("sdkmessageprocessingstep");

           private CrmPluginStepDeployment deployment;

           private string eventHandler;

           private string filteringAttributes;

           private Guid impersonatingUserId;

           private CrmPluginStepInvocationSource? invocationSource;

           private string message;

           private Guid messageEntityId;

           private Guid messageId;

           private CrmPluginStepMode mode;

           private string name;

           private Guid pluginId;

           private string primaryEntity;

           private int rank;

           private string secureConfiguration;

           private Guid secureConfigurationId;

           private CrmPluginStepStage stage;

           private Guid stepId;

           private string unsecureConfiguration;

           #endregion Data Members

           //default constructor

           public CrmPluginStep()

           {

           }

           #region Properties

           public int CustomizationLevel

           {

               get { return customizationLevel; }

               set { customizationLevel = value; }

           }

           public Boolean DeleteAsyncOperationIfSuccessful

           {

               get { return deleteAsyncOperationIfSuccessful; }

               set { deleteAsyncOperationIfSuccessful = value; }

           }

           public CrmPluginStepDeployment Deployment

           {

               get { return deployment; }

               set { deployment = value; }

           }

           /// <summary>

           /// Name of the Plugin on which the step needs to be registered

           /// </summary>

           public string EventHandler

           {

               get { return eventHandler; }

               set { eventHandler = value; }

           }

           public string FilteringAttributes

           {

               get { return filteringAttributes; }

               set { filteringAttributes = value; }

           }

           public Guid ImpersonatingUserId

           {

               get { return impersonatingUserId; }

               set { impersonatingUserId = value; }

           }

           public CrmPluginStepInvocationSource? InvocationSource

           {

               get { return invocationSource; }

               set { invocationSource = value; }

           }

           /// <summary>

           /// Name of the message - e.g. create,update,delete

           /// </summary>

           public string Message

           {

               get { return message; }

               set { message = value; }

           }

           public Guid MessageEntityId

           {

               get { return messageEntityId; }

               set { messageEntityId = value; }

           }

           public Guid MessageId

           {

               get { return messageId; }

               set { messageId = value; }

           }

           /// <summary>

           /// Mode of operation - Sync/Async

           /// </summary>

           public CrmPluginStepMode Mode

           {

               get { return mode; }

               set { mode = value; }

           }

           /// <summary>

           /// Step Name - any valid name is allowed

           /// </summary>

           public string Name

           {

               get { return name; }

               set { name = value; }

           }

           public Guid PluginId

           {

               get { return pluginId; }

               set { pluginId = value; }

           }

           /// <summary>

           /// Primary entity name

           /// </summary>

           public string PrimaryEntity

           {

               get { return primaryEntity; }

               set { primaryEntity = value; }

           }

           /// <summary>

           /// Execution order

           /// </summary>

           public int Rank

           {

               get { return rank; }

               set { rank = value; }

           }

           public string SecureConfiguration

           {

               get { return secureConfiguration; }

               set { secureConfiguration = value; }

           }

           public Guid SecureConfigurationId

           {

               get { return secureConfigurationId; }

               set { secureConfigurationId = value; }

           }

           public CrmPluginStepStage Stage

           {

               get { return stage; }

               set { stage = value; }

           }

           public Guid StepId

           {

               get { return stepId; }

               set { stepId = value; }

           }

           public string UnsecureConfiguration

           {

               get { return unsecureConfiguration; }

               set { unsecureConfiguration = value; }

           }

           #endregion Properties

           #region Methods

           public void DeleteStep(ref IOrganizationService service, Guid stepId)

           {

               service.Delete("sdkmessageprocessingstep", stepId);

           }

           public void DeletePlugin(ref IOrganizationService service, Guid pluginId)

           {

               service.Delete("plugintype", pluginId);

           }

           public Guid RetrieveStepId(ref IOrganizationService service, string stepName)

           {

               Entity step = RetrieveEntity(ref service, "sdkmessageprocessingstep", new string[] { "name" }, new string[] { stepName }, new ColumnSet(true), ConditionOperator.Equal);

               if (step != null)

                   return step.Id;

               else

                   return Guid.Empty;

           }

           public EntityCollection RetrieveStepsByPluginId(ref IOrganizationService service, Guid pluginId)

           {

               EntityCollection steps = RetrieveEntityCollection(ref service, "sdkmessageprocessingstep", new string[] { "plugintypeid" }, new string[] { pluginId.ToString() }, new ColumnSet(true), ConditionOperator.Equal);

               if (steps != null)

                   return steps;

               else

                   return null;

           }

           public Guid RetrievePluginId(ref IOrganizationService service, string pluginName)

           {

               Entity plugin = RetrieveEntity(ref service, "plugintype", new string[] { "name" }, new string[] { pluginName }, new ColumnSet(true), ConditionOperator.Equal);

               if (plugin != null)

                   return plugin.Id;

               else

                   return Guid.Empty;

           }

           public Guid RegisterStep(ref IOrganizationService service)

           {

               SetMessageId(ref service);

               SetMessageEntityId(ref service);

               SetPluginId(ref service);

               step["sdkmessageid"] = new EntityReference("sdkmessage", this.MessageId);

               step["sdkmessagefilterid"] = new EntityReference("sdkmessage", this.MessageEntityId);

               step["eventhandler"] = new EntityReference("plugintype", this.PluginId);

               step["name"] = this.Name;

               step["mode"] = new OptionSetValue((int)this.Mode);

               step["rank"] = this.Rank;

               step["invocationsource"] = new OptionSetValue((int)this.InvocationSource);

               step["stage"] = new OptionSetValue((int)this.Stage);

               step["supporteddeployment"] = new OptionSetValue((int)this.Deployment);

               if (string.IsNullOrEmpty(this.FilteringAttributes))

               {

                   step["filteringattributes"] = string.Empty;

               }

               else

               {

                   step["filteringattributes"] = this.FilteringAttributes;

               }

               try

               {

                   return service.Create(step);

               }

               catch (Exception ex)

               {

                   throw new Exception("Step Registration Error - " + ex.Message);

               }

           }

           private Entity RetrieveEntity(ref IOrganizationService service, string entityName, string[] entitySearchField, object[] entitySearchFieldValue, ColumnSet columnSet, ConditionOperator op)

           {

               Entity entity = null;

               QueryExpression query = new QueryExpression();

               query.EntityName = entityName;

               FilterExpression filter = new FilterExpression();

               for (int i = 0; i < entitySearchField.Length; i++)

               {

                   ConditionExpression condition = new ConditionExpression();

                   condition.AttributeName = entitySearchField[i];

                   condition.Operator = op;

                   condition.Values.Add(entitySearchFieldValue[i]);

                   filter.FilterOperator = LogicalOperator.And;

                   filter.AddCondition(condition);

               }

               query.ColumnSet = columnSet;

               query.Criteria = filter;

               EntityCollection collection;

               try

               {

                   collection = service.RetrieveMultiple(query);

               }

               catch (Exception ex)

               {

                   throw new Exception("Step Registration Error - " + ex.Message);

               }

               if (collection.Entities.Count == 1)

               {

                   entity = (Entity)collection.Entities[0];

               }

               return entity;

           }

           private EntityCollection RetrieveEntityCollection(ref IOrganizationService service, string entityName, string[] entitySearchField, object[] entitySearchFieldValue, ColumnSet columnSet, ConditionOperator op)

           {

               EntityCollection entity = null;

               QueryExpression query = new QueryExpression();

               query.EntityName = entityName;

               FilterExpression filter = new FilterExpression();

               for (int i = 0; i < entitySearchField.Length; i++)

               {

                   ConditionExpression condition = new ConditionExpression();

                   condition.AttributeName = entitySearchField[i];

                   condition.Operator = op;

                   condition.Values.Add(entitySearchFieldValue[i]);

                   filter.FilterOperator = LogicalOperator.And;

                   filter.AddCondition(condition);

               }

               query.ColumnSet = columnSet;

               query.Criteria = filter;

               EntityCollection collection;

               try

               {

                   collection = service.RetrieveMultiple(query);

               }

               catch (Exception ex)

               {

                   throw new Exception("Step Registration Error - " + ex.Message);

               }

               if (collection.Entities.Count > 0)

               {

                   entity = collection;

               }

               return entity;

           }

           private void SetMessageEntityId(ref IOrganizationService service)

           {

               Entity message = RetrieveEntity(ref service, "sdkmessagefilter", new string[] { "sdkmessageid", "primaryobjecttypecode" }, new string[] { MessageId.ToString(), PrimaryEntity.ToLower() }, new ColumnSet(true), ConditionOperator.Equal);

               if (message != null)

                   MessageEntityId = message.Id;

           }

           private void SetMessageId(ref IOrganizationService service)

           {

               Entity message = RetrieveEntity(ref service, "sdkmessage", new string[] { "name" }, new string[] { Message.ToLower() }, new ColumnSet(true), ConditionOperator.Equal);

               if (message != null)

                   MessageId = message.Id;

           }

           private void SetPluginId(ref IOrganizationService service)

           {

               Entity plugin = RetrieveEntity(ref service, "plugintype", new string[] { "name" }, new string[] { EventHandler }, new ColumnSet(true), ConditionOperator.Equal);

               if (plugin != null)

                   PluginId = plugin.Id;

           }        

           #endregion Methods

           #region Enumerations

           public enum CrmPluginStepDeployment

           {

               ServerOnly = 0,

               OfflineOnly = 1,

               Both = 2

           }

           public enum CrmPluginStepInvocationSource

           {

               Parent = 0,

               Child = 1

           }

           public enum CrmPluginStepMode

           {

               Asynchronous = 1,

               Synchronous = 0

           }

           public enum CrmPluginStepStage

           {

               PreValidation = 10,

               PreOperation = 20,

               PostOperation = 40,

               PostOperationDeprecated = 50

           }

           #endregion Enumerations

       }

  • PaulR Profile Picture
    130 on at

    Thanks Imran, Unizap, & Guido for taking the time to reply - I really appreciate it.  There is still a bit of work required it seems, sad there is not more out of the box that would help to automate deployment in general.

    In the end, we decided to put this off and continue with manual deployment for a while longer.

    regards,

    Paul.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans