Using jQuery to return the Managed Metadata Term GUID #SP2010 #SharePoint #in
For a completely different client who is using SharePoint 2010 for their company intranet we needed to implemented a UI web part which allowed end users to click on keywords which would then pre-filter search.
Keywords were entered in using the new Managed Metadata feature of SharePoint 2010.
However, during development we found that SharePoint Search for Managed Metadata does not use just the standard ?k= query string parameter to run the search query.
A new query string parameter is available especially for Managed Metadata:
- &r=
And the expected format is similar to the following:
- “ManagedMetadataTerm:TermGUID”
NB: I will probably explain the above in more detail in a later post
Anyway, when querying the lists.asmx web service and returning managed metadata columns, only the values are returned, not the GUID’s which poses us a problem for restricting search.
So after some research I came up with the following jQuery function calling the taxonomyClientService.asmx web service introduced in SharePoint 2010.
It’s is pretty basic at the moment but suits the requirement for my client in their setup:
Code Example:
1 <script type="text/javascript"> 2 $(document).ready(function() { 3 getTermsByLabel("Project Server"); //pass your variable in here; 4 }); 5 6 function getTermsByLabel(termString) { 7 var soapEnv = 8 "<?xml version='1.0' encoding='utf-8'?> \ 9 <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> \ 10 <soap:Body> \ 11 <GetTermsByLabel xmlns='http://schemas.microsoft.com/sharepoint/taxonomy/soap/'>\ 12 <label>" + termString + "</label>\ 13 <lcid>1033</lcid>\ 14 <matchOption>ExactMatch</matchOption>\ 15 <resultCollectionSize>100</resultCollectionSize>\ 16 <addIfNotFound>0</addIfNotFound>\ 17 </GetTermsByLabel>\ 18 </soap:Body> \ 19 </soap:Envelope>"; 20 //alert(soapEnv); 21 $.ajax({ 22 url: "/_vti_bin/taxonomyclientservice.asmx", 23 type: "POST", 24 dataType: "xml", 25 data: soapEnv, 26 complete: processResult, 27 contentType: "text/xml; charset=\"utf-8\"" 28 }); 29 } 30 31 function processResult(xData, status) { 32 //alert(status); 33 //alert(xData.responseText); 34 35 $(xData.responseXML).find("GetTermsByLabelResult").each(function() { 36 37 var outputValue = $(this).text(); 38 var a9Array = outputValue.split("a9="); 39 var a21Array = a9Array[1].split("a21="); 40 var termGUID = a21Array[0].replace(/"/g, ""); 41 termGUID = white_space(termGUID); 42 alert("-=" + termGUID + "=-"); 43 44 }); 45 46 } 47 48 function white_space(stringValue) { 49 stringValue = (stringValue).replace(/^\s*|\s*$/g,''); 50 return stringValue; 51 } 52 53 </script> 54
Links I found useful during my research:
- TaxonomyClientService.asmx MSDN Reference:
- GetTermsByLabel MSDN Reference:
- Blog Post 1 – .NET Examples:
- Blog Post 2 – .NET Examples + Analysis of the XML output:
Leave a Reply Cancel reply
Recommended Podcast
Top Posts
- Office 365 Products Visio Stencil & Icons available (updated for 2019) #o365 #visio
- Uploading an Existing Local Git Repository to BitBucket.
- Excel formulas not auto calculating but F9 still works #in
- Automating SharePoint 2013 Usage Reports
- Take control of your log files
- So, you want to delete users with the Azure AD Graph API? Good luck with that!
- Sharepoint 2013 “mark task complete”
- Timesheet Managers in Project Server 2013
- Current Year, Month and Quarter in JavaScript #SP2010 #SharePoint #PS2010 #MSProject #ProjectServer #in
- Useful JavaScript Function: PreSaveAction #SP2010 #SharePoint #PS2010 #ProjectServer #in
Archives
Tags
Blog Stats
- 606,319 hits
Brilliant. This exactly what I needed, Giles… thanks.
Hey Giles, you run into a access denied with this solution? I have been trying to incorporate this call into an Office App. I would think it would impersonate the user opening the word app but that does not appear to be the case…