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);
}