最近项目中遇到如何用代码给托管元数据类型的栏目赋值问题,经过折腾,现把我的思路和实现方法共享出来,让大家一起来学习学习。相互探讨下。

 /// <summary>
/// 托管元数据
/// </summary>
public class SPTaxonomyEntity
{
/// <summary>
/// 托管元数据的guid
/// </summary>
public string Guid
{
get;
set;
}
/// <summary>
/// 托管元数据的名称
/// </summary>
public string Name
{
get;
set;
}
}

#region//变量
/// <summary>
/// 子类托管元数据的集合对象
/// </summary>
private static List<SPTaxonomyEntity> termcollection = new List<SPTaxonomyEntity>();
#endregion

#region//私有方法
#region//给托管元数据类型赋值
/// <summary>
/// 给托管元数据类型赋值
/// </summary>
/// <param name="site">当前网站集</param>
/// <param name="list">当前列表</param>
/// <param name="listItem">当前列表项</param>
/// <param name="fieldDisplayName">当前栏目显示名称</param>
/// <param name="taxName">节点元数据的名称</param>
private static void SetTaxonomyValue(SPSite site, SPList list, SPListItem listItem, string fieldDisplayName, string taxName)
{
try
{
//得到值
SPTaxonomyEntity entity = GetCurrentTermToName(site, listItem, fieldDisplayName, taxName);
//赋值
if (entity != null && !string.IsNullOrEmpty(entity.Guid))
{
//得到字段
TaxonomyField taxonomyField = list.Fields[fieldDisplayName] as TaxonomyField;
//字段类型值
TaxonomyFieldValue taxonomyFieldValue = new TaxonomyFieldValue(taxonomyField);
//赋值
taxonomyFieldValue.TermGuid = entity.Guid.ToString();
taxonomyFieldValue.Label = entity.Name;
//最后赋值
if (!string.IsNullOrEmpty(taxonomyFieldValue.Label))
{
listItem[listItem.Fields[fieldDisplayName].InternalName] = taxonomyFieldValue;
}
}
}
catch (Exception ex)
{
MessageLog.WriteLog(DateTime.Now + "给托管元数据类型栏目[" + fieldDisplayName + "]赋值错误:" + ex.Message);
}
}
#endregion #region//根据某个节点托管元数据的名称得到某个节点托管元数据对象
/// <summary>
/// 根据某个节点托管元数据的名称得到某个节点托管元数据对象
/// </summary>
/// <param name="site">当前网站集</param>
/// <param name="listItem">当前列表项</param>
/// <param name="fieldDisplayName">当前栏目显示名称</param>
/// <param name="termName">节点元数据的名称</param>
/// <returns>返回某个节点托管元数据对象</returns>
private static SPTaxonomyEntity GetCurrentTermToName(SPSite site, SPListItem listItem, string fieldDisplayName, string termName)
{
//需要返回的对象
SPTaxonomyEntity result = new SPTaxonomyEntity();
try
{
//全局变量实例化
if (termcollection != null)
{
//移走数据
if (termcollection.Count > 0)
{
termcollection.Clear();
}
}
//得到某个栏目
TaxonomyField taxField = listItem.Fields[fieldDisplayName] as TaxonomyField;
// Get the taxonomy session for the current site
TaxonomySession taxSession = new TaxonomySession(site);
// Get the default term store object for this site.
TermStore taxTermStore = taxSession.DefaultSiteCollectionTermStore;
//得到TermSet
TermSet termSet = taxTermStore.GetTermSet(taxField.TermSetId); //得到某个栏的所有托管元数据集合
List<SPTaxonomyEntity> alltax = GetAllTerms(termSet);
//循环比较是否跟某个节点托管元数据的名称匹配
foreach (SPTaxonomyEntity tax in alltax)
{
//如果匹配则返回对象
if (tax.Name.Equals(termName))
{
result = tax;
break;
}
}
}
catch (Exception ex)
{
MessageLog.WriteLog(DateTime.Now + "取托管元数据错误数据:" + ex.Message);
}
//返回对象
return result;
}
#endregion #region// 取托管元数据的所有节点数据
/// <summary>
/// 取托管元数据的所有节点数据
/// </summary>
/// <param name="termSet">TermSet类型</param>
/// <returns>返回数据集合</returns>
private static List<SPTaxonomyEntity> GetAllTerms(TermSet termSet)
{
//顶部TermSet数据集合
List<SPTaxonomyEntity> parentcollection = new List<SPTaxonomyEntity>();
//子类的Terms数据集合
List<SPTaxonomyEntity> childcollection = new List<SPTaxonomyEntity>();
//
try
{
//如果不为kog
if (termSet != null)
{
//循环
foreach (Term term in termSet.Terms)
{
//SPTaxonomyEntity
SPTaxonomyEntity tax = new SPTaxonomyEntity();
//托管元数据的id
tax.Guid = term.Id.ToString();
//托管元数据的名称
tax.Name = term.Name;
//加入到父集合中去
parentcollection.Add(tax);
//递归子类的数据集合
childcollection = GetChildTerms(term);
}
//如果不为空才添加
if (childcollection != null)
{
//合并父子类数据集合,返回所有托管元数据集合
parentcollection.AddRange(childcollection);
}
}
}
catch(Exception ex)
{
MessageLog.WriteLog(DateTime.Now + "无法取到托管元数据的父数据:" + ex.Message);
}
//返回
return parentcollection;
}
#endregion #region//取得子类托管元数据的集合
/// <summary>
/// 取得子类托管元数据的集合
/// </summary>
/// <param name="itemTerm">子类对象</param>
/// <returns>子类托管元数据的集合</returns>
private static List<SPTaxonomyEntity> GetChildTerms(Term itemTerm)
{
try
{
//循环
foreach (Term childTerm in itemTerm.Terms)
{
//申明
SPTaxonomyEntity tax = new SPTaxonomyEntity();
//托管元数据的id
tax.Guid = childTerm.Id.ToString();
//托管元数据的名称,组合成路径格式保存
tax.Name = childTerm.GetPath();
//把路径格式化下
tax.Name = tax.Name.Replace(";", "/");
//加入子类集合
termcollection.Add(tax);
//递归继续得到所有子类
GetChildTerms(childTerm);
}
}
catch (Exception ex)
{
MessageLog.WriteLog(DateTime.Now + "无法取到托管元数据的子数据:" + ex.Message);
}
//返回
return termcollection;
}
#endregion
#endregion

