Using RetrieveMetadataChangesRequest class (Metadata Querying) in Polaris (CRM 2011)

Hi,

Let us take a very simple example to understand RetrieveMetadataChangesRequest class introduced in Polaris.

In this example we will retrieve information about all the attributes for contact entity where attribute type is of lookup.

For this we will use the RetrieveMetadataChangesRequest class.

This class contains the data which specifies the metadata information that needs to be retrieved.

RetrieveMetadataChangesRequest class has a Query property that accepts an EntityQueryExpression.

RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new
RetrieveMetadataChangesRequest()
{
Query = entityQueryExpression
};

EntityQueryExpression class defines the query to retrieve the metadata information.

Here we will need to set the following three properties of the EntityQueryExpression class


EntityQueryExpression entityQueryExpression = new
EntityQueryExpression()
{
Criteria = EntityFilter,
Properties = EntityProperties,
AttributeQuery = new
AttributeQueryExpression()
{
Criteria = AttributeFilter,
Properties = AttributeProperties
}
};
  • Criteria – specifies the filter criteria like for which entities we want metadata information to be retrieved.
  • Properties –
    specifies the properties to be returned.
  • AttributeQuery – specifies the criteria and properties for the entities attribute metadata like which type of attribute to retrieve and what all information to be retrieved for that attribute.

Specfying EntityFilter

This is how we will specify that we need metadata information for only the contact entity.

Specifying Properties of the Entity to be retrieved

Here we are specifying all the properties to be returned

MetadataPropertiesExpression EntityProperties = new
MetadataPropertiesExpression() { AllProperties = true };


Here we can set the AllProperties as false and instead specify the property names that we want to retrieve as shown below

EntityProperties.PropertyNames.Add(“AttributeType”);

Specifying the lookup attribute to be retrieved

MetadataConditionExpression metadataconditionexpressionEntity = new
MetadataConditionExpression();
metadataconditionexpressionEntity.PropertyName = "SchemaName";
metadataconditionexpressionEntity.Value = "Contact";
metadataconditionexpressionEntity.ConditionOperator = MetadataConditionOperator.Equals;
MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And);
EntityFilter.Conditions.Add(metadataconditionexpressionEntity);

Specifying the properties to be retrieved for the lookup attribute

Here we are specifying that we only need the AttributeType and Description information for the Lookup attributes retrieved.

MetadataPropertiesExpression AttributeProperties = new
MetadataPropertiesExpression() { AllProperties = false };
AttributeProperties.PropertyNames.Add("AttributeType");
AttributeProperties.PropertyNames.Add("Description");

Full sample code

// specifying EntityFilter to only include entity having SchemaName as contact

MetadataConditionExpression metadataconditionexpressionEntity = new
MetadataConditionExpression();
metadataconditionexpressionEntity.PropertyName = "SchemaName";
metadataconditionexpressionEntity.Value = "Contact";
metadataconditionexpressionEntity.ConditionOperator = MetadataConditionOperator.Equals;
MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And);
EntityFilter.Conditions.Add(metadataconditionexpressionEntity);

// specfying EntityProperties to include the properties to be retrieved
MetadataPropertiesExpression EntityProperties = new
MetadataPropertiesExpression() { AllProperties = true };
MetadataConditionExpression metadataExpression = new
MetadataConditionExpression("AttributeType", MetadataConditionOperator.Equals, AttributeTypeCode.Lookup);
MetadataFilterExpression AttributeFilter = new
MetadataFilterExpression(LogicalOperator.And);
AttributeFilter.Conditions.Add(metadataExpression);

//A Properties expression to limit the properties to be included with attributes
MetadataPropertiesExpression AttributeProperties = new
MetadataPropertiesExpression() { AllProperties = false };
AttributeProperties.PropertyNames.Add("AttributeType");
AttributeProperties.PropertyNames.Add("Description");

//An entity query expression to combine the filter expressions and property expressions for the query.
EntityQueryExpression entityQueryExpression = new
EntityQueryExpression()
{
Criteria = EntityFilter,
Properties = EntityProperties,
AttributeQuery = new
AttributeQueryExpression()
{
Criteria = AttributeFilter,
Properties = AttributeProperties
}
};

RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new
RetrieveMetadataChangesRequest()
{
Query = entityQueryExpression
};
RetrieveMetadataChangesResponse retrieveMetadataChangesResponse = (RetrieveMetadataChangesResponse)_service.Execute(retrieveMetadataChangesRequest);
EntityMetadata entityMetadata = retrieveMetadataChangesResponse.EntityMetadata[0];
foreach (AttributeMetadata attMetadata in entityMetadata.Attributes)
{
MessageBox.Show(attMetadata.Description.UserLocalizedLabel.Label);
}

Hope it helps.

ExecuteMultipleRequest message in Polaris (CRM 2011)

Using ExecuteMultipleRequest class we can now execute one or more message requests as a single batch.

For example in the code below we are creating 3 contact entity record as well as deleting one of the contact records as a single batch.

Here below, we are first initializing 3 contact entity record, then creating the associated CreateRequest and DeleteRequest.

We are then adding all these requests to OrganizationRequestCollection which we then use for the Requests property of the ExecuteMutlipleRequest class. The request are executed in the order in which they are added to the OrganizationRequestCollection.

Settings Property defines whether execution should continue if an error occurs and if responses for each message request processed are to be returned or not.

// Create 3 contact entitiy's record
Entity entity1 = new Entity("contact");
entity1.Attributes["lastname"] = "lastname1";
Entity entity2 = new Entity("contact");
entity2.Attributes["lastname"] = "lastname2";
Entity entity3 = new Entity("contact");
entity3.Attributes["lastname"] = "lastname3";
// Create CreateRequest and Delete Request
CreateRequest createReq1 = new CreateRequest();
createReq1.Target = entity1;
CreateRequest createReq2 = new CreateRequest();
createReq2.Target = entity2;

// DeleteRequest with incorrect guid to throw error

DeleteRequest deleteReq = new DeleteRequest();
deleteReq.Target = new EntityReference("contact", Guid.NewGuid());

CreateRequest createReq3 = new CreateRequest();
createReq3.Target = entity3;

// Initialize OrganizationRequestCollection

OrganizationRequestCollection orgReqCollection = new OrganizationRequestCollection();
orgReqCollection.Add(createReq1);
orgReqCollection.Add(createReq2);
orgReqCollection.Add(deleteReq);
orgReqCollection.Add(createReq3);

// Intialize ExecuteMultipleRequest

ExecuteMultipleRequest executeMutlipleRequest = new ExecuteMultipleRequest();
executeMutlipleRequest.Requests = orgReqCollection;
executeMutlipleRequest.Settings = new ExecuteMultipleSettings();

// ContinueOnError - specifies whether to continue in case of error in any of the request or not
executeMutlipleRequest.Settings.ContinueOnError = false;

// ReturnResponses - specifies whether to return responses for the requests or not
executeMutlipleRequest.Settings.ReturnResponses = true;

ExecuteMultipleResponse executeMulResponse = (ExecuteMultipleResponse) _service.Execute(executeMutlipleRequest);

In the above example we are deliberately throwing exception for the DeleteRequest.

So if we are setting the ContinueOnError property as false, our program will create the first two contact record and then throws exception.

Contact Records Created:

Exception:

And if we are setting the ContinueOnError as true, our program will create all the three contact records and for the DeleteRequest it will return the error message as a part of response as we have set the ReturnResponses as true.

All the three contact records created:

ExecuteMultipleResponse:

Here we get 4 responses one for each of the requests in OrganizationCollectionRequest.

IsFaulted property of the ExecuteMultipleResponse has value as true as we get a FaultException in case of DeleteRequest.

The Message for the DeleteRequest gives us the error message.

The response for the CreateRequest contains the Guid of the contact record created.


Hope it helps

ExecuteMultipleRequest and RetrieveMetadataChangesRequest in CRM 2011

Hi,

Check out the following MSDN articles to know how to use these new messages added in SDK.

http://msdn.microsoft.com/en-us/library/jj863631.aspx

http://msdn.microsoft.com/en-us/library/jj863599.aspx

Bye.