1

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);
        }
    }
}
4

3 に答える 3

0

localContextパラメータを使用して組織サービスにアクセスします。

*<param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>*
于 2013-08-06T18:31:16.867 に答える