【问题标题】:Getting all the Term Stores in Sharepoint 2010 (web services or client-side object model)?获取 Sharepoint 2010 中的所有术语库(Web 服务或客户端对象模型)?
【发布时间】:2012-12-01 06:58:34
【问题描述】:

Sharepoint 2010(不是 2013!)是否可以使用 Web 服务或客户端对象模型获取站点上所有术语库的列表? 我知道 2013 已经为它添加了一个库,但这对 2010 没有帮助。

如果不是整个列表,如果我知道一个术语(可能在也可能不在 TaxonomyHiddenList 中),我如何获得术语商店 ID?

【问题讨论】:

    标签: sharepoint-2010 client-side


    【解决方案1】:

    有人提到要检查 TaxonomyFieldType 字段,所以我将这两种方法组合在一起。我不知道这些是否适用于所有情况。

    第一个函数只返回存储在我们遇到的第一个 TaxonomyFieldType* 信息中的术语库 ID。

    public static string GetDefaultTermStore(string site) {
        var context = new ClientContext(site);
        var fields = context.Web.Fields;
        context.Load(fields, fs => fs.Include(f => f.SchemaXml, f => f.TypeAsString));
        context.ExecuteQuery();
        foreach (var field in fields) {
            if (field.TypeAsString.StartsWith("TaxonomyFieldType")) {
                var doc = XDocument.Parse(field.SchemaXml);
                var node = doc.XPathSelectElement("//Name[text()='SspId']/../Value");
                if (node != null && !string.IsNullOrEmpty(node.Value)) {
                    return node.Value;
                }
            }
        }
        throw new Exception("Term Store ID not found!");
    }
    

    第二个函数遍历所有字段并获取所有可能的术语库 ID 并将它们返回到一个列表中。

    public static List<string> GetTermStores(string site) {
        var context = new ClientContext(site);
        var fields = context.Web.Fields;
        context.Load(fields, fs => fs.Include(f => f.SchemaXml, f => f.TypeAsString));
        context.ExecuteQuery();
        var hashlist = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
        foreach (var field in fields) {
            if (field.TypeAsString.StartsWith("TaxonomyFieldType")) {
                var doc = XDocument.Parse(field.SchemaXml);
                var node = doc.XPathSelectElement("//Name[text()='SspId']/../Value");
                if (node != null && !string.IsNullOrEmpty(node.Value)) {
                    if (!hashlist.Contains(node.Value)) {
                        hashlist.Add(node.Value);
                    }
                }
            }
        }
        if (hashlist.Count == 0) throw new Exception("No Term Store IDs not found!");
        return hashlist.ToList();
    }
    

    这是对我的问题的正确答案吗?有人有更确定的方法来获取 ID 吗?

    似乎没有其他人对这个问题有很好的答案。 我已经添加了我在下面创建的实用程序类。下面的大块未注释代码供可能需要的人使用:

    using Microsoft.SharePoint.Client;
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web.Services.Protocols;
    using System.Windows.Forms;
    using System.Xml.Linq;
    using System.Xml.XPath;
    
    namespace VitaminTilKanbanPusher.Sharepoint {
        public class SharepointTaxonomyAgent {
    
            //URLS:
            //http://www.novolocus.com/2012/02/06/working-with-the-taxonomyclientservice-part-1-what-fields-are-there/
            //
    
            public static void Test() {
    
                var site = ConfigurationManager.AppSettings["VitaminSite"];
                //var list = ConfigurationManager.AppSettings["VitaminList"];
                //var id = GetDefaultTermStore(site);
                //var ids = GetTermStores(site);
                var rs = GetAllTermSetNames(site);
                var ts = GetTermSetTerms(site, "Some Name");
                //var ts = GetTermSetTerms(site, "Some other name");
    
                //var term = GetTermInfo(site, "Priority");
                //var term2 = GetTermInfo(site, "My term");
                //var termset = GetTermSetInfo(site, "My term");
                //var termsets = GetTermSets(site, "My term");
    
    
            }
    
            public static string GetDefaultTermStore(string site) {
                var context = new ClientContext(site);
                context.ExecutingWebRequest += ctx_MixedAuthRequest;
                var fields = context.Web.Fields;
                context.Load(fields, fs => fs.Include(f => f.InternalName, f => f.SchemaXml, f => f.TypeAsString));
                context.ExecuteQuery();
    
                foreach (var field in fields) {
                    //field.InternalName== "TaxKeyword" -> possibly default?
                    if (field.TypeAsString.StartsWith("TaxonomyFieldType")) {
                        var doc = XDocument.Parse(field.SchemaXml);
                        var node = doc.XPathSelectElement("//Name[text()='SspId']/../Value");
                        if (node != null && !string.IsNullOrEmpty(node.Value)) {
                            return node.Value;
                        }
                    }
                }
                throw new Exception("Term Store ID not found!");
            }
    
    
            public static List<string> GetTermStores(string site) {
                var context = new ClientContext(site);
                context.ExecutingWebRequest += ctx_MixedAuthRequest;
                var fields = context.Web.Fields;
                context.Load(fields, fs => fs.Include(f => f.SchemaXml, f => f.TypeAsString));
                context.ExecuteQuery();
    
                var hashlist = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
                foreach (var field in fields) {
                    if (field.TypeAsString.StartsWith("TaxonomyFieldType")) {
                        var doc = XDocument.Parse(field.SchemaXml);
                        var node = doc.XPathSelectElement("//Name[text()='SspId']/../Value");
                        if (node != null && !string.IsNullOrEmpty(node.Value)) {
                            if (!hashlist.Contains(node.Value)) {
                                hashlist.Add(node.Value);
                            }
                        }
                    }
                }
                if (hashlist.Count == 0) throw new Exception("No Term Store IDs not found!");
                return hashlist.ToList();
            }
    
            private static List<TermSet> _termSets;
            public static List<TermSet> GetAllTermSetNames(string site, string onlySpecificTermSetName = null) {
                if (_termSets != null) {
                    if (onlySpecificTermSetName == null) return _termSets;
                    foreach (var ts in _termSets) {
                        if (ts.Name.Equals(onlySpecificTermSetName, StringComparison.InvariantCultureIgnoreCase)) {
                            return new List<TermSet>() { ts };
                        }
                    }
                    return new List<TermSet>();
                }
    
                var context = new ClientContext(site);
                context.ExecutingWebRequest += ctx_MixedAuthRequest;
                var fields = context.Web.Fields;
                context.Load(fields, fs => fs.Include(f => f.SchemaXml, f => f.TypeAsString));
                context.ExecuteQuery();
    
                var hashlist = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
                var termSets = new List<TermSet>();
    
                TermSet theChosenTermSet = null;
    
                foreach (var field in fields) {
                    if (field.TypeAsString.StartsWith("TaxonomyFieldType")) {
                        var ts = new TermSet();
                        var doc = XDocument.Parse(field.SchemaXml);
    
                        var fn = doc.Element("Field");
                        if (fn == null) continue;
    
                        if (fn.Attribute("DisplayName") == null) continue;
                        if (fn.Attribute("ID") == null) continue;
    
                        ts.Name = fn.Attribute("DisplayName").Value;
    
                        //Only 1 set?
                        if (onlySpecificTermSetName != null) {
                            if (!ts.Name.Equals(onlySpecificTermSetName, StringComparison.InvariantCultureIgnoreCase)) {
                                theChosenTermSet = ts;
                            }
                        }
    
                        if (fn.Attribute("Description") != null) {
                            ts.Description = fn.Attribute("Description").Value;
                        }
    
                        var node = doc.XPathSelectElement("//Name[text()='SspId']/../Value");
                        if (node != null && !string.IsNullOrEmpty(node.Value)) {
                            ts.TermStoreId = node.Value;
                        }
    
                        var node2 = doc.XPathSelectElement("//Name[text()='TermSetId']/../Value");
                        if (node2 != null && !string.IsNullOrEmpty(node2.Value)) {
                            ts.Id = node2.Value;
                        }
                        else {
                            continue; //No ID found
                        }
    
                        //Unique hites
                        if (!hashlist.Contains(ts.Id)) {
                            hashlist.Add(ts.Id);
                            termSets.Add(ts);
                        }
                    }
                }
                _termSets = termSets;
    
                if (onlySpecificTermSetName != null) return (theChosenTermSet == null ? new List<TermSet>() : new List<TermSet>() { theChosenTermSet });
    
                return termSets;
            }
    
            public static TermSet GetTermSetTerms(string site, string termName) {
                var ts = GetAllTermSetNames(site, termName);
                if (ts.Count == 0) throw new Exception("Could not find termset: " + termName);
                var theTermSet = ts[0];
    
                var proxy = new SharepointTaxWS.Taxonomywebservice();
                proxy.UseDefaultCredentials = true;
                proxy.PreAuthenticate = true;
                proxy.Url = Path.Combine(site, "_vti_bin/taxonomyclientservice.asmx");
                GetAuthCookie(proxy, site);
    
                var lciden = 1033; //var lcidno = 1044; // System.Globalization.CultureInfo.CurrentCulture.LCID
    
                var clientTime = DateTime.Now.AddYears(-2).ToUniversalTime().Ticks.ToString();
                var termStoreId = new Guid(theTermSet.TermStoreId);// Guid.Parse(theTermSet.TermStoreId);
                var termSetId = new Guid(theTermSet.Id);
    
                string clientTimestamps = string.Format("<timeStamp>{0}</timeStamp>", clientTime);
                string clientVersion = "<version>1</version>";
                string termStoreIds = string.Format("<termStoreId>{0}</termStoreId>", termStoreId.ToString("D"));
                string termSetIds = string.Format("<termSetId>{0}</termSetId>", termSetId.ToString("D"));
                string serverTermSetTimestampXml;
                string result = proxy.GetTermSets(termStoreIds, termSetIds, 1033, clientTimestamps, clientVersion, out serverTermSetTimestampXml);
    
    
                var term = ParseTermSetInfo(result);
                term.Description = theTermSet.Description;
                term.Id = theTermSet.Id;
                term.Name = theTermSet.Name;
                return term;
            }
    
    
            //public static Term GetTermSetInfo(string site, string termName) {
    
            //    var proxy = new SharepointTaxWS.Taxonomywebservice();
            //    proxy.UseDefaultCredentials = true;
            //    proxy.PreAuthenticate = true;
            //    proxy.Url = Path.Combine(site, "_vti_bin/taxonomyclientservice.asmx");
    
            //    GetAuthCookie(proxy, site);
    
            //    var lciden = 1033; //var lcidno = 1044; // System.Globalization.CultureInfo.CurrentCulture.LCID
    
            //    var sets = proxy.GetChildTermsInTermSet(Guid.Parse(""), lciden, Guid.Parse("termsetguid"));
    
            //    var term = ParseTermInfo(sets);
    
            //    return term;
            //}
    
            public static Term GetTermInfo(string site, string termName) {
    
                var proxy = new SharepointTaxWS.Taxonomywebservice();
                proxy.UseDefaultCredentials = true;
                proxy.PreAuthenticate = true;
                proxy.Url = Path.Combine(site, "_vti_bin/taxonomyclientservice.asmx");
    
                GetAuthCookie(proxy, site);
    
                var lciden = 1033; //var lcidno = 1044; // System.Globalization.CultureInfo.CurrentCulture.LCID
    
                var sets = proxy.GetTermsByLabel(termName, lciden, SharepointTaxWS.StringMatchOption.StartsWith, 100, null, false);
    
                var term = ParseTermInfo(sets);
    
                return term;
            }
    
    
            private static TermSet ParseTermSetInfo(string xml) {
                //Not done
                var info = XDocument.Parse(xml);
                var ts = new TermSet();
                ts.Terms = new List<Term>();
    
                var n1 = info.XPathSelectElements("//T");
                if (n1 != null) {
                    foreach (var item in n1) {
                        var t = new Term();
                        t.Id = item.Attribute("a9").Value;
                        t.Name = item.XPathSelectElement("LS/TL").Attribute("a32").Value;
                        t.TermSet = ts;
                        ts.Terms.Add(t);
                    }
    
                }
                return ts;
            }
    
            private static Term ParseTermInfo(string xml) {
    
                var info = XDocument.Parse(xml);
                var t = new Term();
                var ts = new TermSet();
    
                var n1 = info.XPathSelectElement("TermStore/T");
                var n2 = info.XPathSelectElement("TermStore/T/LS/TL");
                var n3 = info.XPathSelectElement("TermStore/T/TMS/TM");
    
                if (n1 != null && n1.Attribute("a9") != null) {
                    t.Id = n1.Attribute("a9").Value;
                }
                if (n2 != null && n2.Attribute("a32") != null) {
                    t.Name = n2.Attribute("a32").Value;
                }
                if (n3 != null && n3.Attribute("a24") != null) {
                    ts.Id = n3.Attribute("a24").Value;
                }
                if (n3 != null && n3.Attribute("a12") != null) {
                    ts.Name = n3.Attribute("a12").Value;
                }
                t.TermSet = ts;
                return t;
            }
    
    
            private static CookieCollection _theAuth;
            private static bool _bNoClaims;
            static void GetAuthCookie(SoapHttpClientProtocol proxy, string site) {
                return;
                //if (_bNoClaims) {
                //    return; //Ingen claims. 
                //}
                //// get the cookie collection - authentication workaround
                //CookieCollection cook = null;
                //try {
                //    if (_theAuth == null) {
                //        cook = ClaimClientContext.GetAuthenticatedCookies(site, 925, 525);
                //    }
                //    else {
                //        cook = _theAuth;
                //    }
    
                //    _theAuth = cook; 
                //    _bNoClaims = false;
                //}
                //catch (ApplicationException ex) {
                //    if (ex.Message.Contains("claim")) _bNoClaims = true;
                //    Console.Write("Auth feilet: " + ex.Message + " - ");
                //    //IGNORE
                //}
                //if (_theAuth != null) {
                //    proxy.CookieContainer = new CookieContainer();
                //    proxy.CookieContainer.Add(_theAuth);
                //}
            }
    
    
            static void ctx_MixedAuthRequest(object sender, WebRequestEventArgs e) {
                //add the header that tells SharePoint to use Windows Auth
                e.WebRequestExecutor.RequestHeaders.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            }
    
        }
    
    
    
        public class TermSet {
            public string Id { get; set; }
            public string Name { get; set; }
            public List<Term> Terms { get; set; }
            public string TermStoreId { get; set; }
    
            public string Description { get; set; }
    
            public override string ToString() {
                int tc = 0;
                if (Terms != null) tc = Terms.Count;
                return Name + "|" + Id + " (" + tc + "terms)";
            }
        }
    
        public class Term {
            public string Id { get; set; }
            public string Name { get; set; }
            public TermSet TermSet { get; set; }
            public override string ToString() {
                return Name + "|" + Id;
            }
        }
    
    }
    

    【讨论】:

    • 嗨@Wolf5 我也在同样的情况下苦苦挣扎 我有一个名为 Products under Taxonomy 的术语商店,需要从该商店获取所有 fieldValues,想知道您是否终于找到了解决方案这个吸血问题?
    • 没有。对不起。最终使用上面的代码。解决了我的问题。
    • 我已经添加了我工作的结果代码。让我从共享点列表(分类字段等)中提取我需要的所有信息。
    • 谢谢@Wolf5 我会尝试从上面的代码中受益,但上面的函数似乎不是获取 TermStore 值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2023-03-17
    • 1970-01-01
    • 2013-03-02
    • 2013-03-22
    • 1970-01-01
    • 2011-07-15
    相关资源
    最近更新 更多