调用方法:

我的托管元数据的如下结构图,建立在默认的那个大类下面,所以代码部分写成如下:

 // Get the default term store object for this site.
TermStore taxTermStore = taxSession.DefaultSiteCollectionTermStore;
//得到TermSet
TermSet termSet = taxTermStore.GetTermSet(taxField.TermSetId);
 
 

如果不是我这个情况,必须建立在其他单独的下面,那么代码部分可以改成如下:

 TaxonomySession taxonomySession = new TaxonomySession(site);
TermStore termStore = taxonomySession.TermStores["Managed Metadata Service"];
Group group = termStore.Groups["文档中心元数据"];
//**************公司
TermSet termSet = group.TermSets["公司"];
 
 

调用实例:

 #region//更改托管元数据类型
//公司
SetTaxonomyValue(site, docList, docItem, "公司", "东莞公司");
//项目
SetTaxonomyValue(site, docList, docItem, "项目", "上海公司/项目1/我的项目");
//阶段
SetTaxonomyValue(site, docList, docItem, "阶段", "初步设计阶段Ⅳ");
//专业
SetTaxonomyValue(site, docList, docItem, "专业", "初步设计阶段Ⅳ");
#endregion

最后写入的效果如下:

MOSS2010中如何用代码给托管元数据类型的栏目赋值的更多相关文章

  1. android中如何用代码来关闭打开的相机

    场景描述: 比如你再应用中打开了系统相机,然后需要在几分钟后自动关闭这个系统相机(不是手动关闭) 1.在activityA中利用startActivityForResult(intent,reques ...

  2. WPF中如何用代码触发按钮Click处理

    btnOk.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));

  3. Unity3D除了在编辑器里,怎么用代码给一个Texture类型的变量赋值

    resource.load上来一张贴图就行. using UnityEngine; using System.Collections; public class example : MonoBehav ...

  4. ZH奶酪:PHP中添加HTML代码的三种方法

    php中添加HTML代码,就是php类型的文件中添加html代码~ 第一种是在HTML中加PHP. 大段大段的html代码中,在各个需要执行php的地方<?php .... ?> 比如 l ...

  5. php中嵌套html代码和html代码中嵌套php方式

    php中嵌套html代码和html代码中嵌套php方式 一.总结 拷贝的话直接html代码是极好的方式 1.php中嵌套html代码(本质是原生php):a.原生嵌套<?php .....?&g ...

  6. Salesforce 自定义元数据类型

    自定义元数据类型的优点 Salesforce中的设定都是以元数据(Metadata)存在的.在Salesforce中,用户可以新建自定义对象.自定义字段等,这些数据结构都以元数据的形式存储在系统中.当 ...

  7. 一步一步学Silverlight 2系列(22):在Silverlight中如何用JavaScript调用.NET代码

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  8. Excel 2003 中如何用VBA 代码访问单元格里的值及操作单元格 - 唐诗宋词的专栏 - 博客频道 - CSDN.NET

    在Excel 中编写VBA 代码,最常做的事可能就是操作表单中单元格里的数据. 我这里总结一下如何从VBA 代码中操作单元格的数据. 在VBA 代码中操作单元格需要用到Range 对象,Range 是 ...

  9. ng1中 如何用双向绑定 实现单向绑定的初始时不显示双括号效果?

    ng1中 如何用双向绑定 实现单向绑定(ng-bind就可以不显示{{}})的初始时不显示双括号效果? AngularJS 实例 页面加载时防止应用闪烁: <div ng-app="& ...

