Detect metadata changes in MSCRM 4.0

The

Google Tags:
Message retrieves a time stamp for the metadata can be executed before you retrieve metadata. You can repeat this process later to see if the metadata has changed in the intervening time. If the value is the same, the metadata has not changed. If the value is different, you retrieve the metadata again to get the updated values.

This sample shows how to detect metadata changes in MSCRM 4.0 for the purposes of building a metadata cache.


using System;
using System.Collections;
using MetadataServiceSdk;
using CrmSdk;

namespace Microsoft.Crm.Sdk.Reference.MetadataServiceApi
{
///
/// This sample shows how to retrieve a timestamp.
///

public class RetrieveTimestamp
{
public RetrieveTimestamp()
{
}

public static bool Run(string crmServerUrl, string orgName)
{
bool success = true;

try
{
// Set up the CRM Services.
MetadataService metadataService = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetMetadataService(crmServerUrl, orgName);
metadataService.PreAuthenticate = true;

// Create a metadata timestamp request
RetrieveTimestampRequest initialTimestampRequest = new RetrieveTimestampRequest();

// Execute the request
RetrieveTimestampResponse initialTimestampResponse = (RetrieveTimestampResponse)metadataService.Execute(initialTimestampRequest);

// Store the retrieved metadata timestamp
string initialTimestamp = initialTimestampResponse.Timestamp;

// Create a metadata timestamp request
RetrieveTimestampRequest timestampRequest = new RetrieveTimestampRequest();

// Execute the request
RetrieveTimestampResponse timestampResponse = (RetrieveTimestampResponse)metadataService.Execute(timestampRequest);

// Access the retrieved timestamp
string currentTimestamp = timestampResponse.Timestamp;

// Compare the timestamp with a previously retrieved timestamp
if (currentTimestamp == initialTimestamp)
{
// The metadata has not been modified since our initial timestamp was retrieved.
}
else
{
// The metadata has been modified since our initial timestamp was retrieved.
}

#region check success

// Verify that the timestamp has data
if (currentTimestamp.Length == 0)
{
success = false;
}

#endregion

#region Remove Data Required for this Sample

#endregion

}
catch (System.Web.Services.Protocols.SoapException)
{
// Perform error handling here.
throw;
}
catch (Exception)
{
throw;
}

return success;
}
}
}