Mscrm 2011 Plugin
basic code snippsets:
Code to extract
service objects:
IPluginExecutionContext
context =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//
Get a reference to the organization service.
IOrganizationServiceFactory factory
=
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service =
factory.CreateOrganizationService(context.UserId);
//
Get a reference to the tracing service.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
Getting input
parameters:
if (context.InputParameters.Contains("Target")
&&
context.InputParameters["Target"] is Entity)
{
//
Obtain the target entity from the input parmameters.
Entity entity =
(Entity)context.InputParameters["Target"];
Shared variable:
context.SharedVariables.Add("PrimaryContact",
(Object)contact.ToString());
if (context.SharedVariables.Contains("PrimaryContact"))
{
Guid contact =
new Guid((string)context.SharedVariables["PrimaryContact"]);
// Do something with the contact.
}
Image reading:
if (context.PreEntityImages.Contains("PreImage")
&&
|
12
|
context.PreEntityImages["PreImage"]
is Entity)
|
|
|
|
13
|
{
|
14
|
preMessageImage
= (Entity)context.PreEntityImages["PreImage"];
|
Query expression
class:
// Create query using querybyattribute
QueryByAttribute querybyexpression = new QueryByAttribute("account");
querybyexpression.ColumnSet = new ColumnSet("name", "address1_city", "emailaddress1");
// Attribute to query
querybyexpression.Attributes.AddRange("address1_city");
// Value of queried attribute to return
querybyexpression.Values.AddRange("Detroit");
// Query passed to the service proxy
EntityCollection retrieved =
_serviceProxy.RetrieveMultiple(querybyexpression);
// Iterate through returned collection
foreach (var c in retrieved.Entities)
{
System.Console.WriteLine("Name: " +
c.Attributes["name"]);
System.Console.WriteLine("Address: " +
c.Attributes["address1_city"]);
System.Console.WriteLine("E-mail: " +
c.Attributes["emailaddress1"]);
}
-___________________________X___________________________
// Query using ConditionExpression and FilterExpression
ConditionExpression
condition1 = new ConditionExpression();
condition1.AttributeName
= "lastname";
condition1.Operator =
ConditionOperator.Equal;
condition1.Values.Add("Brown");
FilterExpression filter1
= new
FilterExpression();
filter1.Conditions.Add(condition1);
QueryExpression query = new QueryExpression("contact");
query.ColumnSet.AddColumns("firstname", "lastname");
query.Criteria.AddFilter(filter1);
EntityCollection result1
= _serviceProxy.RetrieveMultiple(query);
_________________x________________
QueryExpression
contactquery = new QueryExpression
{
EntityName="contact",
ColumnSet = new ColumnSet("firstname", "lastname", "contactid")
};