随机推荐

  1. Exadata LVM snapshot备份失败

    一台X4-2 的计算节点进行image升级,在正式升级之前利用LVM snapshot备份操作系统时备份失败,并且报大量IO错误,提示无法找到LVM snapshot的挂载点.检查文件系统状态: [r ...

  2. ERROR (UnicodeEncodeError): 'ascii' codec can't encode character u'\uff08' in position 9: ordinal not in range(128)

    环境win10+anaconda2 在安装labelme时遇到了这个问题,其实跟labelme没啥关系,主要是python2读取中文路径时报错,因为默认编码是ASCII,不认识中文,看到有一个一次性解 ...

  3. 《C程序设计II》简易计算器,杨辉,数字杯子图形

    <C程序设计II>简易计算器,杨辉,数字杯子图形.<C程序设计II>简易计算器,杨辉,数字杯子图形.<C程序设计II>简易计算器,杨辉,数字杯子图形. <C程 ...

  4. CSS+jQuery实现轮播

    CSS+jQuery实现轮播 CSS jQuery 前端  实现功能: 自动轮播: 鼠标放在上面停止轮播: 鼠标放在上面显示左右切换的按钮: 鼠标放在小圆圈上显示对应的图片: 轮播效果图 style. ...

  5. UVALive 3645 时序模型

    按航班拆点 注意返边的条件 #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; const int ...

  6. php __CLASS__、get_class()与get_called_class()的区别

    __CLASS__获取当前的类名, get_class()与上面一样,都是获取当前的类名 get_called_class()获取当前主调类的类名 当涉及到继承时,在方法中使用类名.直接贴图了 MVC ...

  7. Juniper SRX550防火墙web页面CPU达到100%的故障解决办法

    Juniper SRX550防火墙web页面CPU达到100%的故障解决办法 利用telnet远程连接主机,对web页面注销重新登录即可,在配置中输入命令:run restart web-manage ...

  8. codeforces之4.1学习记录

    记录一些之前没见过的代码: #include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF ...

  9. error creating bean with name 'defaultvalidator' defined in class path resource

    场景: 1.直接用eclipse 运行没问题(本地用的tomcat是7.0.70): 2.打包发布到服务器运行也没问题(服务器tomcat是8.5.30): 3.将打包发布的放到本地tomcat(7. ...

  10. Linux下安装&运行Jmeter程序

    Jmeter在linux系统中运行需要安装jdk和Jmeter两个软件: 1.安装JDK 先检查系统是否有安装jdk,在linux中执行如下命令:java -version  如果返回版本信息,说明系 ...