Category Archives: Plugins

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;
            service.Update(sitev);             
        }

Advertisements

Query N to N relationship

Having and N to N relationship between points and parameters, from a point ID we want to retrieve all the related parameters.


 EntityCollection getEPParameters(IOrganizationService serv, Guid POINTid)
        {
            QueryExpression query = new QueryExpression()
            {   //Parameters entity
                EntityName = "mile_emissionpointparameters",
                ColumnSet = new ColumnSet(true),
                LinkEntities = 
                        {
                            new LinkEntity
                            {
                                LinkFromEntityName = "mile_emissionpointparameters", //Parameters table
                                LinkFromAttributeName = "mile_emissionpointparametersid", //Parameters table id
                                LinkToEntityName = "mike_mile_emissionpoint_mile_emissionpointpara", // N to N entity 
                                LinkToAttributeName = "mile_emissionpointparametersid", 
                                LinkCriteria = new FilterExpression
                                {
                                    FilterOperator = LogicalOperator.And,
                                    Conditions = 
                                    {
                                        new ConditionExpression
                                        {
                                            AttributeName = "mile_emissionpointid",
                                            Operator = ConditionOperator.Equal,
                                            Values = { POINTid }  // Point id
                                        }
                                    }
                                }
                            }
                        }
            };

            // Obtain results from the query expression.
            return serv.RetrieveMultiple(query);

        }

If what we need is to get a list of entities filtered by some values of the other entities in the N to N relationship we just need to use nested LinkEntities. Imagine we have an N 2 N between control measures and eventRevisions. We want to get all the measures related to any open – semiopen (L_status 3 or 4) revisions.


QueryExpression objQueryExpression = new QueryExpression("lema_controlmeasure");
objQueryExpression.ColumnSet = new ColumnSet(true);
objQueryExpression.Distinct = true;

// Add the filter using the N to N middle table 
LinkEntity linkEntity1 = new LinkEntity();
linkEntity1.JoinOperator = JoinOperator.Inner;
linkEntity1.LinkFromEntityName = "L_controlmeasure";
linkEntity1.LinkFromAttributeName = "L_controlmeasureid";
linkEntity1.LinkToEntityName = "L_controlmeasure_hazardouseventrevision";
linkEntity1.LinkToAttributeName = "L_controlmeasureid";

// To be able to do a double Link we use another LinkEntity within the the original linked Entity
LinkEntity InnerLinkEntity = new LinkEntity();
InnerLinkEntity.JoinOperator = JoinOperator.Inner;
InnerLinkEntity.LinkFromEntityName = "L_controlmeasure_hazardouseventrevision";
InnerLinkEntity.LinkFromAttributeName = "L_hazardouseventrevisionid";
InnerLinkEntity.LinkToEntityName = "L_hazardouseventrevision";
InnerLinkEntity.LinkToAttributeName = "L_hazardouseventrevisionid";

// Filter of the inner Join by a value of the second entity of the N 2 N
InnerLinkEntity.LinkCriteria.AddCondition("L_status", ConditionOperator.In, 3, 4);

//Add this link to the original link
linkEntity1.LinkEntities.Add(InnerLinkEntity);

objQueryExpression.LinkEntities.Add(linkEntity1);   

EntityCollection results = _service.RetrieveMultiple(objQueryExpression);

Following this idea of having a InnerLinkEntity, we could add more innerLinkEntities being able to query N relationships at once!

I hope it was helpfull!!!

Cheers,

Conecting to WS from Plugins using EarlyBinding

Working for one of my clients i had to develope a plugin that could connect to a Web Service to send SMS´s to some customers depending on some entity values.

I archived these goal by creating a HTTP Binding to the Web service.

BasicHttpBinding myBinding = new BasicHttpBinding();

myBinding.Name = "WsSanPortBinding";
myBinding.Security.Mode = BasicHttpSecurityMode.None;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
myBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
myBinding.UseDefaultWebProxy = false;
EndpointAddress endPointAddress = new EndpointAddress("http://server/sms-webapp/sms?wsdl");

WsSanClient wsc = new WsSanClient(myBinding, endPointAddress);

wsc.WebServiceMethod(data);