How to do it...

  1. Using the same solution used for the previous recipe, create a new class called PacktOptionSetFiltering with the following code:
using System; 
using System.Collections.Generic;
using System.Linq;
using Microsoft.Crm.Services.Utility;
using Microsoft.Xrm.Sdk.Metadata;

namespace Packt.Xrm.CrmSvcUtilExensions
{
public sealed class PacktOptionSetFiltering : ICodeWriterFilterService
{
private ICodeWriterFilterService DefaultService { get; }
private Dictionary<string, bool> GeneratedOptionSets { get; }
public PacktOptionSetFiltering(ICodeWriterFilterService defaultService)
{
DefaultService = defaultService;
GeneratedOptionSets = new Dictionary<string, bool>();
}
//Only include piclists, state, and status attributes
bool ICodeWriterFilterService.GenerateAttribute(AttributeMetadata attributeMetadata, IServiceProvider services)
{
return (attributeMetadata.AttributeType == AttributeTypeCode.Picklist
|| attributeMetadata.AttributeType == AttributeTypeCode.State
|| attributeMetadata.AttributeType == AttributeTypeCode.Status);
}

bool ICodeWriterFilterService.GenerateOptionSet(OptionSetMetadataBase optionSetMetadata,
IServiceProvider services)
{
if (!optionSetMetadata.Name.StartsWith(GetFilter()) || GeneratedOptionSets.ContainsKey(optionSetMetadata.Name))
return false;
if (optionSetMetadata.IsGlobal.HasValue && optionSetMetadata.IsGlobal.Value)
GeneratedOptionSets[optionSetMetadata.Name] = true;
return true;

}

// Omitted code GenerateServiceContext returns false
// GenerateRelationship returns flase
// GenerateEntity same as previous recipe
// GetFilter same as previous recipe
// GenerateOption default behavior

}
}
  1. Create a new class called CodeCustomizationService.cs with the following piece of code:
using System; 
using System.CodeDom;
using Microsoft.Crm.Services.Utility;

namespace Packt.Xrm.CrmSvcUtilExensions
{
public sealed class CodeCustomizationService : ICustomizeCodeDomService
{
public void CustomizeCodeDom(CodeCompileUnit codeUnit, IServiceProvider services)
{
for (var i = 0; i < codeUnit.Namespaces.Count; ++i)
{
var types = codeUnit.Namespaces[i].Types;
for (var j = 0; j < types.Count;)
{
if (!types[j].IsEnum || types[j].Members.Count == 0)
{
types.RemoveAt(j);
}
else
{
j += 1;
}
}
}
}
}
}
  1. Compile your code.
  2. Run the CrmSvcUtil command in the binDebug folder with the following parameters:
CrmSvcUtil.exe /codewriterfilter:"Packt.Xrm.CrmSvcUtilExensions.PacktOptionSetFiltering, Packt.Xrm.CrmSvcUtilExensions" /codecustomization:"Packt.Xrm.CrmSvcUtilExensions.CodeCustomizationService, Packt.Xrm.CrmSvcUtilExensions" /connectionstring:"AuthType=Office365;[email protected]; Password=;Url=https://.crm.dynamics.com" /namespace:Packt.Xrm.Entities /out:OptionSets.cs /filter:packt_
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset