sharepoint添加自定義Term組、TermSet、Term

 

項目需求:從網站的list裏面讀取數據添加到Term術語裏面,是個自定義的術語組,然後添加TermSet、Term。

public class TimerSocialTag
    {
       //控制檯應用程序中訪問地址要寫成固定的
       public string webUrl = "http://s3-sp";
        //向數據倉庫裏面添加數據
        public void AddSocialTag() {
            SPSite site = new SPSite(webUrl);
            if (site != null)
            {
                //從網站中獲取TermStore
                TermStore termStore = GetATermStore(site);
                if (termStore != null)
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        //WebSite是術語組名字
                        if (termStore.Groups["WebSite"] != null)
                        {
                            // 獲取名爲WebSite的組
                            Group group = termStore.Groups["WebSite"];
                            TestTermCodeSamples.TestGetTerms(termStore, group);
                        }
                    });
                }
            }
        }
        //獲取網站的TermStore
        public TermStore GetATermStore(SPSite site)
        {
            TermStore termStore = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                // Get a TaxonomySession from the site
                TaxonomySession session = new TaxonomySession(site);
                // Get a TermStore from the session
                if (session.TermStores != null && session.TermStores.Count > 0)
                {
                    termStore = session.TermStores[0];
                }
            });
            return termStore;
        }
    }

    //向組裏添加術語集及術語
    public class TestTermCodeSamples
    {
        public static void TestGetTerms(TermStore termStore, Group group)
        {
            if (termStore == null)
            {
                throw new System.ArgumentNullException("WebSite");
            }
            if (group == null)
            {
                throw new System.ArgumentNullException("group");
            }
            // get current thread lcid
            int lcid = CultureInfo.CurrentCulture.LCID;
            // create term set“[]"裏面的值必須存在
            if (group.TermSets[""] != null)
            {
                //group.TermSets[""].Delete();
                TermSet termSet = group.TermSets[""];
                //"[]"裏的值必須存在
                if (termSet.Terms[""] != null)
                {
                    termSet.Terms[""].Delete();
                    Term term = termSet.CreateTerm("", lcid);
                    //調用數據倉庫
                    TimerSocialTag timerTag = new TimerSocialTag();
                    using (SPWeb spWeb = new SPSite(timerTag.webUrl + "/DataDepot").OpenWeb())
                    {
                        SPList sp_list = spWeb.Lists["Hits"];
                        //調用列表[Hits]大小寫字母注意
                        //SPListItemCollection spListColl = spWeb.Lists["Hits"].Items;
                        SPQuery sp_q = new SPQuery();
                        sp_q.Query = @"<OrderBy><FieldRef Name='HitCount' Ascending='False' />" +
                                    "</OrderBy><Where><Lt><FieldRef Name='ID' /><Value Type='Counter'>20</Value></Lt></Where>";
                        SPListItemCollection spListColl = sp_list.GetItems(sp_q);
                        foreach (SPListItem spListItem in spListColl)
                        {
                            //獲取HitName名字
                            string hitName = spListItem["HitName"].ToString();
                            // 創建terms
                            term.CreateTerm(hitName, lcid);
                        }
                    }
                    //用來讀取terms的
                    //Term termFri = term.CreateTerm("Fri", lcid);

                    //更新
                    termStore.CommitAll();
                }

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章