Plugins Microsoft CRM 2011插件-简单问题
我正在尝试编写一个插件,在MicrosoftCRM 2011中创建一个新的联系人实体。我还没有在网上找到任何有用的信息,我觉得我整天都在碰壁。我在下面发布的代码给了我一个错误:“名称‘服务’在当前上下文中不存在”。有人能告诉我发生了什么事吗Plugins Microsoft CRM 2011插件-简单问题,plugins,dynamics-crm-2011,Plugins,Dynamics Crm 2011,我正在尝试编写一个插件,在MicrosoftCRM 2011中创建一个新的联系人实体。我还没有在网上找到任何有用的信息,我觉得我整天都在碰壁。我在下面发布的代码给了我一个错误:“名称‘服务’在当前上下文中不存在”。有人能告诉我发生了什么事吗 // //版权所有(c)2013保留所有权利 // // //2013年8月6日下午4:22:10 //实现PreValidateContactCreate插件。 // //这段代码是由一个工具生成的。 //运行时版本:4.0.30319.1 //
//
//版权所有(c)2013保留所有权利
//
//
//2013年8月6日下午4:22:10
//实现PreValidateContactCreate插件。
//
//这段代码是由一个工具生成的。
//运行时版本:4.0.30319.1
//
名称空间插件1
{
使用制度;
使用System.ServiceModel;
使用System.Collections.Generic;
使用Microsoft.Xrm.Sdk;
使用Microsoft.Crm.Sdk.Messages;
使用Microsoft.Xrm.Sdk.Client;
使用Microsoft.Xrm.Sdk.Discovery;
使用Microsoft.Xrm.Sdk.Metadata;
使用Microsoft.Xrm.Sdk.Query;
///
///PreValidateContactCreate插件。
///
公共类PreValidateContactCreate:插件
{
///
///初始化类的新实例。
///
public PreValidateContactCreate()
:base(typeof(PreValidateContactCreate))
{
Add(新元组(10,“创建”,“联系”,新操作(ExecutePreValidateContactCreate));
//注意:如果此插件不是特定于单个实体和消息组合,则可以在此处注册更多事件。
//您可能还需要更新RegisterFile.crmregister插件注册文件以反映任何更改。
}
///
///执行插件。
///
///包含
/// ,
///
///及
///
///
///为了提高性能,Microsoft Dynamics CRM缓存插件实例。
///插件的Execute方法应该写为无状态的构造函数
///不是每次调用插件都调用。此外,多个系统线程
///无法同时执行插件。所有每次调用的状态信息
///存储在上下文中。这意味着您不应该在插件中使用全局变量。
///
受保护的void ExecutePreValidateContactCreate(LocalPluginContext localContext)
{
if(localContext==null)
{
抛出新ArgumentNullException(“localContext”);
}
//TODO:实现自定义插件业务逻辑。
实体联系人=新实体(“联系人”);
Contact.Attributes[“firstname”]=“SomeName”;
Contact.Attributes[“lastname”]=“some姓氏”;
服务。创建(联系人);
}
}
}
使用localContext参数访问组织服务
*<param name="localContext">The <see cref="LocalPluginContext"/> which contains the
/// <see cref="IPluginExecutionContext"/>,
/// <see cref="IOrganizationService"/>*
*包含
/// ,
/// *
使用localContext参数访问组织服务
*<param name="localContext">The <see cref="LocalPluginContext"/> which contains the
/// <see cref="IPluginExecutionContext"/>,
/// <see cref="IOrganizationService"/>*
*包含
/// ,
/// *
由于尚未定义服务,正在引发错误。需要先定义它,然后才能调用service.Create
下面是一些我用于插件的代码,您可能会发现这些代码很有用。看起来比你的例子简单一点
编辑:我修改了代码以显示创建和更新
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
namespace PluginSample
{
public class ContactSample : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Get the context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
try
{
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
if (context.MessageName == "Create")
{
// Creates a contact
Entity contact = new Entity("contact");
contact.Attributes.Add("firstname", "SomeName");
contact.Attributes.Add("lastname", "SomeSurname");
service.Create(contact);
}
else if (context.MessageName == "Update")
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Update contact
Entity contact = new Entity("contact");
contact.Id = new Guid("EBFB262C-5EFF-E211-8BEB-1CC1DEEAE7EC");
contact.Attributes.Add("firstname", "Name Changed");
service.Update(contact);
}
}
}
catch (Exception generalException)
{
throw new InvalidPluginExecutionException("Plugin Failed - Execute :-(", generalException);
}
}
}
}
由于尚未定义服务,正在引发错误。需要先定义它,然后才能调用service.Create 下面是一些我用于插件的代码,您可能会发现这些代码很有用。看起来比你的例子简单一点
编辑:我修改了代码以显示创建和更新
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
namespace PluginSample
{
public class ContactSample : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Get the context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
try
{
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
if (context.MessageName == "Create")
{
// Creates a contact
Entity contact = new Entity("contact");
contact.Attributes.Add("firstname", "SomeName");
contact.Attributes.Add("lastname", "SomeSurname");
service.Create(contact);
}
else if (context.MessageName == "Update")
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Update contact
Entity contact = new Entity("contact");
contact.Id = new Guid("EBFB262C-5EFF-E211-8BEB-1CC1DEEAE7EC");
contact.Attributes.Add("firstname", "Name Changed");
service.Update(contact);
}
}
}
catch (Exception generalException)
{
throw new InvalidPluginExecutionException("Plugin Failed - Execute :-(", generalException);
}
}
}
}
public类CreateRecord:IPlugin
{
///
///执行方法
///
///IServiceProvider对象。
public void Execute(IServiceProvider服务提供程序)
{
//获取跟踪服务。
ITracingService tracingService=(ITracingService)serviceProvider.GetService(类型(ITracingService));
如果(tracingService==null)
抛出新异常(“无法获取跟踪服务”);
//从服务提供程序获取执行上下文。
IPluginExecutionContext上下文=(IPluginExecutionContext)serviceProvider.GetService(类型为(IPluginExecutionContext));
if(上下文==null)
抛出新异常(“无法获取插件执行上下文”);
//获取组织服务参考
IOOrganizationServiceFactory serviceFactory=(IOOrganizationServiceFactory)serviceProvider.GetService(类型为(IOOrganizationServiceFactory));
if(serviceFactory==null)
抛出新异常(“无法获取组织服务工厂”);
IOOrganizationService=serviceFactory.CreateOrganizationService(context.UserId);
if(服务==null)
抛出新异常(“无法获取组织服务”);
实体联系人=新实体(“联系人”);
联系人[“名字”]=“您的名字”;
联系人[“姓氏”]=“您的姓氏”;
Guid contactId=服务。创建(联系人);
}
}
上面的代码是创建记录的示例,在本例中是CRM中的联系人。contactid应该保存已创建的记录。您可以在“活动联系人”视图下找到与其他联系人一起创建的记录
希望这有帮助。如果你需要更多的帮助,请告诉我。快乐学习!!!:) public类CreateRecord:IPlugin
{
///
///执行方法
///
///IServiceProvider对象。
public void Execute(IServiceProvider服务提供程序)
{
//获取跟踪服务。
ITracingService tracingService=(ITracingService)serviceProvider.GetService(类型(ITracingService));