using System;
using Microsoft.Xrm.Sdk;
namespace ASK1025Populatefullname
{
public class PopulateFullname : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the tracing service
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.Depth > 1)
{
return;
}
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity SourceData = (Entity)context.InputParameters["Target"];//syntax for post image
//if we use image use this to get data insted of using sourcedata
//remember when we set use only sourcedata
// Obtain the IOrganizationService instance which you will need for
// web service calls.
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
string firstName = " ";
string lastName = " ";
if (context.MessageName.ToLower() == "create")
{
// Plug-in business logic goes here.
if (SourceData.Contains("sdc_firstname") && SourceData["sdc_firstname"] != null)
{
firstName = (string)SourceData["sdc_firstname"];
}
//----get syntax for plugin --single line filed
if (SourceData.Contains("sdc_secondname") && SourceData["sdc_secondname"] != null)
{
lastName = (string)SourceData["sdc_secondname"];
}
//--fullname
string fullname = firstName + " " + lastName;
//--set syntax for plugin --single line of syntax
SourceData["sdc_name"] = fullname;
}
else
{
Entity postValues = (Entity)context.PostEntityImages["latestvalues"];
// Plug-in business logic goes here.
if (postValues.Contains("sdc_firstname") && postValues["sdc_firstname"] != null)
{
firstName = (string)postValues["sdc_firstname"];
}
//----get syntax for plugin --single line filed
if (postValues.Contains("sdc_secondname") && postValues["sdc_secondname"] != null)
{
lastName = (string)postValues["sdc_secondname"];
}
//--fullname
string fullname = firstName + " " + lastName;
//--set syntax for plugin --single line of syntax
//SourceData["sdc_name"] = fullname;
Entity entUpdate = new Entity(); // creating new object
entUpdate.LogicalName = SourceData.LogicalName; // we need to give proper instructions which entity we need to update
entUpdate.Id = SourceData.Id; //we will have multiple records in entity.. so we need to update guid of record that we need to update
entUpdate["sdc_name"] = fullname; // in record we have multiple fileds so we need to..set which filed we need to update
service.Update(entUpdate); //send data to server
}
}
catch (Exception ex)
{
tracingService.Trace("PopulateFullname: {0}", ex.ToString());
throw;
}
}
}
}
}