Below are the snippets for creating fields:
// Create a boolean attribute BooleanAttributeMetadata boolField = new BooleanAttributeMetadata { // Set base properties - language code = 1033 for english SchemaName = "new_boolean", DisplayName = new Label("Sample Boolean", _languageCode), RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None), Description = new Label("Boolean Attribute", _languageCode), // Set extended properties OptionSet = new BooleanOptionSetMetadata( new OptionMetadata(new Label("True", _languageCode), 1), new OptionMetadata(new Label("False", _languageCode), 0) ) };
// Create a date time attribute DateTimeAttributeMetadata dtAttribute = new DateTimeAttributeMetadata { // Set base properties SchemaName = "new_datetime", DisplayName = new Label("Sample DateTime", _languageCode), RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None), Description = new Label("DateTime Attribute", _languageCode), // Set extended properties Format = DateTimeFormat.DateOnly, ImeMode = ImeMode.Disabled };
// Create a boolean attribute BooleanAttributeMetadata boolField = new BooleanAttributeMetadata { // Set base properties - language code = 1033 for english SchemaName = "new_boolean", DisplayName = new Label("Sample Boolean", _languageCode), RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None), Description = new Label("Boolean Attribute", _languageCode), // Set extended properties OptionSet = new BooleanOptionSetMetadata( new OptionMetadata(new Label("True", _languageCode), 1), new OptionMetadata(new Label("False", _languageCode), 0) ) };
// Create a date time attribute DateTimeAttributeMetadata dtAttribute = new DateTimeAttributeMetadata { // Set base properties SchemaName = "new_datetime", DisplayName = new Label("Sample DateTime", _languageCode), RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None), Description = new Label("DateTime Attribute", _languageCode), // Set extended properties Format = DateTimeFormat.DateOnly, ImeMode = ImeMode.Disabled };
// Create a decimal attribute
DecimalAttributeMetadata decimalAttribute = new DecimalAttributeMetadata
{
// Set base properties
SchemaName = "new_decimal",
DisplayName = new Label("Sample Decimal", _languageCode),
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
Description = new Label("Decimal Attribute", _languageCode),
// Set extended properties
MaxValue = 100,
MinValue = 0,
Precision = 1
};
// Create a integer attribute
IntegerAttributeMetadata integerAttribute = new IntegerAttributeMetadata
{
// Set base properties
SchemaName = "new_integer",
DisplayName = new Label("Sample Integer", _languageCode),
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
Description = new Label("Integer Attribute", _languageCode),
// Set extended properties
Format = IntegerFormat.None,
MaxValue = 100,
MinValue = 0
};
// Create a memo attribute
MemoAttributeMetadata memoAttribute = new MemoAttributeMetadata
{
// Set base properties
SchemaName = "new_memo",
DisplayName = new Label("Sample Memo", _languageCode),
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
Description = new Label("Memo Attribute", _languageCode),
// Set extended properties
Format = StringFormat.TextArea,
ImeMode = ImeMode.Disabled,
MaxLength = 500
};
// Create a money attribute
MoneyAttributeMetadata moneyAttribute = new MoneyAttributeMetadata
{
// Set base properties
SchemaName = "new_money",
DisplayName = new Label("Money Picklist", _languageCode),
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
Description = new Label("Money Attribue", _languageCode),
// Set extended properties
MaxValue = 1000.00,
MinValue = 0.00,
Precision = 1,
PrecisionSource = 1,
ImeMode = ImeMode.Disabled
};
PicklistAttributeMetadata pickListAttribute =
new PicklistAttributeMetadata
{
// Set base properties
SchemaName = "new_picklist",
DisplayName = new Label("Sample Picklist", _languageCode),
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
Description = new Label("Picklist Attribute", _languageCode),
// Set extended properties
// Build local picklist options
OptionSet = new OptionSetMetadata
{
IsGlobal = false,
OptionSetType = OptionSetType.Picklist,
Options =
{
new OptionMetadata(
new Label("Created", _languageCode), null),
new OptionMetadata(
new Label("Updated", _languageCode), null),
new OptionMetadata(
new Label("Deleted", _languageCode), null)
}
}
};
CreateAttributeRequest createAttributeRequest = new CreateAttributeRequest
{
EntityName = Contact.EntityLogicalName,
Attribute = anAttribute
};
// Execute the request.
_serviceProxy.Execute(createAttributeRequest);
For Lookup fields
CreateOneToManyRequest createOneToManyRelationshipRequest =
new CreateOneToManyRequest
{
OneToManyRelationship =
new OneToManyRelationshipMetadata
{
ReferencedEntity = "account",
ReferencingEntity = "new_customentity", //Entity to have lookup field
SchemaName = "new_account",
AssociatedMenuConfiguration = new AssociatedMenuConfiguration
{
Behavior = AssociatedMenuBehavior.UseLabel,
Group = AssociatedMenuGroup.Details,
Label = new Label("Account", 1033),
Order = 10000
},
CascadeConfiguration = new CascadeConfiguration
{
Assign = CascadeType.Cascade,
Delete = CascadeType.Cascade,
Merge = CascadeType.Cascade,
Reparent = CascadeType.Cascade,
Share = CascadeType.Cascade,
Unshare = CascadeType.Cascade
}
},
Lookup = new LookupAttributeMetadata
{
SchemaName = "new_parent_accountid",
DisplayName = new Label("Account Lookup", 1033),
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
Description = new Label("Sample Lookup", 1033)
}
};
CreateOneToManyResponse createOneToManyRelationshipResponse =
(CreateOneToManyResponse)appService.Execute(
createOneToManyRelationshipRequest);
Guid _oneToManyRelationshipId = createOneToManyRelationshipResponse.RelationshipId;
string _oneToManyRelationshipName =
createOneToManyRelationshipRequest.OneToManyRelationship.SchemaName;
Console.WriteLine(
"The one-to-many relationship has been created between {0} and {1}.",
"account", "campaign");