Create Fields in MS CRM 2011 using CRM Metadata Service

Create Fields in MS CRM 2011 using CRM Metadata Service

Some times we may have a requirement to create more fields with same data type , so in this way if we try to create from the CRM it will take so much time to create all the fields. Better way i approach is using CRM Meta Data service we can create it easily.

Steps to Create Fields:

Step 1: Create On Console Application “CreateFieldsInCRMUsingMetaDataService” and paste the following code in your console application.

Step 2 : Specify your Schema Name and Display name separated by | (pipe) symbol. The next field must be separated with ,(Comma).

Code will detect the next field with ,(Comma).

Step 3: You may replace your Organization Prefix below as shown “new_”

In this code i am creating the Two Option Sets , you may create any field in the Meta Data Using this Service.

namespace CreateFieldsInCRMUsingMetaDataService
{
class CreateFieldsInCRM
{
static IOrganizationService service = GetOrgService();
public const int _languageCode = 1033;
static void Main(string[] args)
{
CreateBooleanFiledsInCRM();
}
public static IOrganizationService GetOrgService()
{
Uri oUri = new Uri(“http://<servername>/<OrgName>/XRMServices/2011/Organization.svc&#8221;);

//** Your client credentials

ClientCredentials cliCredentials = new ClientCredentials();
cliCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

//Create your Organization Service Proxy
OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(oUri, null, cliCredentials, null);
IOrganizationService Service = (IOrganizationService)_serviceProxy;
_serviceProxy.EnableProxyTypes();
return Service;
}
public static void CreateBooleanFiledsInCRM()
{
string SchemaAndDisplayNames = “gender|Gender,age|Age”;

string[] splitSchemaAndDisplayNamesByComma = SchemaAndDisplayNames.Split(new Char[] { ‘,’, ‘.’, ‘:’, });

foreach (string FieldNames in splitSchemaAndDisplayNamesByComma)
{
if (FieldNames.Trim() != “”)
{
string FieldSchemaAndDisplayNames = FieldNames.ToString();
string[] splitSchemaAndDisplayNames = FieldSchemaAndDisplayNames.Split(new Char[] { ‘|’ });

string schemaname = Regex.Replace(splitSchemaAndDisplayNames[0], “[^a-zA-Z0-9_.]+”, “”, RegexOptions.Compiled);
schemaname = “new_” + schemaname;
string displayname = splitSchemaAndDisplayNames[1];

//The below function will create the Fields in MS CRM
CreateAttributeInMsCRMOrgService(“new_entity”, schemaname, displayname, “”);

}
}
}
private static void CreateAttributeInMsCRMOrgService(string EntitySchemaName, string FieldSchemaName, string FieldDisplayName, string FieldDescription)
{
// Create storage for new attributes being created
List<AttributeMetadata> addedAttributes = new List<AttributeMetadata>();

// Create a boolean attribute
BooleanAttributeMetadata boolAttribute = new BooleanAttributeMetadata
{
// Set base properties
SchemaName = FieldSchemaName,
DisplayName = new Label(FieldDisplayName, _languageCode),
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
Description = new Label(FieldDescription, _languageCode),
// Set extended properties
OptionSet = new BooleanOptionSetMetadata(
new OptionMetadata(new Label(“True”, _languageCode), 1),
new OptionMetadata(new Label(“False”, _languageCode), 0)
)
};

// Add to list
addedAttributes.Add(boolAttribute);

foreach (AttributeMetadata anAttribute in addedAttributes)
{
// Create the request.
CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
{
EntityName = EntitySchemaName,
Attribute = anAttribute
};

// Execute the request.
service.Execute(createAttributeRequest);
}

}

}
}

Regards,

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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