Taxonomies and Queries in Migration, (Determine Taxonomy Is by Term)

Jun 14, 2013 at 6:28 AM
I am attempting to create a custom Query through a DataMigration, and I'd like to be able to add a new FilterRecord for a Taxonomy, so that the Query is filtering by some particular Taxonomy Term.

The problem is I cannot predict which TermIds will eb related to which Taxonomies at the time of creation. I am using code similar to this:

First, I am exposing the services necessary in the Migrations Constructor
 public Migrations(IMenuService menuService,
                          IContentManager contentManager,
                          IQueryService queryService,
                          IWidgetsService widgetsService,
                          ITaxonomyService taxonomyService,
                            IProjectionManager projectionManager) {

            _menuService = menuService;
            _contentManager = contentManager;
            _queryService = queryService;
            _widgetsService = widgetsService;
            _taxonomyService = taxonomyService;
            _projectionManager = projectionManager;
Then afterwards, I am creating a Query
var someQuery = _queryService.CreateQuery("SOmeQueryName");
then I am making a new filterrecord on my FilterGroup
var taxForm = GenerateXmlFormState(new TaxonomyForm { TermIds = "29" });

private string GenerateXmlFormState(TaxonomyForm form)
            var xmlSerializer = new XmlSerializer(form.GetType());
            StringWriter sww = new StringWriter();
            XmlWriter writer = XmlWriter.Create(sww);
            xmlSerializer.Serialize(writer, form);
            var state = sww.ToString();
            return state;

            someQuery.FilterGroups[0].Filters.Add(new FilterRecord
                Category = "Taxonomy",
                Description = "My Description",
                Position = 0,
                State = taxForm,
                Type = "HasTerms"
And the State has an output similar to
<?xml version="1.0" encoding="utf-16"?><TaxonomyForm xmlns:xsi="" xmlns:xsd=""><TermIds>30</TermIds></TaxonomyForm>
I suppose the question is this: I dont necessarily know what my Term Id will be. I know what the Text Value of the particular term will be, but not its Id. Is there any way I can find that out? Looking through the ITaxonomyService I see ways to get a taxonomy if you already know its Id, but not the other way around. any advice is greatly appreciated!