Creating Associate Plugin

The Plugin needs to be registered on Post Operation of the Associate Plugin.

Associate Plugin

Associate Plugin

From the Context variable we can obtain the target entity and the associated entities.

namespace Lema.SiteVisits.Plugins
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using Microsoft.Crm.Sdk;
    using Microsoft.Xrm.Sdk;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Metadata;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Sdk.Client;

    public class PostSiteCategoryAssociate: IPlugin
        public void Execute(IServiceProvider serviceProvider)
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));          

            if (!context.InputParameters.Contains("Relationship")) { return; }
            Relationship relationship = (Relationship)context.InputParameters["Relationship"];
            if (relationship.SchemaName != "mike_mike_sitevisit_mike_sitevisittype") { return; }
            if (!context.InputParameters.Contains("Target")) { return; }
            EntityReference target = (EntityReference)context.InputParameters["Target"];
            if (!context.InputParameters.Contains("RelatedEntities")) { return; }
            EntityReferenceCollection related = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];

            //Relationship is a reference to the relationship which this association is working against. i.e. mike_mike_sitevisit_mike_sitevisittype
            //Target is a reference to the primary Entity – i.e. Site Visit
            //RelatedEntities is a set of references pointing to the records to associate with – i.e. site Visits Type

            Guid SiteId = (Guid)target.Id;
            Entity sitev = service.Retrieve("mike_sitevisit", SiteId, new ColumnSet(true));

            string typenames = string.Empty;
            foreach (EntityReference sitetype in related)
                typenames += sitetype.Name + ";";        
            sitev.Attributes["mike_summary"] = typenames;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s