Hi All,
I am trying to replicate following blog for account entity.
http://blog.developers.ba/develop-create-update-plugin-dynamics-crm-2013/
But I am getting following Errors.
Missing assembly refernce and Could not copy file path.dll beacuse it was not found.
Please find my code and assembly refnce below and let me know what I am missing?
// <copyright file="PostAccountCreate.cs" company="IBM">
// Copyright (c) 2016 All Rights Reserved
// </copyright>
// <author>IBM</author>
// <date>4/20/2016 3:00:55 PM</date>
// <summary>Implements the PostAccountCreate Plugin.</summary>
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
// </auto-generated>
using
System;
using
System.ServiceModel;
using
Microsoft.Xrm.Sdk;
using
Microsoft.Xrm.Sdk.Client;
using
Microsoft.Xrm.Sdk.Query;
using
System.Net;
using
System.Text;
using
System.IO;
using
System.Globalization;
using
Microsoft.Crm.Sdk;
using
System.Collections.ObjectModel;
using
System.Globalization;
using
System.Linq;
namespace
CrmVSSolution3.Plugins
{
///<summary>
/// PostAccountCreate Plugin.
///</summary>
publicclassPostAccountCreate: Plugin
{
///<summary>
/// Initializes a new instance of the <see cref="PostAccountCreate"/> class.
///</summary>
public PostAccountCreate()
:
base(typeof(PostAccountCreate))
{
base.RegisteredEvents.Add(newTuple<int, string, string, Action<LocalPluginContext>>(40, "Create", "account", newAction<LocalPluginContext>(ExecutePostAccountCreate)));
// Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
// You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
}
///<summary>
/// Executes the plug-in.
///</summary>
///<param name="localContext">The <see cref="LocalPluginContext"/> which contains the
///<see cref="IPluginExecutionContext"/>,
///<see cref="IOrganizationService"/>
/// and <see cref="ITracingService"/>
///</param>
///<remarks>
/// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
/// The plug-in's Execute method should be written to be stateless as the constructor
/// is not called for every invocation of the plug-in. Also, multiple system threads
/// could execute the plug-in at the same time. All per invocation state information
/// is stored in the context. This means that you should not use global variables in plug-ins.
///</remarks>
//create plugin for CRM 2013
//To generate “Insert / Create plugin” choose pipeline stage Post-Operation and message Create.
//create plugin for entity Account,which contains optionset and lookup field.
//This Create Plugin is used to call our REST Web API to exchange data between CRM and our third party system.
protectedvoid ExecutePostAccountCreate(LocalPluginContext localContext)
{
if (localContext == null)
{
thrownewArgumentNullException("localContext");
}
// TODO: Implement your custom Plug-in business logic.
if (localContext == null)
{
thrownewArgumentNullException("localContext");
}
// TODO: Implement your custom Plug-in business logic.
var context = localContext.PluginExecutionContext;
// if user is our web api user return to prevent recursive call
if (context.InitiatingUserId.ToString().Equals("2f2c9848-194a-e411-80cc-00155d0c03cf"))
{
return;
}
IOrganizationService service = localContext.OrganizationService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] isEntity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "account")
{
try
{
if (entity.Attributes.Contains("name") && entity.Attributes.Contains("accountid") && entity.Attributes.Contains("territorycode") && entity.Attributes.Contains("territoryid"))
{
CustomAccount d =
new CustomAccount();
d.Terr_Name = (
string)entity.Attributes["territorycode"];
d.Terr_Id = (
string)entity.Attributes["territoryid"];
// here we grab optionset value
d.Terr_Name = entity.GetAttributeValue<
OptionSetValue>("territorycode").Value.ToString();
// here we grab lookup value for field region
d.Terr_Id = (
string)entity.Attributes["territoryid"];
// here we call our external web api and pass municipality entity to be created
var customAccountRepository = new CustomAccountRepository();
var result = customAccountRepository.Insert(d);
if (!string.IsNullOrWhiteSpace(result))
{
thrownewInvalidPluginExecutionException("Error with calling Web API occured: " + result);
}
// We do not call service to update entity becouse no data in entity is changed. If we need to change entity fields before save do so like this://
// entity.Attributes["name"] = "My new name of Account";
// and then call service to update previously created entity //
// service.Update(entity);
}
else
{
thrownewInvalidPluginExecutionException("Some required field missing");
}
}
catch (FaultException ex)
{
thrownewInvalidPluginExecutionException("An error occured in create plugin: " + ex.ToString());
}
catch (Exception ex)
{
thrownewInvalidPluginExecutionException("An error occured in create plugin: " + ex.ToString());
}
}
else
{
thrownewInvalidPluginExecutionException("Entity does not exists");
}
}
}
}
}
Refernces :
Microsoft.Crm.Sdk.Proxy
Microsft.CSharp
Microsft.Xrm.Sdk
System.ServiceModel