
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MetadataServiceTest.MetadataService;
namespace MetadataServiceTest
{
/// <summary>
/// author:seer lin
/// description:use MetadataService to create entity schema and attribute
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ServiceSetup();
}
MetadataService.MetadataService metadataService = null;
private void button1_Click(object sender, EventArgs e)
{
//a new entity schema
EntityMetadata entity = new EntityMetadata();
entity.SchemaName = "new_testentity";
//entity.LogicalName = "new_testentity";
entity.DisplayName = CreateCrmLabel("Test Entity",2052);
entity.DisplayCollectionName = CreateCrmLabel("Test Entities",2052);
entity.Description = CreateCrmLabel("demo entity",2052);
entity.OwnershipType = new CrmOwnershipTypes();
entity.OwnershipType.Value = OwnershipTypes.UserOwned;
//entity.IsCustomEntity = new CrmBoolean();
//entity.IsCustomEntity.Value = true;
entity.IsAvailableOffline = new CrmBoolean();
entity.IsAvailableOffline.Value = true;
//entity.IsActivity = new CrmBoolean();
//entity.IsActivity.Value = true;
//define attribute schema
StringAttributeMetadata newName = new StringAttributeMetadata();
newName.SchemaName = "new_name";
newName.DisplayName = CreateCrmLabel("Name",2052);
newName.EntityLogicalName = "new_testentity";
newName.AttributeType = new CrmAttributeType();
newName.AttributeType.Value = AttributeType.String;
newName.MaxLength = new CrmNumber();
newName.MaxLength.Value = 15;
newName.RequiredLevel = new CrmAttributeRequiredLevel();
newName.RequiredLevel.Value = AttributeRequiredLevel.Required;
PicklistAttributeMetadata newAction = new PicklistAttributeMetadata();
newAction.SchemaName = "new_action";
newAction.DisplayName = CreateCrmLabel("Action", 2052);
newAction.EntityLogicalName = "new_testentity";
newAction.AttributeType = new CrmAttributeType();
newAction.AttributeType.Value = AttributeType.Picklist;
newAction.RequiredLevel = new CrmAttributeRequiredLevel();
newAction.RequiredLevel.Value = AttributeRequiredLevel.None;
newAction.Options= CreateOptions("create","select","modify");
//create entity
CreateEntityRequest createEntityRequest = new CreateEntityRequest();
createEntityRequest.Entity = entity;
createEntityRequest.HasActivities = false;
createEntityRequest.HasNotes = true;
createEntityRequest.PrimaryAttribute = newName;//to create a new entity ,the primary attribute is required
CreateEntityResponse response = (CreateEntityResponse)metadataService.Execute(createEntityRequest);
string guid= response.EntityId.Value.ToString();
//6003cf19-2175-df11-9b67-0003ff779ab5,
//create attribute
CreateAttributeRequest attributeRequest = new CreateAttributeRequest();
//atrributeRequest.EntityName = "new_testentity";
//atrributeRequest.Attribute = newName;
//metadataService.Execute(atrributeRequest);
//attributeRequest = new CreateAttributeRequest();
attributeRequest.EntityName = "new_testentity";
attributeRequest.Attribute = newAction;
metadataService.Execute(attributeRequest);
}
//
/// <summary>
/// setup service
/// </summary>
private void ServiceSetup()
{
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = "crmdev2";
metadataService = new MetadataService.MetadataService();
metadataService.Url = "http://localhost:5555/mscrmservices/2007/MetadataService.asmx";
metadataService.Credentials = new System.Net.NetworkCredential("administrator", "", "crm");
metadataService.CrmAuthenticationTokenValue = token;
}
/// <summary>
/// create a CrmLabel
/// </summary>
/// <param name="name">label name</param>
/// <param name="langCode">language code</param>
private CrmLabel CreateCrmLabel(string labelText, int langCode) {
CrmLabel crmLabel = new CrmLabel();
LocLabel locLabel = new LocLabel();
locLabel.Label = labelText;
locLabel.LanguageCode = new CrmNumber();
locLabel.LanguageCode.Value = langCode;
crmLabel.LocLabels = new LocLabel[] { locLabel };
return crmLabel;
}
/// <summary>
/// create options for picklist etc.
/// </summary>
/// <param name="labels">display labels</param>
/// <returns>options</returns>
private Option[] CreateOptions(params object[] labels) {
Option[] options=new Option[labels.Length];
for (int i = 0; i < options.Length;i++ )
{
options[i] = new Option();
options[i].Label = CreateCrmLabel(labels[i].ToString(), 2052);
options[i].Value = new CrmNumber();
options[i].Value.Value = i+1;//the value must large than 0
}
return options;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MetadataServiceTest.MetadataService;
namespace MetadataServiceTest
{
/// <summary>
/// author:seer lin
/// description:use MetadataService to create entity schema and attribute
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ServiceSetup();
}
MetadataService.MetadataService metadataService = null;
private void button1_Click(object sender, EventArgs e)
{
//a new entity schema
EntityMetadata entity = new EntityMetadata();
entity.SchemaName = "new_testentity";
//entity.LogicalName = "new_testentity";
entity.DisplayName = CreateCrmLabel("Test Entity",2052);
entity.DisplayCollectionName = CreateCrmLabel("Test Entities",2052);
entity.Description = CreateCrmLabel("demo entity",2052);
entity.OwnershipType = new CrmOwnershipTypes();
entity.OwnershipType.Value = OwnershipTypes.UserOwned;
//entity.IsCustomEntity = new CrmBoolean();
//entity.IsCustomEntity.Value = true;
entity.IsAvailableOffline = new CrmBoolean();
entity.IsAvailableOffline.Value = true;
//entity.IsActivity = new CrmBoolean();
//entity.IsActivity.Value = true;
//define attribute schema
StringAttributeMetadata newName = new StringAttributeMetadata();
newName.SchemaName = "new_name";
newName.DisplayName = CreateCrmLabel("Name",2052);
newName.EntityLogicalName = "new_testentity";
newName.AttributeType = new CrmAttributeType();
newName.AttributeType.Value = AttributeType.String;
newName.MaxLength = new CrmNumber();
newName.MaxLength.Value = 15;
newName.RequiredLevel = new CrmAttributeRequiredLevel();
newName.RequiredLevel.Value = AttributeRequiredLevel.Required;
PicklistAttributeMetadata newAction = new PicklistAttributeMetadata();
newAction.SchemaName = "new_action";
newAction.DisplayName = CreateCrmLabel("Action", 2052);
newAction.EntityLogicalName = "new_testentity";
newAction.AttributeType = new CrmAttributeType();
newAction.AttributeType.Value = AttributeType.Picklist;
newAction.RequiredLevel = new CrmAttributeRequiredLevel();
newAction.RequiredLevel.Value = AttributeRequiredLevel.None;
newAction.Options= CreateOptions("create","select","modify");
//create entity
CreateEntityRequest createEntityRequest = new CreateEntityRequest();
createEntityRequest.Entity = entity;
createEntityRequest.HasActivities = false;
createEntityRequest.HasNotes = true;
createEntityRequest.PrimaryAttribute = newName;//to create a new entity ,the primary attribute is required
CreateEntityResponse response = (CreateEntityResponse)metadataService.Execute(createEntityRequest);
string guid= response.EntityId.Value.ToString();
//6003cf19-2175-df11-9b67-0003ff779ab5,
//create attribute
CreateAttributeRequest attributeRequest = new CreateAttributeRequest();
//atrributeRequest.EntityName = "new_testentity";
//atrributeRequest.Attribute = newName;
//metadataService.Execute(atrributeRequest);
//attributeRequest = new CreateAttributeRequest();
attributeRequest.EntityName = "new_testentity";
attributeRequest.Attribute = newAction;
metadataService.Execute(attributeRequest);
}
//
/// <summary>
/// setup service
/// </summary>
private void ServiceSetup()
{
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = "crmdev2";
metadataService = new MetadataService.MetadataService();
metadataService.Url = "http://localhost:5555/mscrmservices/2007/MetadataService.asmx";
metadataService.Credentials = new System.Net.NetworkCredential("administrator", "", "crm");
metadataService.CrmAuthenticationTokenValue = token;
}
/// <summary>
/// create a CrmLabel
/// </summary>
/// <param name="name">label name</param>
/// <param name="langCode">language code</param>
private CrmLabel CreateCrmLabel(string labelText, int langCode) {
CrmLabel crmLabel = new CrmLabel();
LocLabel locLabel = new LocLabel();
locLabel.Label = labelText;
locLabel.LanguageCode = new CrmNumber();
locLabel.LanguageCode.Value = langCode;
crmLabel.LocLabels = new LocLabel[] { locLabel };
return crmLabel;
}
/// <summary>
/// create options for picklist etc.
/// </summary>
/// <param name="labels">display labels</param>
/// <returns>options</returns>
private Option[] CreateOptions(params object[] labels) {
Option[] options=new Option[labels.Length];
for (int i = 0; i < options.Length;i++ )
{
options[i] = new Option();
options[i].Label = CreateCrmLabel(labels[i].ToString(), 2052);
options[i].Value = new CrmNumber();
options[i].Value.Value = i+1;//the value must large than 0
}
return options;
}
}
}