Saturday, November 20, 2010

Create Custom Entity Message (MetadataService)

The relevant classes: 1) CreateEntityRequest 2) CreateEntityResponse

Remarks
To perform this action, the caller must be a user in the organization for which metadata is requested and must haveCreate Entity and Read Entity privileges.

The entity created will have a property value of true for the EntityMetadata.IsCustomEntity property.

You must publish the changes to the metadata before this change will be visible in the application. For more information see Publishing the Metadata.

Example with Source Code:

// Create an authentication token.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = "AdventureWorksCycle";

// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0;

// Create the metadata Web service.
MetadataService metadataService = new MetadataService();
metadataService.Url = "http://<servername>:<port>/MSCRMServices/2007/MetadataService.asmx";
metadataService.CrmAuthenticationTokenValue = token;
metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
metadataService.PreAuthenticate = true;

// Create the entity.
EntityMetadata timesheetEntity = new EntityMetadata();
timesheetEntity.SchemaName = "new_timesheet";
timesheetEntity.OwnershipType = new CrmOwnershipTypes();
timesheetEntity.OwnershipType.Value = OwnershipTypes.UserOwned;
timesheetEntity.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Timesheet", 1033);
timesheetEntity.DisplayCollectionName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Timesheets", 1033);
timesheetEntity.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Employee Timesheet", 1033);

// Create the primary attribute.
StringAttributeMetadata primaryAttribute = new StringAttributeMetadata();
primaryAttribute.SchemaName = "new_name";
primaryAttribute.RequiredLevel = new CrmAttributeRequiredLevel();
primaryAttribute.RequiredLevel.Value = AttributeRequiredLevel.None;
primaryAttribute.MaxLength = new MetadataServiceSdk.CrmNumber();
primaryAttribute.MaxLength.Value = 100;
primaryAttribute.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Name", 1033);
primaryAttribute.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Employee Name", 1033);

// Create the entity request.
CreateEntityRequest createEntity = new CreateEntityRequest();
createEntity.Entity = timesheetEntity;
createEntity.HasActivities = false;
createEntity.HasNotes = true;
createEntity.PrimaryAttribute = primaryAttribute;

// Execute the request.
CreateEntityResponse entityResponse = (CreateEntityResponse )metadataService.Execute(createEntity);

// Publish the new custom entity.
// Note that the custom entities cannot be used until they have been published.
PublishAllXmlRequest publishAllRequest = new PublishAllXmlRequest();
PublishAllXmlResponse publishAllResponse = (PublishAllXmlResponse)metadataService.Execute(publishAllRequest);

No comments:

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...