SCRITTURA DEI METADATI VIA CODICE

using System;
using System.Collections.Generic;
using System.Text;
 
using MetadataServiceAPI.MetadataServiceSdk;
 
namespace MetadataServiceAPI
{
  class CreateCustomEntity
  {
      ///

      /// Example of using the new writeable Metadata API
      ///

      ///
 
      static void Main(string[] args)
      {
            // Retrieve an instance of MetadataService
            MetadataService metadataService = GetMetadataService();
 
            // Create the project audit enity using the medataservice
            EntityMetadata projectAuditEntity =
                  new EntityMetadata();
            projectAuditEntity.SchemaName = "new_projectaudit";
            projectAuditEntity.DisplayName = CreateSingleLabel("Project Audit", 1033);
            projectAuditEntity.DisplayCollectionName = CreateSingleLabel("Project Audits", 1033);
            projectAuditEntity.OwnershipType = new CrmOwnershipTypes();
            projectAuditEntity.OwnershipType.Value = OwnershipTypes.UserOwned;
            projectAuditEntity.IsAvailableOffline = new CrmBoolean();
            projectAuditEntity.IsAvailableOffline.Value = true;
            projectAuditEntity.Description = CreateSingleLabel("Demo", 1033);
 
            // Create a primary attribute for the Project Audit
            StringAttributeMetadata primary =
                  new StringAttributeMetadata();
            primary.SchemaName = "new_name";
            primary.DisplayName = CreateSingleLabel("Name", 1033);
            primary.RequiredLevel = new CrmAttributeRequiredLevel();
            primary.RequiredLevel.Value = AttributeRequiredLevel.None;
            primary.MaxLength = new CrmNumber();
            primary.MaxLength.Value = 100;
            primary.EntityLogicalName = "new_projectaudit";
            primary.LogicalName = "new_name";
            primary.Description = CreateSingleLabel("Demo", 1033);
 
            // Create an action picklist attribute for the Project Audit
            PicklistAttributeMetadata actionPickList =
                  new PicklistAttributeMetadata();
            actionPickList.SchemaName = "new_action";
            actionPickList.DisplayName = CreateSingleLabel("Action", 1033);
            actionPickList.RequiredLevel = new CrmAttributeRequiredLevel();
            actionPickList.RequiredLevel.Value = AttributeRequiredLevel.None;
            actionPickList.AttributeType = new CrmAttributeType();
            actionPickList.AttributeType.Value = AttributeType.Picklist;
            actionPickList.Options = CreateOptions(new string[] { "created", "updated", "deleted" });
            actionPickList.EntityLogicalName = "new_projectaudit";
            actionPickList.LogicalName = "new_action";
            actionPickList.Description = CreateSingleLabel("Demo", 1033);
 
            // Create the entity request
            CreateEntityRequest createEntity =
                  new CreateEntityRequest();
            createEntity.Entity = projectAuditEntity;
            createEntity.HasActivities = false;
            createEntity.HasNotes = true;
            createEntity.PrimaryAttribute = primary;
 
            // Execute the request
            metadataService.Execute(createEntity);
 
            // Create the attribute request object
            CreateAttributeRequest createAttribute =
                  new CreateAttributeRequest();
            createAttribute.EntityName = "new_projectaudit";
            createAttribute.Attribute = actionPickList;
 
            // Execute the request
            metadataService.Execute(createAttribute);
 
            // Create an One-To-Many relationship between project and projectaudit entity
            OneToManyMetadata rel =
                  new OneToManyMetadata();
            rel.SchemaName = "new_projecttoprojectaudit";
            rel.ReferencedEntity = "new_project";
            rel.ReferencingEntity = "new_projectaudit";
            rel.AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();
            rel.AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;
            rel.AssociatedMenuGroup = new CrmAssociatedMenuGroup();
            rel.AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;
            rel.AssociatedMenuLabel = CreateSingleLabel("Project Audit", 1033);
            rel.AssociatedMenuOrder = new CrmNumber();
            rel.AssociatedMenuOrder.Value = 10000;
            rel.CascadeAssign = new CrmCascadeType();
            rel.CascadeAssign.Value = CascadeType.Cascade;
            rel.CascadeDelete = new CrmCascadeType();
            rel.CascadeDelete.Value = CascadeType.Cascade;
            rel.CascadeReparent = new CrmCascadeType();
            rel.CascadeReparent.Value = CascadeType.Cascade;
            rel.CascadeShare = new CrmCascadeType();
            rel.CascadeShare.Value = CascadeType.Cascade;
            rel.CascadeUnshare = new CrmCascadeType(); 
            rel.CascadeUnshare.Value = CascadeType.Cascade;
 
            // Create the relationship request
            CreateOneToManyRequest createRelationship =
                  new CreateOneToManyRequest();
            createRelationship.Lookup = new LookupAttributeMetadata();
            createRelationship.Lookup.AttributeType = new CrmAttributeType();
            createRelationship.Lookup.AttributeType.Value = AttributeType.Lookup;
            createRelationship.Lookup.DisplayName = CreateSingleLabel("Project", 1033);
            createRelationship.Lookup.SchemaName = "new_project";
            createRelationship.Lookup.RequiredLevel = new CrmAttributeRequiredLevel();
            createRelationship.Lookup.RequiredLevel.Value = AttributeRequiredLevel.None;
            createRelationship.Lookup.Description = CreateSingleLabel("Demo", 1033);
            createRelationship.Lookup.EntityLogicalName = "new_project";
            createRelationship.OneToManyRelationship = rel;
 
            // Execute the request
            metadataService.Execute(createRelationship);
 
      }
 
 
      ///

      /// Creates a MetadataService
      ///

      /// A valid MetaService for the current user
      private static MetadataService GetMetadataService()
      {
            CrmAuthenticationToken token = new CrmAuthenticationToken();
            token.OrganizationName = "CRM_Titan_CTP";
 
            MetadataService metadataService = new MetadataService();
            metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
            metadataService.CrmAuthenticationTokenValue = token;
 
            return metadataService;
      }
 
      ///

      /// Create a Crm form label
      ///

      /// string label value for LocLabel
      /// Language Code for CrmLabel
      ///
      private static CrmLabel CreateSingleLabel(string label, int langCode)
      {
            CrmNumber crmNumber = new CrmNumber();
            crmNumber.Value = langCode;
 
            LocLabel locLabel = new LocLabel();
            locLabel.LanguageCode = crmNumber;
            locLabel.Label = label;
 
            CrmLabel crmLabel = new CrmLabel();
            crmLabel.LocLabels = new LocLabel[] { locLabel };
 
            return crmLabel;
      }
 
 
      ///

      /// Create an array of Options (the base class for picklist)
      ///

      /// string array of picklist labels
      /// Array of Options
      private static Option[] CreateOptions(string[] labels)
      {
            Option[] picklistOptions = new Option[labels.Length];
            Option picklistOption;
 
            for (int i = 0; i < labels.Length; i++)
            {
                  picklistOption = new Option();
                  picklistOption.Value = new CrmNumber();
                  picklistOption.Value.Value = i + 1;
                  picklistOption.Label = CreateSingleLabel(labels[i], 1033);
                  picklistOptions[i] = picklistOption;
            }
 
            return picklistOptions;
      }
  }
}

Dettagli...