I have been trying to upgrade this plugin from CRM 4.0 to 2016, the issue its related with
var crm = new CrmServiceContext();
and
crm.GetEntities...
Because it can not find CrmServiceContext and gets error therefore, all the content related with the variable crm gets to error. Thanks
error details (The type or namespace name 'CrmServiceContext' could not be found (are you missing a using directive or an assembly reference?))
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk; using Microsoft.VisualBasic; using System.ServiceModel; namespace newPluginsCRM2016 { public class CreateApplicationPlugin : IPlugin { //private string _secureInformation; //private string _unsecureInformation; public void Execute(IServiceProvider serviceProvider) { //Extract the tracing service for use in debugging sandboxed plug-ins. ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); // 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 entity = (Entity)context.InputParameters["Target"]; // Verify that the target entity represents an account. // If not, this plug-in was not registered correctly. if (entity.LogicalName != "new_application") return; try { ///////////////////////// Begin Business Logic //////////////////////// // Get appliation id Guid MyApplication = new Guid(context.OutputParameters["id"].ToString()); var crm = new CrmServiceContext(); // Get Application details var MyApplication = (from application in crm.GetEntities("new_application") where application.GetPropertyValue<Guid>("new_applicationid") == applicationId select application).Single(); // Get a list of all requirements for selected application type var applicationRequirements = from requirements in crm.GetEntities("new_requirement") join reqm2m in crm.GetEntities("new_applicationtype_requirement") on requirements.GetPropertyValue<Guid>("new_requirementid") equals reqm2m.GetPropertyValue<Guid>("new_requirementid") join applicationtypes in crm.GetEntities("new_applicationtype") on reqm2m.GetPropertyValue<Guid>("new_applicationtypeid") equals applicationtypes.GetPropertyValue<Guid>("new_applicationtypeid") where applicationtypes.GetPropertyValue<Guid>("new_applicationtypeid") == MyApplication.GetPropertyValue<Guid>("new_applicationtypeid") select requirements; // Add the requirements to the application foreach (var applicationRequirement in applicationRequirements) { var requirement = crm.CreateEntity("new_applicationrequirement"); requirement.SetPropertyValue("new_requirementid", applicationRequirement.GetPropertyValue<Guid>("new_requirementid")); requirement.SetPropertyValue("new_applicationid", applicationId); requirement.SetPropertyValue("new_contactid", MyApplication.GetPropertyValue<Guid>("new_contactid")); crm.AddObject("new_applicationrequirement", requirement); crm.SaveChanges(); } // Get a list of all application fees for selected application type //CacheManager.GetBaseCache().Remove("adxdependency:crm:entity:new_fee"); var applicationFees = from fees in crm.GetEntities("new_fee") join feetypem2m in crm.GetEntities("new_applicationtype_feetype") on fees.GetPropertyValue<Guid>("new_feetypeid") equals feetypem2m.GetPropertyValue<Guid>("new_feetypeid") join applicationtypes in crm.GetEntities("new_applicationtype") on feetypem2m.GetPropertyValue<Guid>("new_applicationtypeid") equals applicationtypes.GetPropertyValue<Guid>("new_applicationtypeid") where applicationtypes.GetPropertyValue<Guid>("new_applicationtypeid") == MyApplication.GetPropertyValue<Guid>("new_applicationtypeid") where fees.GetPropertyValue<Guid>("new_renewalyearid") == MyApplication.GetPropertyValue<Guid>("new_renewalyearid") select fees; // Add the fees to the application int NumMonths = MyApplication.GetPropertyValue<Int32>("new_numberofmonths"); foreach (var applicationFee in applicationFees) { var quantity = 1; decimal? amount = 0; if (applicationFee.GetPropertyValue<int>("new_defaultunit") == 2) //Month { quantity = NumMonths; } //throw new InvalidPluginExecutionException(applicationFee.GetPropertyValue<Decimal>("new_proratedamount").ToString()); if (MyApplication.GetPropertyValue<DateTime>("new_effectivedate") != new DateTime(1, 1, 1) && (MyApplication.GetPropertyValue<DateTime>("new_effectivedate").Month >= 9 || MyApplication.GetPropertyValue<DateTime>("new_effectivedate").Month < 3) && applicationFee.GetPropertyValue<Decimal>("new_proratedamount") > 0) { amount = applicationFee.GetPropertyValue<Decimal>("new_proratedamount"); } else { amount = applicationFee.GetPropertyValue<Decimal>("new_amount"); } var fee = crm.CreateEntity("new_applicationfee"); fee.SetPropertyValue("new_applicationid", applicationId); fee.SetPropertyValue("new_feeid", applicationFee.GetPropertyValue<Guid>("new_feeid")); fee.SetPropertyValue("new_quantity", quantity); fee.SetPropertyValue("new_amount", amount, typeof(CrmMoney)); fee.SetPropertyValue("new_unit", applicationFee.GetPropertyValue<int>("new_defaultunit")); fee.SetPropertyValue("new_total", quantity * amount); crm.AddObject("new_applicationfee", fee); crm.SaveChanges(); } ///////////////////////// End Business Logic ////////////////////////// } catch (FaultException<OrganizationServiceFault> ex) { throw new InvalidPluginExecutionException("An error occurred in the newPluginsCRM2016 plug-in.", ex); } catch (Exception ex) { tracingService.Trace("CreateApplicationPlugin: {0}", ex.ToString()); throw; } } } } }