Skip to content

Instantly share code, notes, and snippets.

@pawelgradecki
Last active October 3, 2017 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelgradecki/7aee455d86c12144cee9caaeb01bf178 to your computer and use it in GitHub Desktop.
Save pawelgradecki/7aee455d86c12144cee9caaeb01bf178 to your computer and use it in GitHub Desktop.
Removes attributes that do not exist in the system anymore, from the solution
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