Sharepoint 2010 获取Sharepoint 2010中的所有术语库(web服务或客户端对象模型)?

Sharepoint 2010 获取Sharepoint 2010中的所有术语库(web服务或客户端对象模型)?,sharepoint-2010,client-side,Sharepoint 2010,Client Side,Sharepoint 2010(不是2013!)是否可以使用web服务或客户端对象模型获取网站上所有术语库的列表? 我知道2013年已经为它添加了一个库,但这对我在2010年不会有任何帮助 如果不是整个列表,如果我知道一个术语(可能在分类hiddenlist中,也可能不在分类hiddenlist中),如何获取术语库ID?有人提到要检查分类字段,所以我将这两个方法组合在一起。我不知道这些措施是否在所有情况下都有效 第一个函数只返回术语存储ID,它存储在我们遇到的第一个TaxonomyFieldTy

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


如果不是整个列表,如果我知道一个术语(可能在分类hiddenlist中,也可能不在分类hiddenlist中),如何获取术语库ID?

有人提到要检查分类字段,所以我将这两个方法组合在一起。我不知道这些措施是否在所有情况下都有效

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

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();
}
公共静态列表GetTermStores(字符串站点){
var context=新的ClientContext(站点);
变量字段=context.Web.fields;
Load(字段,fs=>fs.Include(f=>f.SchemaXml,f=>f.TypeAsString));
context.ExecuteQuery();
var hashlist=新的HashSet(StringComparer.InvariantCultureInogoreCase);
foreach(字段中的变量字段){
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)){
如果(!hashlist.Contains(node.Value)){
hashlist.Add(node.Value);
}
}
}
}
如果(hashlist.Count==0)抛出新异常(“找不到术语库ID!”);
返回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;
        }
    }

}
使用Microsoft.SharePoint.Client;
使用制度;
使用System.Collections.Generic;
使用系统配置;
使用System.IO;
使用System.Linq;
Net系统;
使用系统文本;
使用System.Web.Services.Protocols;
使用System.Windows.Forms;
使用System.Xml.Linq;
使用System.Xml.XPath;
命名空间VitaminTilKanbanPusher.Sharepoint{
公共类SharePointTaxyAgent{
//网址:
//http://www.novolocus.com/2012/02/06/working-with-the-taxonomyclientservice-part-1-what-fields-are-there/
//
公共静态无效测试(){
var site=ConfigurationManager.AppSettings[“VitaminSite”];
//var list=ConfigurationManager.AppSettings[“维生素列表”];
//var id=GetDefaultTermStore(站点);
//var id=GetTermStores(站点);
var rs=GetAllTermSetNames(站点);
var ts=GetTermSetTerms(站点,“某些名称”);
//var ts=GetTermSetTerms(站点,“其他名称”);
//var term=GetTermInfo(站点,“优先级”);
//var term2=GetTermInfo(站点,“我的术语”);
//var termset=GetTermSetInfo(站点,“我的术语”);
//var termsets=GetTermSets(站点,“我的术语”);
}
公共静态字符串GetDefaultTermStore(字符串站点){
var context=新的ClientContext(站点);
context.ExecutingWebRequest+=ctx_MixedAuthRequest;
变量字段=context.Web.fields;
Load(字段,fs=>fs.Include(f=>f.InternalName,f=>f.SchemaXml,f=>f.TypeAsString));
context.ExecuteQuery();
foreach(字段中的变量字段){
//field.InternalName==“TaxKeyword”->是否可能是默认值?
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)){
返回节点值;
}
}
}
抛出新异常(“未找到术语库ID!”);
}
公共静态列表GetTermStores(字符串站点){
var context=新的ClientContext(站点);
context.ExecutingWebRequest+=ctx_MixedAuthRequest;
变量字段=context.Web.fields;
Load(字段,fs=>fs.Include(f=>f.SchemaXml,f=>f.TypeAsString));
context.ExecuteQuery();
var hashlist=新的HashSet(StringComparer.InvariantCultureInogoreCase);
foreach(字段中的变量字段){
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)){
如果(!hashlist.Contains(node.Value)){
hashlist.Add(node.Value);
}
}
}
}
如果(hashlist.Count==0)抛出新异常(“找不到术语库ID!”);
返回hashlist.ToList();
}
专用静态列表_术语集;
公共静态列表GetAllTermSetNames(字符串站点,仅字符串SpecificterMsetName=null){
如果(_termSets!=null){
if(onlySpecificTermSetName==null)返回_termSets;
foreach(变量ts在_termset中){
if(ts.Name.Equals(仅限SpecificterMsetName、StringComparison.InvariantCultureIgnoreCase)){
返回新列表(){ts};
}
}
返回新列表();
}
var context=新的ClientContext(站点);
context.ExecutingWebRequest