Click here to Skip to main content
Click here to Skip to main content

SPQuery and SPSiteDataQuery Managed Metadata columns

, 29 Nov 2010
Rate this:
Please Sign up or sign in to vote.
SharePoint 2010 supports Managed Metadata and of course you can include a column of this type in any list definition. However, when querying a list and specifying a Manage Metadata column in the ViewFields different information is returned depending on … Continue reading →

SharePoint 2010 supports Managed Metadata and of course you can include a column of this type in any list definition. However, when querying a list and specifying a Manage Metadata column in the ViewFields, different information is returned depending on which type of query you use.

For testing, I created a new Managed Metadata Service Group and TermSet with a couple Terms, nothing outlandish.

Termstore

Then I created a Custom List and added a Managed Metadata column named “Tax” that references the MySet TermSet.

TaxList

For testing purposes, I created a very simple and minimal console application. As you can see from the code below, both methods shared the ViewFields so I expected the same results in the Tax field.

private static string VIEW_FIELDS = "<FieldRef Name='Title'/><FieldRef Name='Tax'/>";

static void Main(string[] args)
{
   using(SPSite site = new SPSite(http://MyServer))
   {
      using(SPWeb web = site.OpenWeb())
      {
         SPList list = web.Lists.TryGetList("TaxTest");
         UseSPQuery(list);
         UseSPSiteDataQuery(web);
      }
   }
   Console.ReadLine();
}

private static void UseSPQuery(SPList list)
{
   SPQuery query = new SPQuery();
   query.ViewFields = VIEW_FIELDS;    
   DataTable dt = list.GetItems(query).GetDataTable();    
   Console.WriteLine("SPQuery:\t\t{0}", dt.Rows[0]["Tax"]);
}

private static void UseSPSiteDataQuery(SPWeb web)
{   
   SPSiteDataQuery query = new SPSiteDataQuery();   
   query.ViewFields = VIEW_FIELDS;    
   DataTable dt = web.GetSiteData(query);   
   Console.WriteLine("SPSiteDataQuery:\t{0}", dt.Rows[0]["Tax"]);
}

The results however were this:

Result1

As you can see, the SPQuery returned the Managed Metadata field as the Term name then the Term ID while the SPSiteDataQuery returned it as WssId then the Term name.

To correct this and have both queries return the same, you need to set the ViewFieldsOnly property to true for the SPQuery.

SPQuery query = new SPQuery();
query.ViewFields = VIEW_FIELDS;
query.ViewFieldsOnly = true;

With this property set, the results are now what are expected.

Result2


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 29 Nov 2010
Article Copyright 2010 by Not Active
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid