Crud Operations in Ms crm Using Plugin
If anyone want to work on crud operation then we need to know some basic requirements for the crud operations.
We need to know the get and set in the plugin, if you know these, then we can work easily in plugins.
After learning get and set in plugin we can work easily.
Crud operations means create a record, retrieve the record, update a record and delete the record.
in this post we discuss about creation of the record.
- if we want to create a record fist create a project with class library in visual studio
- While creating give the proper project name and known location
- After that it will create on default class with class1 name.
- in solution, select the class name and rename the class name with proper name like cresting on account or contact without spaces.
- After that set the class as public.
- Go to project and right click and select the manage nugget package.
- Search for the Xrm.SDK in browse section, and download the
- Download these pacages into your project.
- Check below refrences are added or not

Add Interface of Iplugin to your Class
11. Add the below common code to all the plugins.
//Getting the plugin context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//creating service facotry to plugin
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
//getting the login user details and secuirty based on this we can execute the code
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
//tracing the plugin
ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
After adding the code we need to check the target entity is entity or not
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
//write logic here
} catch (Exception ex){
throw new InvalidPluginExecutionException(ex.Message);
//catch the any error in the try block
}
}
After adding the above code to in class we can write require logic in the try block
CREATE OF RECORD:
If we want to create a record then use the Entity class to create a record
Entity Objectname = new Entity("Schema name of your Entity");
By using object name we can create the record by passing the schema name of the fields
Objectname ["Schema name of the field"]= Value Based on data type of the filed
I tried below code to creatimg the record.
Employsdata["crc78_name"] = "this is from plugin";
Employsdata["crc78_adhranumber"] = "3456g";
Employsdata["crc78_employ_id"] = 34;
Employsdata["crc78_pannumber"] = "ddfb2311g";
// by using service object we can call the service call
service.Create(Employsdata);
By following this we can create our fist plugin as Create of the record.
Updating the Record using Plugin in MS Crm
Easy to update the record using plugin.
Updating also same as the creating, Most of the code is common as creating record.
This code is for the reading the parent phone number from child entity lookup and updating the record in Child Entity.
//I am targeting the Child entity and create object for the child entity
Entity Employdata =(Entity) context.InputParameters["Target"];
//I am updating the phone number in the entity
Employdata["crc78_phinenumber"] = 123567890;
service.Update(Employdata);
tracer.Trace("updated");
Deleting the record using plugin
Deleting the record is easy we need to pass the guid of the record and Entity logical name. By passing these two we can delete our record using plugin
//I create separate plugin to deltet the record that why i created target object
Entity Employdata = (Entity)context.InputParameters["Target"];
// i am getting the Guid of the Target record
Guid iddata = Employdata.Id;
//I am reading the logical name of the record
string nameentity = Employdata.LogicalName;
// i am passing the Entity logical name and guid of the record
service.Delete(nameentity, iddata);
tracer.Trace("data is deleted");
Retrieving the records
In this we have two type
1. Retrieve the singel record data
For the singel record Retrieve, we need to pass the logical name of the entity, Guid of the record and columns list.
Entity record = service.Retrieve("Schema name of the entity", "Record Guid", new ColumnSet(true));
Columns set we can pass the either true or selected Fileds
Columnset(true): It means getting the all the fields data in the record.
Columnset("Pass the schemana name of the selected fileds by passing the comma separate")
ColumnSet("crc78_counting","crc78_counting");
By using record Entity object name we can get the all the fileds data based on data type
2. Retriving the multple record data.
This is for the getting the multiple record data.
For this we have two types to get the data
1. Fetchxml
Entity Companydata = (Entity)context.InputParameters["Target"];
string phonenumber = Companydata.Contains("crc78_phonenumber") ? (string)Companydata.Attributes["crc78_phonenumber"] : null;
We need to download the fetch xml from the advacne find, no need to write the code
Formating c# fetch xml we need to fetch xml formatter online. Copay the xml and paste in above space.
string queryfetch = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='crc78_employ'>" +
"<attribute name='crc78_employid' />" +
"<attribute name='crc78_name' />" +
"<attribute name='createdon' />" +
"<order attribute='crc78_name' descending='false' />" +
"<filter type='and'>" +
"<condition attribute='crc78_companyname' operator='eq' uitype='crc78_company' value='" + Companydata.id+ "' />" +
"</filter>" +
"</entity>" +
"</fetch>";
//Here we getting the multiple records so we are using entity collection to store the data
EntityCollection Employslist = service.RetrieveMultiple(new FetchExpression(queryfetch));
//By using for each we are reading the each record and updating the record with parent phone number
foreach (var employs in Employslist.Entities)
{
//updating the phone number
employs["crc78_phinenumber"] = phonenumber;
service.Update(employs);
tracer.Trace("data setted");
}
2.Query Expression
QueryExpression Querys = new QueryExpression("crc78_employ")
{
//Mention required columns or set as true
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("crc78_companyname", ConditionOperator.Equal, Companyid)
}
}
};
Common for the both fetch xml and query expression
EntityCollection Employslists = service.RetrieveMultiple(Querys);
foreach (var employs in Employslists.Entities)
{
employs["crc78_phinenumber"] = phonenumber;
service.Update(employs);
tracer.Trace("data setted from query experssion");
}
Thanks for reading my post. Post Your comment and share to your friends.
Follow The blog for the latest Posts.
Comments
Post a Comment