我正在尝试编写一个插件来在Microsoft CRM 2011中创建一个新的联系人实体。我还没有在网上找到任何有用的信息,我觉得我一直在抨击我的头一整天都是砖墙。我在下面发布的代码给了我一个错误,说“名称'服务'在当前上下文中不存在”。有谁能告诉我发生了什么事吗?Microsoft CRM 2011插件 - 简单问题
// <copyright file="PreValidateContactCreate.cs" company="">
// Copyright (c) 2013 All Rights Reserved
// </copyright>
// <author></author>
// <date>8/6/2013 4:22:10 PM</date>
// <summary>Implements the PreValidateContactCreate Plugin.</summary>
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
// </auto-generated>
namespace Plugins1
{
using System;
using System.ServiceModel;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
/// <summary>
/// PreValidateContactCreate Plugin.
/// </summary>
public class PreValidateContactCreate: Plugin
{
/// <summary>
/// Initializes a new instance of the <see cref="PreValidateContactCreate"/> class.
/// </summary>
public PreValidateContactCreate()
: base(typeof(PreValidateContactCreate))
{
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(10, "Create", "contact", new Action<LocalPluginContext>(ExecutePreValidateContactCreate)));
// 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>
protected void ExecutePreValidateContactCreate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
// TODO: Implement your custom Plug-in business logic.
Entity Contact = new Entity("contact");
Contact.Attributes["firstname"] = "SomeName";
Contact.Attributes["lastname"] = "SomeSurname";
service.Create(Contact);
}
}
}
非常感谢您的回复。我试过这段代码,它似乎没有创建记录。你(或其他人)有任何示例代码可以显示如何创建记录吗? – user2463758
你怎么注册你的插件步骤?你能否提供关于何时创建联系人的更多细节?我会调查我的回应并更新。 – Campey
因为它是全部注册的,它在运行时不会报错,但没有创建联系。创建联系人只是一个训练练习,因为我是一个完全新手。 很抱歉让你心烦,但相反,如果你能与我分享一些代码,获取一个字段的值,在其刚刚创造了一项纪录,然后更新与其他一些价值这一领域,这将是惊人。 非常感谢您的帮助 – user2463758