Removes attributes that do not exist in the system anymore, from the solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
var solutionUniqueName = "SOLUTIONNAME"; | |
var orgService = new CrmServiceClient(ConfigurationManager.ConnectionStrings["CRM"].ConnectionString); | |
var solutionComponentQuery = new QueryExpression("solutioncomponent"); | |
solutionComponentQuery.Distinct = true; | |
solutionComponentQuery.ColumnSet.AddColumns("objectid", "componenttype", "solutioncomponentid"); | |
var solutioncomponentSolutionLink = solutionComponentQuery.AddLink("solution", "solutionid", "solutionid"); | |
solutioncomponentSolutionLink.EntityAlias = "aa"; | |
solutioncomponentSolutionLink.LinkCriteria.AddCondition("uniquename", ConditionOperator.Equal, solutionUniqueName); | |
var results = orgService.RetrieveMultiple(solutionComponentQuery); | |
foreach (var item in results.Entities) | |
{ | |
if(item.GetAttributeValue<OptionSetValue>("componenttype").Value == 2) | |
{ | |
var attributeRequest = new RetrieveAttributeRequest | |
{ | |
MetadataId = item.GetAttributeValue<Guid>("objectid") | |
}; | |
try | |
{ | |
var result = (RetrieveAttributeResponse)orgService.Execute(attributeRequest); | |
Console.WriteLine(result.AttributeMetadata.LogicalName); | |
} | |
catch | |
{ | |
//attribute is missing, remove it from solution | |
Console.WriteLine($"Attribute {attributeRequest.MetadataId} is missing. Removing from the solution."); | |
var removeSolutionComponent = new RemoveSolutionComponentRequest() | |
{ | |
ComponentId = item.GetAttributeValue<Guid>("objectid"), | |
ComponentType = item.GetAttributeValue<OptionSetValue>("componenttype").Value, | |
SolutionUniqueName = solutionUniqueName | |
}; | |
orgService.Execute(removeSolutionComponent); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment