Blog Stats

Getting the Value of a Managed Metadata Column with jQuery

There was a post from Roger Langedal over on SPYam the other day asking about getting the value of a Managed Metadata column using jQuery.

I hadn’t tried this before, so I thought I should give it a go.

[important]By the way, if you are a SharePoint person (you’ve arrived here, so I think we can assume it’s pretty likely you are) and you aren’t a member of SPYam, which is the Yammer-based SharePoint community created by Joel Oleson (@joeloleson), then you should be. If you’d like an invitation, ping me and I’ll send you one. UPDATE 2013-01-04: Actually, anyone can request membership themselves at this point. Just go to SPYam and sign up.[/important]

The basics are pretty simple. The Managed Metadata Picker (or perhaps it should be called a Taxonomy Picker, given the CSS classes that Microsoft uses within it) is embedded within the form much like the People Picker is. Luckily there is a pretty easy way to identify it in the page. When you dig into the markup in the DOM, there’s a div which has the column name as its Title attribute.

In my experiment, I’ve got a column called Office, and here’s what that div looks like:

There’s another div within that one, and within the second div, you’ll see several spans.

If there are spans with the div which have the CSS class valid-text, then they contain the valid value(s) you’ve selected for the picker. For example, Newton Centre below.

If there are spans within the div with the CSS class invalid-text, they contains the entered value(s) which aren’t legitimate values for that column. For instance, if I type in Hanover for my Office (which isn’t one of the allowed values), it’ll be in one of these spans.

So getting at the acceptable values is fairly simple with jQuery. Again, this is a first pass, but it should be serviceable.

From here, you could do whatever you’d like with the values. They are in an array, so if you’d like to save them to another column, you’ll probable want to join them into a single string. The code below will join the values together into one nice, displayable string, with the values separated by commas. If there’s just a single value, that the string will be that single value.

5 Comments

Thanks for sharing Marc,
There is also a hidden field that you can read the values from, slightly easier to extract the terms as all the values are stored in the value attribute, separated by a semicolon. There is no title attribute on it so you can’t use as clean of selector as you show, but if you have only one MMF on a form, something like this should do the trick input[type=’hidden’][class^=’ms-taxonomy’]
For invalid values it adds a label with an empty guid (zeros only).

Good point. The value for that hidden input is “Newton Centre|977e6e98-2515-4426-92eb-6bbbb45eab20;hanover|00000000-0000-0000-0000-000000000000” in my example above. It’s a little messier to parse out, but as you point out, it contains everything.

Marc, could you elaborate on why you recommend to join SPYam? At this point, I don’t see a clear added value, compared to LinkedIn groups for example. And a major drawback is that the network is private, so threads are not indexed by search engines and this limits their reach.

SPYam seems to have the critical mass that I don’t see in any of the LinkedIn groups, which are simply very fragmented. I’m a member of about 10 of the LinkedIn groups, and the content just doesn’t have the value I see in SPYam.

The other clear benefit is that there are Microsoft Product Group people who are active in SPYam, which I also don’t see in any of the LinkedIn groups.

Good points, thanks Marc. It’s true that a social network is about the community it draws, not just the features it offers, and the involvement from Joel Oleson and Microsoft people has to count for something!

Think you’ve got great Active Directory data?

Try the FREE Hyperfish Analyzer
Find out just how bad your Active Directory data really is. In your custom report you will see how you are faring with key attributes including Profile Pictures, Phone Numbers, and Job Titles, as well as learn the value of an up to date Directory.