web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)
Answered

Retrieve Metadata changes

(0) ShareShare
ReportReport
Posted on by

Greetings,

I need to retrieve any changes in Dynamics metadata (ie, when a new value to an optionset is added).

This has to be done upon the change of the option set and not triggered from an entity-related event such as "Create" or "Update".

What is the best way to achieve that? My best guess is to write a C# plugin that will take care of this task.

Would you give me some suggestion on how to register the plugin correctly?

Thank you

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Alessandro Graps Profile Picture
    2,664 on at

    Hi,

    I agree with you. you can use a plugin with c#. Here there is an example to retrieve only change in metadata msdn.microsoft.com/.../jj863599.aspx

    Thanks

    A.G.

  • Community Member Profile Picture
    on at

    Alessandro Graps 

    Thank you for your reply. I found the article very useful to start, however how would I call this method? Is there a way to make the plugin listens to metadata changes?

    As of now I know how to register the plugin for entities and records' changes. Any suggestion on that?

  • Verified answer
    Community Member Profile Picture
    on at

    Hello again, I am using the following code to retrieve the optionsets that have changed however, when I try to read them I cannot get the name and more importantly I cannot detect if there have been any changes to it.

    What I would like to do is store the value of the ServerVersionStamp and use it to check whether an optionset has changed since then.

    How would I go to accomplish that?

    Below is the code that I am using:

    private OptionMetadata[] RetrieveOptionSet(IOrganizationService service, string globalOptionSetName)
      {
       RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest { Name = globalOptionSetName };
       RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)service.Execute(retrieveOptionSetRequest);
       OptionSetMetadata retrievedOptionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;
       OptionMetadata[] optionList = retrievedOptionSetMetadata.Options.ToArray();

       return optionList;
      }

      private void DetectOptionSetChanges(IOrganizationService service, IPluginExecutionContext context)
      {
       String[] excludedEntities =
       {
        "WorkflowLog",
        "Template",
        "CustomerOpportunityRole",
        "Import",
        "UserQueryVisualization",
        "UserEntityInstanceData",
        "ImportLog",
        "RecurrenceRule",
        "QuoteClose",
        "UserForm",
        "SharePointDocumentLocation",
        "Queue",
        "DuplicateRule",
        "OpportunityClose",
        "Workflow",
        "RecurringAppointmentMaster",
        "CustomerRelationship",
        "Annotation",
        "SharePointSite",
        "ImportData",
        "ImportFile",
        "OrderClose",
        "Contract",
        "BulkOperation",
        "CampaignResponse",
        "Connection",
        "Report",
        "CampaignActivity",
        "UserEntityUISettings",
        "IncidentResolution",
        "GoalRollupQuery",
        "MailMergeTemplate",
        "Campaign",
        "PostFollow",
        "ImportMap",
        "Goal",
        "AsyncOperation",
        "ProcessSession",
        "UserQuery",
        "ActivityPointer",
        "List",
        "ServiceAppointment"
       };

       MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And);
       EntityFilter.Conditions.Add(new MetadataConditionExpression("IsIntersect", MetadataConditionOperator.Equals, false));
       EntityFilter.Conditions.Add(new MetadataConditionExpression("OwnershipType", MetadataConditionOperator.Equals, OwnershipTypes.UserOwned));
       EntityFilter.Conditions.Add(new MetadataConditionExpression("SchemaName", MetadataConditionOperator.NotIn, excludedEntities));

       MetadataConditionExpression[] optionsetAttributesTypes = new MetadataConditionExpression[] { new MetadataConditionExpression("AttributeType", MetadataConditionOperator.Equals, AttributeTypeCode.Picklist) };
       MetadataFilterExpression AttributeFilter = new MetadataFilterExpression(LogicalOperator.Or);
       AttributeFilter.Conditions.AddRange(optionsetAttributesTypes);

       MetadataPropertiesExpression EntityProperties = new MetadataPropertiesExpression() { AllProperties = false };
       EntityProperties.PropertyNames.AddRange(new string[] { "Attributes" });
       MetadataPropertiesExpression AttributeProperties = new MetadataPropertiesExpression() { AllProperties = false };
       AttributeProperties.PropertyNames.Add("OptionSet");
       AttributeProperties.PropertyNames.Add("AttributeType");

       int _languageCode = RetrieveUserUILanguageCode(service, context.UserId);
       LabelQueryExpression labelQuery = new LabelQueryExpression();
       labelQuery.FilterLanguages.Add(_languageCode);

       EntityQueryExpression entityQueryExpression = new EntityQueryExpression()
       {
        Criteria = EntityFilter,
        Properties = EntityProperties,
        AttributeQuery = new AttributeQueryExpression()
        {
         Criteria = AttributeFilter,
         Properties = AttributeProperties
        },
        LabelQuery = labelQuery
       };

    //I would like to store and retain the timestamp in order to check for changes   

       RetrieveMetadataChangesResponse initialRequest = getMetadataChanges(service, entityQueryExpression, clientVersionStamp, DeletedMetadataFilters.OptionSet);

       string temp = string.Empty;
       if (initialRequest.Results.Values != null)
        foreach (EntityMetadata item in initialRequest.EntityMetadata) // I believe the mistake is here. How would I read what option set has changed and what value.
        {
         temp += item.HasChanged + "\n";
        }
       else
        throw new InvalidPluginExecutionException("metadata has not changed");

       throw new InvalidPluginExecutionException("count: " + initialRequest.EntityMetadata.Count + "\nattributes: " + temp);

      }

      protected int RetrieveUserUILanguageCode(IOrganizationService service, Guid userId)
      {
       QueryExpression userSettingsQuery = new QueryExpression("usersettings");
       userSettingsQuery.ColumnSet.AddColumns("uilanguageid", "systemuserid");
       userSettingsQuery.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, userId);
       EntityCollection userSettings = service.RetrieveMultiple(userSettingsQuery);
       if (userSettings.Entities.Count > 0)
       {
        return (int)userSettings.Entities[0]["uilanguageid"];
       }
       return 0;
      }
      
      protected RetrieveMetadataChangesResponse getMetadataChanges(
       IOrganizationService service,
       EntityQueryExpression entityQueryExpression,
       string clientVersionStamp,
       DeletedMetadataFilters deletedMetadataFilter
       )
      {
       RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest()
       {
        Query = entityQueryExpression,
        ClientVersionStamp = clientVersionStamp,
        DeletedMetadataFilters = deletedMetadataFilter
       };

       return (RetrieveMetadataChangesResponse)service.Execute(retrieveMetadataChangesRequest);

      }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans