Detect Metadata Changes

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

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

Example

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

namespace Microsoft.Crm.Sdk.Reference.MetadataServiceApi
{
   /// <summary>
   /// This sample shows how to retrieve a timestamp.
   /// </summary>
   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;
      }
   }
}

See Also

Reference

© 2010 Microsoft Corporation. All rights reserved.