Microsoft CRM 2011 Plugins - Simple Issue

偶尔善良 提交于 2019-12-08 14:25:30

An error is being thrown because service is not yet defined. It needs to be defined before you can call service.Create.

The following is some code that I use for plugins that you might find useful. Seems a bit simpler than your example.

EDIT: I have modified the code to show a Create and an Update

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

Use the localContext parameter to get to the org service.

*<param name="localContext">The <see cref="LocalPluginContext"/> which contains the
        /// <see cref="IPluginExecutionContext"/>,
        /// <see cref="IOrganizationService"/>*
public class CreateRecord : IPlugin
    {
        /// <summary>
        /// Execute Method
        /// </summary>
        /// <param name="serviceProvider">IServiceProvider object.</param>
        public void Execute(IServiceProvider serviceProvider)
        {
            //Obtain the tracing service.
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            if (tracingService == null)
                throw new Exception("Unable to obtain tracing service.");

            //Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            if (context == null)
                throw new Exception("Unable to obtain Plugin Execution context.");

            //Obtain the organization service reference
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            if (serviceFactory == null)
                throw new Exception("Unable to obtain Organization service factory.");

            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            if (service == null)
                throw new Exception("Unable to obtain organization service.");

            Entity contact = new Entity("contact");
            contact["firstname"] = "Your First Name";
            contact["lastname"] = "Your Last Name";
            Guid contactId = service.Create(contact);
        }
    }  

Above code is a sample for creating a record, in this case contact in CRM. contactid should be holding the record which got created.You can locate the created record with other contacts under active contact view.

Hope this helps. Let me know if you need any more help on this. Happy learning !!! :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!