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();
                }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值