Sunday, April 29, 2012

Extending CrmSvcUtil part 2

Last month, I wrote a blog on how to extend CrmSvcUtil. The blog was about filtering the classes generated by CrmSvcUtil. Here is the link to that blog. I have updated the code to generate the early bind classes for a specific solution. Here is the code..
//<snippetBasicFilteringService>

using System;
using Microsoft.Crm.Services.Utility;
using Microsoft.Xrm.Sdk.Metadata;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Client;
using System.Xml;
using System.Xml.Linq;
namespace CRMExtensions
{
    /// <summary>
    /// Sample extension for the CrmSvcUtil.exe tool that generates early-bound
    /// classes for custom entities.
    /// </summary>
    public sealed class BasicFilteringService : ICodeWriterFilterService
    {
        //To store the guids belongs to a specified 
        public List<Guid> li = new List<Guid>();
       
        //Put the uniquename of the solution here
        public string sSolution = "ActivityFeeds";

        public BasicFilteringService(ICodeWriterFilterService defaultService)
        {
            this.DefaultService = defaultService;

            // Call the function to generate the the guids for the solution
            getDefaultEntities();

        }

        private ICodeWriterFilterService DefaultService { get; set; }

        bool ICodeWriterFilterService.GenerateAttribute(AttributeMetadata attributeMetadata, IServiceProvider services)
        {
            return this.DefaultService.GenerateAttribute(attributeMetadata, services);
        }

        bool ICodeWriterFilterService.GenerateEntity(EntityMetadata entityMetadata, IServiceProvider services)
        {
            //if (!entityMetadata.IsCustomEntity.GetValueOrDefault()) { return false; }
            //if (entityMetadata.LogicalName!="account") { return false; }
            if (li.Contains(entityMetadata.MetadataId.Value)==false) { return false; }
            
            return this.DefaultService.GenerateEntity(entityMetadata, services);
        }

        bool ICodeWriterFilterService.GenerateOption(OptionMetadata optionMetadata, IServiceProvider services)
        {
            return this.DefaultService.GenerateOption(optionMetadata, services);
        }

        bool ICodeWriterFilterService.GenerateOptionSet(OptionSetMetadataBase optionSetMetadata, IServiceProvider services)
        {
            return this.DefaultService.GenerateOptionSet(optionSetMetadata, services);
        }

        bool ICodeWriterFilterService.GenerateRelationship(RelationshipMetadataBase relationshipMetadata, EntityMetadata otherEntityMetadata,
        IServiceProvider services)
        {
            return this.DefaultService.GenerateRelationship(relationshipMetadata, otherEntityMetadata, services);
        }

        bool ICodeWriterFilterService.GenerateServiceContext(IServiceProvider services)
        {
            return this.DefaultService.GenerateServiceContext(services);
        }

        // This function will add guids of solution entities
        public void getDefaultEntities()
        {
            //you can put this in the config file
            //change the connection details with your crm details
            var connection = CrmConnection.Parse("Url=https://org.crm5.dynamics.com; Username=user@live.com; Password=password; DeviceID=deviceid; DevicePassword=password");
            var context = new CrmOrganizationServiceContext(connection);
                         
            QueryExpression query = new QueryExpression()
            {
                Distinct = false,
                EntityName = "solutioncomponent",
                ColumnSet = new ColumnSet(true),
                LinkEntities = 
            {
                new LinkEntity 
                {
                    JoinOperator = JoinOperator.Inner,
                    LinkFromAttributeName = "solutionid",
                    LinkFromEntityName = "solutioncomponent",
                    LinkToAttributeName = "solutionid",
                    LinkToEntityName = "solution",
                    LinkCriteria = 
                    {
                        Conditions = 
                        {
                            new ConditionExpression("uniquename", ConditionOperator.Equal, sSolution)
                        }
                    }
                }
            },
                Criteria =
                {
                    Filters = 
                {
                    new FilterExpression
                    {
                        FilterOperator = LogicalOperator.And,
                        Conditions = 
                        {
                            new ConditionExpression("componenttype", ConditionOperator.Equal, 1)

                        },
                    }
                }
                }
            };

            EntityCollection ec = context.RetrieveMultiple(query);

            foreach (Entity entity in ec.Entities)
            {
                Guid id = (Guid)entity.Attributes["objectid"];
                li.Add(id);

            }
        }//end function
    }
    //</snippetBasicFilteringService>
}

Please provide some feedback.

1 comment:

  1. Microsoft Dynamics CRM training will help you manage and prioritize your business goals, customize.we teach MS Dynamics CRM training and class available at Hyderabad.

    ReplyDelete