How do I get SharePoint items tagged with a term from the term store?
-
Monday, April 12, 2010 12:25 PM
Hi
I'm trying to create a web part that lists SP items by the terms (from a managed metadata store) they are tagged with. I can't find any Taxonomy web parts that could be extended (are there any?). Does anyone know how to query sharepoint for items tagged with taxonomy terms?
Answers
-
Monday, April 12, 2010 7:02 PM
Are you wanting to find items that have a particular term in a site collection, or across site collections?
In a site collection, there is a specific field attached to all items with a metadata field associated with them (The catch all field). If you call into the GetWssIdsOfTerm, you will get the full set of IDs that map to that term for that site collection. You can then perform a SiteDataQuery where the catch all field contains those IDs.
If you are wanting to go across site collections, you'll need to use Search. There is a single managed property (owstaxidmetadataalltagsinfo ) that contains all of the terms used on an item. If you query this property for the GUID of the term, you should get the full set of items tagged with that term.
Hoping to have a blog post up where this is discussed futher.
Pat.
All Replies
-
Monday, April 12, 2010 1:09 PM
There is the tag cloud web part. But in terms of code, I have some for managing the term store in my series about Enterprise Metadata Management in SharePoint 2010
MSDN page: Microsoft.SharePoint.Taxonomy NamespaceBut neither of these is an example from the viewpoint of gathering taxonomy data from various types of objects. If you find an example, please post a link here.
- cawood
blog | twitter -
Monday, April 12, 2010 4:15 PM
Yeah, managing the term store is not what I'm after.
I've found this GetWssIdsOfTerm but that seems to look up only values that you put in a managed metadata field, and although I do need to query list item's values of such fields, I also need to find which are tagged - which I can't seem to find.
Also the above method returns ids which need to be queried against the correct list with the correct field name (pls correct me if I'm wrong), which isn't generic enogh for me*. I need to just find if ANY item has been tagged with a term, or is using it in one of it's metadata fields. Are there any such methods?
Thanks
*especially as we don't know at design time what a user may have called a field in a list when setting it up to use the metadata term store. We need to be able to pull back items that "use" the terms from the term store regardless of how a user or admin may have set up the lists/libraries etc.
-
Monday, April 12, 2010 7:02 PM
Are you wanting to find items that have a particular term in a site collection, or across site collections?
In a site collection, there is a specific field attached to all items with a metadata field associated with them (The catch all field). If you call into the GetWssIdsOfTerm, you will get the full set of IDs that map to that term for that site collection. You can then perform a SiteDataQuery where the catch all field contains those IDs.
If you are wanting to go across site collections, you'll need to use Search. There is a single managed property (owstaxidmetadataalltagsinfo ) that contains all of the terms used on an item. If you query this property for the GUID of the term, you should get the full set of items tagged with that term.
Hoping to have a blog post up where this is discussed futher.
Pat.
-
Tuesday, April 13, 2010 7:57 AM
It's only within a site collection that I want, but I'd want to query sub sites of that site collection.
I've had a look at the GetWssIdsOfTerm, but one I have the term ids how do I then utilise that to search the entire site collection for sp items? At the moment I'm having to query each list which just doesn't scale so I'm obviously not doing the right thing.
Could you give an example of what to do with the IDs *after* GetWssIdsOfTerm to query the whole site collection?
Thx
UPDATE - sorry i missed the part of your post where you mention SiteDataQuery , will look into that asap thanks!
-
Tuesday, April 13, 2010 11:26 AM
I've tried using this code. It successfully gets the ID of a term which I've tagged a list item with (I've also used the term in a managed metadata field on the item), but when running the query it returns 0 items. Is there something wrong with my CAML query?
int wssId = SharePointTaxonomyHelper.GetWssIdByTermId(rootWeb, term.Id);
var query = new SPSiteDataQuery
{
Query = String.Format(
@"<Where><Eq><FieldRef Name='TaxCatchAll' LookupId='TRUE' /><Value Type='Lookup'>{0}</Value></Eq></Where>",
wssId),
Lists = "<Lists BaseType=\"1\" />",
ViewFields = "<FieldRef Name=\"Title\" />"
};
var results = rootWeb.GetSiteData(query);
if (results != null)
{
int count = results.Rows.Count;
} -
Tuesday, April 13, 2010 12:16 PM
I specified the wrong list basetype! duh! I needed 0 not 1
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsitedataquery.lists.aspx
-
Tuesday, April 13, 2010 12:50 PM
Hi
I removed the term from the value of the item's fields, as I wanted to check the tags. The code above doesn't get back items that have been tagged (which is what I was originally after). Do you know how to do that?
Thanks
-
Wednesday, April 14, 2010 8:52 AM
Ah, if you are looking for items that have been socially tagged, not actually edited in the content itself, you need to use the social tag feature. Look at the SocialTagManager class : http://msdn.microsoft.com/en-us/library/microsoft.office.server.socialdata.socialtagmanager_members%28v=office.14%29.aspx
Specifically, take a look at GetAllURLs
I don't know if you can specify a subset of the URLs - it didn't look like it through my scan of the methods, but I may have missed an override somewhere.
Hope that helps,
Pat.
-
Thursday, April 15, 2010 12:03 PM
Thanks - I'll take a look at that.
I'm going to mark the previous thread as the answer as after looking at exactly what's needed, it actually makes more sense to get taxonomy terms used in managed metadata fields rather than socially tagged items.
Thanks for you help
-
Friday, April 16, 2010 10:10 AM
Hi Pat
I'm not sure if you're getting updates to this thread, but if you are I was wondering if you could take a look at this related problem I've run intohttp://social.msdn.microsoft.com/Forums/en-US/sharepoint2010programming/thread/66fc38b1-94db-46ae-b4e5-9dccdb280be5
When I loop through the site query's results and go through the items' fields, the taxonomy fields bug out on items from subsites. Do you think it could be a sharepoint bug?
Thanks in advance
-
Sunday, October 31, 2010 9:17 PM
This is how we solved this issue. You need to substitute YourColumnName with the name of the column storing your tagged value and YourWssId with the Id of your term
int wssId = Int32.Parse(YourWssId); SPSiteDataQuery siteDataQuery = new SPSiteDataQuery(); siteDataQuery.Webs = "<Webs Scope=\"SiteCollection\">"; siteDataQuery.Lists = "<Lists ServerTemplate=\"850\" />"; // generate query siteDataQuery.Query = string.Format("<Where><Eq><FieldRef Name='YOURCOLUMNNAME' LookupId='TRUE' /><Value Type='Lookup'>{0}</Value></Eq></Where>", Settings.CurrentTopicWssId); // ensure all required fields are returned siteDataQuery.ViewFields = SiteQueryResultsProvider.ViewFields();//add any fields that you want returned here public static string ViewFields() { StringBuilder sb = new StringBuilder(); sb.Append("<FieldRef Name=\"Title\" />"); return sb.ToString(); }- Proposed As Answer by PixieTech Sunday, October 31, 2010 9:17 PM
-
Saturday, November 27, 2010 12:23 PMThanks input from this thread made me realize how to use Managed Metadata to find related content in SharePoint based on Managed MetaData. Blogpost and full sourcecode available here: http://pzl.no/hzTcPa

