Search

CRM 4 Plugin Template

using System;
using System.Collections.Generic;
using Microsoft.Win32;

// Microsoft Dynamics CRM namespaces
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.SdkTypeProxy.Metadata;

namespace My.Plugin
{
public class MyPlugin : IPlugin
{
// Configuration information that can be passed to a plug-in at run-time.
private string _secureInformation;
private string _unsecureInformation;

// Note: Due to caching, Microsoft Dynamics CRM does not invoke the plug-in 
// constructor every time the plug-in is executed.

// Related SDK topic: Writing the Plug-in Constructor
public MyPlugin(string unsecureInfo, string secureInfo)
{
_secureInformation = secureInfo;
_unsecureInformation = unsecureInfo;
}

// Related SDK topic: Writing a Plug-in
public void Execute(IPluginExecutionContext context)
{
DynamicEntity entity = null;

// Check if the InputParameters property bag contains a target
// of the current operation and that target is of type DynamicEntity.
if (context.InputParameters.Properties.Contains(ParameterName.Target) &&
context.InputParameters.Properties[ParameterName.Target] is DynamicEntity)
{
// Obtain the target business entity from the input parameters.
entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];

// TODO Test for an entity type and message supported by your plug-in.
// if (entity.Name != EntityName.account.ToString()) { return; }
// if (context.MessageName != MessageName.Create.ToString()) { return; }
}
else
{
return;
}

try
{
// Create a Microsoft Dynamics CRM Web service proxy.
// TODO Uncomment or comment out the appropriate statement.

// For a plug-in running in the child pipeline, use this statement.
// CrmService crmService = CreateCrmService(context, true);

// For a plug-in running in the parent pipeline, use this statement.
ICrmService crmService = context.CreateCrmService(true);

// TODO Plug-in business logic goes here.

}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw new InvalidPluginExecutionException(
String.Format("An error occurred in the {0} plug-in.",
this.GetType().ToString()),
ex);
}
}

#region Private methods
/// 
/// Creates a CrmService proxy for plug-ins that execute in the child pipeline.
/// 
/// The execution context that was passed to the plug-ins Execute method.
/// Set to True to use impersontation.
/// A CrmServce instance.
private CrmService CreateCrmService(IPluginExecutionContext context, Boolean flag)
{
CrmAuthenticationToken authToken = new CrmAuthenticationToken();
authToken.AuthenticationType = 0;
authToken.OrganizationName = context.OrganizationName;

// Include support for impersonation.
if (flag)
authToken.CallerId = context.UserId;
else
authToken.CallerId = context.InitiatingUserId;

CrmService service = new CrmService();
service.CrmAuthenticationTokenValue = authToken;
service.UseDefaultCredentials = true;

// Include support for infinite loop detection.
CorrelationToken corToken = new CorrelationToken();
corToken.CorrelationId = context.CorrelationId;
corToken.CorrelationUpdatedTime = context.CorrelationUpdatedTime;
corToken.Depth = context.Depth;

RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM");

service.Url = String.Concat(regkey.GetValue("ServerUrl").ToString(), "/2007/crmservice.asmx");
service.CorrelationTokenValue = corToken;

return service;
}

/// 
/// Creates a MetadataService proxy for plug-ins that execute in the child pipeline.
/// 
/// The execution context that was passed to the plug-ins Execute method.
/// Set to True to use impersontation.
/// A MetadataServce instance.
private MetadataService CreateMetadataService(IPluginExecutionContext context, Boolean flag)
{
CrmAuthenticationToken authToken = new CrmAuthenticationToken();
authToken.AuthenticationType = 0;
authToken.OrganizationName = context.OrganizationName;

// Include support for impersonation.
if (flag)
authToken.CallerId = context.UserId;
else
authToken.CallerId = context.InitiatingUserId;

MetadataService service = new MetadataService();
service.CrmAuthenticationTokenValue = authToken;
service.UseDefaultCredentials = true;

RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM");

service.Url = String.Concat(regkey.GetValue("ServerUrl").ToString(), "/2007/metadataservice.asmx");

return service;
}
#endregion Private Methods
}
}