0

Sharepoint 2010(2013ではありません!)を使用して、Webサービスまたはクライアント側オブジェクトモデルのいずれかを使用して、サイト上のすべての用語ストアのリストを取得することは可能ですか?2013年にライブラリが追加されたことは知っていますが、2010年には役に立ちません。

リスト全体ではない場合、用語を知っている場合(TaxonomyHiddenListに含まれる場合と含まれない場合があります)、用語ストアIDを取得するにはどうすればよいですか?

4

1 に答える 1

0

誰かがTaxonomyFieldTypeフィールドをチェックアウトすることに言及したので、私はこれら2つのメソッドを一緒にハックしました。これらがすべての状況で機能するかどうかはわかりません。

最初の関数は、最初に取得した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!");
}

2番目の関数は、すべてのフィールドを調べて、すべての可能な用語ストア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;
        }
    }

}
于 2012-12-13T12:10:43.003 に答える