Пример #1
0
        public void ContactPublish()
        {
            ContactServiceClient client = new ContactServiceClient();

            client.Publish(new Toyota.Tsusho.CRM.API.MessageContracts.ContactPublishRequestMessage()
            {
                Contact = new Toyota.Tsusho.CRM.API.Contact()
                {
                    FirstName = "Test",
                    LastName = "Testing"
                }
            }
            );
        }
        /// <summary>
        /// Executes the plug-in.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</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>
        public void Execute(IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException("serviceProvider");
            }

            // Construct the Local plug-in context.
            LocalPluginContext localcontext = new LocalPluginContext(serviceProvider);

            localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Entered {0}.Execute()", this.ChildClassName));

            try
            {
                // Iterate over all of the expected registered events to ensure that the plugin
                // has been invoked by an expected event
                // For any given plug-in event at an instance in time, we would expect at most 1 result to match.
                Action<LocalPluginContext> entityAction =
                    (from a in this.RegisteredEvents
                     where (
                     a.Item1 == localcontext.PluginExecutionContext.Stage &&
                     a.Item2 == localcontext.PluginExecutionContext.MessageName &&
                     (string.IsNullOrWhiteSpace(a.Item3) ? true : a.Item3 == localcontext.PluginExecutionContext.PrimaryEntityName)
                     )
                     select a.Item4).FirstOrDefault();

                if (entityAction != null)
                {
                    localcontext.Trace(string.Format(
                        CultureInfo.InvariantCulture,
                        "{0} is firing for Entity: {1}, Message: {2}",
                        this.ChildClassName,
                        localcontext.PluginExecutionContext.PrimaryEntityName,
                        localcontext.PluginExecutionContext.MessageName));

                    entityAction.Invoke(localcontext);

                    ServiceContext context = new ServiceContext(localcontext.OrganizationService);

                    Contact contact = null;
                    ModificationEnumeration modification = ModificationEnumeration.Create;

                    // The InputParameters collection contains all the data passed in the message request.
                    if (localcontext.PluginExecutionContext.InputParameters.Contains("Target") &&
                        localcontext.PluginExecutionContext.InputParameters["Target"] is Entity)
                    {
                        // Obtain the target entity from the input parameters.
                        Entity entity = (Entity)localcontext.PluginExecutionContext.InputParameters["Target"];

                        contact = entity.ToEntity<Contact>();
                    }
                    else if (localcontext.PluginExecutionContext.InputParameters.Contains("Target") && localcontext.PluginExecutionContext.InputParameters["Target"] is EntityReference)
                    {
                        // Obtain the target entity from the input parameters.
                        EntityReference entity = (EntityReference)localcontext.PluginExecutionContext.InputParameters["Target"];

                        contact = (from c in context.ContactSet
                                   where c.Id == entity.Id
                                   select c).FirstOrDefault();

                        modification = ModificationEnumeration.Delete;
                    }

                    ContactServiceClient client = new ContactServiceClient();

                    client.Open();

                    try
                    {
                        client.Publish(new ContactPublishRequestMessage()
                        {
                            Contact = contact,
                            Modification = modification
                        });
                    }
                    catch
                    {
                        if(client.State == CommunicationState.Faulted)
                            client.Abort();

                        throw;
                    }
                    finally
                    {
                        if(client.State == CommunicationState.Opened)
                            client.Close();
                    }

                    // now exit - if the derived plug-in has incorrectly registered overlapping event registrations,
                    // guard against multiple executions.
                    return;
                }
            }
            catch (FaultException<OrganizationServiceFault> e)
            {
                localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exception: {0}", e.ToString()));

                // Handle the exception.
                throw;
            }
            finally
            {
                localcontext.Trace(string.Format(CultureInfo.InvariantCulture, "Exiting {0}.Execute()", this.ChildClassName));
            }
        }