Fetch the publisher's prefix in dynamics 365 plugin

71 views Asked by At

I am writing a plugin for dynamics to store some records in a custom entity/table. Now the problem is every custom entity/table you create in the dynamics environment is assigned a publisher prefix in its LogicalName and schema name even its attributes have the prefix. Now when a certain event is triggered in the system the plugin stores some information in this table but i don't know what the prefix is.

PROBLEM:
How can I fetch the publisher prefix inside the plugin dynamically?

I can hardcode the name of the table in the plugin but the prefix needs to be fetched dynamically.

I can just fetch all entities and then filter out the table I created, but I believe that will be heavily taxing on the dynamics system.

Well I do have oauth access to the dynamics CRM. Can I store the prefix in the dynamics system somewhere probably as a name of the entity then dynamically fetch it in the plugin?

2

There are 2 answers

0
Junaid Soomro On BEST ANSWER

So, I managed to finally figure out how to fetch the prefix and this is how, I am assuming the plugin was registered in the active solutions.

  1. Fetch your registered plugin by querying the pluginassembly table using QueryExpression.
  2. Extract the solutionId from the plugin info.
  3. Fetch the solution by solutionId and that's how you get the publisherId associated with the solution and then use that to fetch the prefix. The property is called customizationprefix.

The sample code is as follows,

 QueryExpression query = new QueryExpression
 {
     EntityName = "pluginassembly",
     ColumnSet = new ColumnSet("solutionid"),
     Criteria = new FilterExpression()
 };

 query.Criteria.AddCondition("name", ConditionOperator.Equal, "pluginName");

 var pluginInfo = service.RetrieveMultiple(query);

 solutionId = pluginInfo.Entities[0].Attributes["solutionid"].ToString();
 var solutionEntity = service.Retrieve("solution", Guid.Parse(solutionId), new ColumnSet(true));

 EntityReference publisherAttr = (EntityReference)solutionEntity.Attributes["publisherid"];
 var publisher = service.Retrieve("publisher", publisherAttr.Id, new ColumnSet(true));
 
 string publisherPrefix = publisher.Attributes["customizationprefix"].ToString();

I understand that the code is poorly written because I am not a .Net dev but it does the job ;).

2
Guido Preite On

Plugins are attached to a message to a specific table (for example create of account or update of myprefix_customtable)

Inside the plugin you can check the table that triggered the event by extracting the LogicalName of the Entity or EntityReference (EntityReference you have inside the Delete message)

However you have no direct information on the publisher (or the solution) from a plugin. You can query publisher and solution tables, but that has nearly nothing to do on which message and field your plugin has been triggered.