Answered by:
Plugin code explanation

Question
-
Hi,
I am new to Microsoft Dynamics CRM. I have requirement where i need to develop a plugin. I have the code but I don't know how exactly it works.
Can any one explain me this below code.........
I just need explanation on Namespaces, syntax, interfaces used in the code....
Please help me out...
Thanks in Advance..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk;
using System.Runtime;
using System.Data.Common;
using System.Data.Services;
using System.Data.Services.Common;
using Xrm;
namespace AutoNumber{
public class Patient : IPlugin {
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName == "new_employee")
{
// An accountnumber attribute should not already exist because
// it is system generated.
if (entity.Attributes.Contains("new_employeeid1") == false)
{
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
OrganizationServiceContext orgContext = new OrganizationServiceContext(service);
if (entity.Attributes.Contains("new_subemployeeid") == true)
{
var ttGuid = ((EntityReference)entity["new_subemployeeid"]).Id;
//Grab case to create subcase on
var subTT = (from CN in orgContext.CreateQuery<new_employee>()
where CN.Id == ttGuid
select CN).First();
//Check for existing sub TT numbers
var checkForCounter = from entry in orgContext.CreateQuery<new_employee_autonumbering>()
where entry.new_name == subTT.new_employeeid1
select entry;
int i = 0;
foreach (var t in checkForCounter)
{
i++;
}
//if sub tt doesnt exist, create counter for sub tt
if (i == 0)
{
var counter = new new_employee_autonumbering()
{
new_name = subTT.new_employeeid1,
new_counter = 0
};
orgContext.AddObject(counter);
orgContext.SaveChanges();
}
//read from sub tt and tt number and set this number as TTNUBMER - SUBTTNUMBER on the form. save changes
var subCounter = checkForCounter.First();
var sub = subCounter.new_counter + 1;
var main = subTT.new_employeeid1;
entity.Attributes.Add("new_employeeid1", main + "-" + sub.ToString());
subCounter.new_counter = sub;
orgContext.UpdateObject(subCounter);
orgContext.SaveChanges();
}
else
{
//if no sub tt has been selected create new, grab counter from CaseCounter entry and add 1, save after this
var query = from CN in orgContext.CreateQuery<new_employee_autonumbering>()
where CN.new_name == "CaseCounter"
select CN;
foreach (var entry in query)
{
var t = entry.new_counter + 1;
string patientid;
patientid = "EMP-" + t.ToString();
entity.Attributes.Add("new_employeeid1", patientid);
entry.new_counter = t;
orgContext.UpdateObject(entry);
orgContext.SaveChanges();
}
}
}
}
else
{
throw new InvalidPluginExecutionException("TrackNTrace Number can only be set by the system.");
}
}
}
}
}
Thursday, January 5, 2012 12:04 PM
Answers
-
Did you read through the comments? The comments are the lines that start with "//". This plugin looks like it is using LINQ to CRM to autonumber a Case. I am guessing this plugin is fired on-create of the entity.
Jamie Miley
Check out my about.me profile!
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Marked as answer by Jamie MileyModerator Sunday, April 1, 2012 8:14 PM
Thursday, January 5, 2012 12:15 PMModerator -
The using spaces are adding/importing namespaces so that you don't have to preface every object with it's individual namespace.
the :Iplugin means that the Patient class implements the Iplugin interface, which is the interface that must be used by all CRM plugins. This is to enforce that the correct things can be passed into the class and that it has a properly formed execute method and such which allows it to be called by the plugin execution pipeline. In short, it's what makes this class a plugin.
The execute method is the main method for every plugin and contains the code to be executed when the plugin is fired. The serviceProvider parameter which is an instance of IServiceProvider and implements it's interface contains all the info being passed to the plugin about what message it is attached to, what entity it is being fired from, that entities state, etc...
The context class is the main class that all LINQ works from in this case. It allows LINQ to act on items in CRM and is being instantiated from part of the service provider parameter that was passed in.
The IOrganizationServiceFactory Line is used to instantiate instances of a service into CRM that allows you to act on the CRM instance.
The last line shown is instantiating ITracingService which is used to debug plugins and pass out data from plugins. This is useful to find out what is going on in a plugin during execution. This is especially helpful for plugins in CRM Online because you cannot debug them line by line by attaching to the w3wp process like you can in on-premise.
I hope this helps!
Jamie Miley
Check out my about.me profile!
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Thursday, January 5, 2012 12:36 PM
- Marked as answer by Jamie MileyModerator Sunday, April 1, 2012 8:14 PM
Thursday, January 5, 2012 12:36 PMModerator -
Hi
Most of the comments are wrong.
Plugin is registered on custom entity named "new_employee"
The program is trying to get the new counter for the record.
here is simple pseudocode
if entity does not contain "new_employeeid1"
{
if entity does contain "new_subemployeeid"
{
get id of the retrieve all the record from "new_employee_autonumbering" related to the id
if the records returns then {
add one to number of records to get the new counter}
else {counter create a new counter}
Update the new_employee_autonumbering" entity with the new record.
update the new_employee entity's new_employeeid1 field to "EMP + counter"
}
}
I hope this helps.
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com- Edited by Amreek Singh Thursday, January 5, 2012 12:56 PM
- Proposed as answer by Amreek Singh Thursday, January 5, 2012 12:56 PM
- Marked as answer by Jamie MileyModerator Sunday, April 1, 2012 8:14 PM
Thursday, January 5, 2012 12:53 PM
All replies
-
Did you read through the comments? The comments are the lines that start with "//". This plugin looks like it is using LINQ to CRM to autonumber a Case. I am guessing this plugin is fired on-create of the entity.
Jamie Miley
Check out my about.me profile!
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Marked as answer by Jamie MileyModerator Sunday, April 1, 2012 8:14 PM
Thursday, January 5, 2012 12:15 PMModerator -
Thanks for replying Jamie...
Ya, this code is for autonumber only. I just want some brief explanation on few statements. I want to know how exactly the plugin works..
I have made required statements Bold...
Thanks in Advance...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk;
using System.Runtime;
using System.Data.Common;
using System.Data.Services;
using System.Data.Services.Common;
namespace AutoNumber{
public class Patient : IPlugin {
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));Thursday, January 5, 2012 12:27 PM -
The using spaces are adding/importing namespaces so that you don't have to preface every object with it's individual namespace.
the :Iplugin means that the Patient class implements the Iplugin interface, which is the interface that must be used by all CRM plugins. This is to enforce that the correct things can be passed into the class and that it has a properly formed execute method and such which allows it to be called by the plugin execution pipeline. In short, it's what makes this class a plugin.
The execute method is the main method for every plugin and contains the code to be executed when the plugin is fired. The serviceProvider parameter which is an instance of IServiceProvider and implements it's interface contains all the info being passed to the plugin about what message it is attached to, what entity it is being fired from, that entities state, etc...
The context class is the main class that all LINQ works from in this case. It allows LINQ to act on items in CRM and is being instantiated from part of the service provider parameter that was passed in.
The IOrganizationServiceFactory Line is used to instantiate instances of a service into CRM that allows you to act on the CRM instance.
The last line shown is instantiating ITracingService which is used to debug plugins and pass out data from plugins. This is useful to find out what is going on in a plugin during execution. This is especially helpful for plugins in CRM Online because you cannot debug them line by line by attaching to the w3wp process like you can in on-premise.
I hope this helps!
Jamie Miley
Check out my about.me profile!
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Thursday, January 5, 2012 12:36 PM
- Marked as answer by Jamie MileyModerator Sunday, April 1, 2012 8:14 PM
Thursday, January 5, 2012 12:36 PMModerator -
Hi
Most of the comments are wrong.
Plugin is registered on custom entity named "new_employee"
The program is trying to get the new counter for the record.
here is simple pseudocode
if entity does not contain "new_employeeid1"
{
if entity does contain "new_subemployeeid"
{
get id of the retrieve all the record from "new_employee_autonumbering" related to the id
if the records returns then {
add one to number of records to get the new counter}
else {counter create a new counter}
Update the new_employee_autonumbering" entity with the new record.
update the new_employee entity's new_employeeid1 field to "EMP + counter"
}
}
I hope this helps.
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com- Edited by Amreek Singh Thursday, January 5, 2012 12:56 PM
- Proposed as answer by Amreek Singh Thursday, January 5, 2012 12:56 PM
- Marked as answer by Jamie MileyModerator Sunday, April 1, 2012 8:14 PM
Thursday, January 5, 2012 12:53 PM