Create Attribute Request in CRM 2011 SDK

Posted on Posted in CRM 2011 SDK, MS CRM 2011, MS Dynamics, MS Dynamics CRM

In CRM 4.0 SDK we had different way of creating a new attribute for an Entity

First of all we had to call MetadataService to create an instance of it then use CreateAttributeRequest to assign it Entity and attribute values

Sample in CRM 4 is below


using (MetadataService mds = CrmServices.MscrmMetadataService(orgName))
{
// Create the request/response objects.
CreateAttributeRequest createAttribute = new CreateAttributeRequest();
CreateAttributeResponse attributeResponse = null;

// Set the request properties
createAttribute.EntityName = entityName;
createAttribute.Attribute = attribute;

try
{
// Execute the request.
attributeResponse = (CreateAttributeResponse)mds.Execute(createAttribute);
}
catch
{
throw new ApplicationException(“Failed creating attribute: ” + entityName + “.” + attribute.SchemaName);
}

In CRM 2011 SDK it is a different approch we need to take to achieve our results


public static void CreateAttributeRequest(string orgName, string entityName, AttributeMetadata attribute)
{
using (OrganizationServiceProxy service = CrmServerConnection.GetOrganisationProxy(orgName))
{
CreateAttributeRequest req = new CreateAttributeRequest
{
EntityName = entityName,
Attribute = attribute
};

try
{
RetrieveAttributeResponse res = (RetrieveAttributeResponse)service.Execute(req);
}
catch (Exception)
{
throw new ApplicationException(“Failed creating attribute: ” + entityName + “.” + attribute.SchemaName);
}

}
}