Programmatically validate Pre/Post plugin step

By | February 20, 2020

Introduction

In this blog, we will see how to programmatically check whether plugin step already exists or not for both Pre & Post stage.

We came across this situation when we received a requirement, where we had to programmatically register plugins. Well, the registration piece was quite easy, however, we struggled to see if the step was already registered or not.

For this, we came up with the following solution:
In order to check whether the plugin already exists or not we need to retrieve entity with specific attributes.

 //Variables
QueryByAttribute _Query = null;

RetrieveMultipleRequest _Request = null;

RetrieveMultipleResponse _Response = null;

EntityCollection _EntityColl = null;

Entity _Entity = null;

 

//Code

 

_Query = new QueryByAttribute();

 

_Request = new RetrieveMultipleRequest();

 

_Query.EntityName = “account”;

 

_Query.ColumnSet = new ColumnSet(_RetrieveColumns);

 

_Query.Attributes.AddRange(_FilterByColumns);

 

_Query.Values.AddRange(_FilterByColumnsValue);

 

_Request.Query = _Query;

 

_Response = (RetrieveMultipleResponse)_CrmWebService.Execute(_Request);

_EntityColl = _Response.EntityCollection;

 

//Check if the EntityCollection is not empty

if (_EntityColl != null && _EntityColl.Entities.Count > 0)

{

_Tracing.Trace("Found entity");

//Return the topmost entity of the entitycollection

_Entity = (Entity)_EntityColl.Entities[0];

}

//Stage is added here to check plugin is added already on pre/post operation for selected entity – we need to pass stage as an parameter

In above example “_RetrieveColumns” contain columns need to be retrieve, “_FilterByColumns” contains filter attributes and “_FilterByColumnsValue” contains filter attributes values on which we want to check.

_RetrieveColumns = new string[] { "sdkmessagefilterid" }

_FilterByColumns = new string[] { "plugintypeid", "sdkmessageid", "sdkmessagefilterid", "stage"}

_FilterByColumnsValue = new object[] { PluginTypeEntityRef.Id, SDKMessageIdRefrence.Id, SdkMsgFiltrEntityRef.Id, SdkStep.Stage.Value };

Conclusion

Thus, with the above code you can programmatically check whether the plugin step is registered or not for pre/post stage.

Kanban Board