我有以下一点的PowerShell,它将返回存储在SharePoint托管元数据中的所有部门的列表:
Add-PSSnapin Microsoft.Sharepoint.Powershell
# Get the site collection
$sitecollection = Get-SPSite 'http://mysharepointsite/'
# Get the term store id
$taxsession = Get-SPTaxonomySession -Site $sitecollection
# Change to the requierd service name
$termstore = $taxsession.TermStores["Managed Metadata Service"]
$termstore.id
# Get the term set id
$termstoregroup = $termstore.Groups["People"]
# Change to your term set name
$termset = $termstoregroup.TermSets["Department"]
$termset.id
$departments = $termset.id.name
我需要使用来实现相同的目的C#
,但找不到对我有用的任何东西。有谁知道这里的标准方法是什么?
我总是可以从我的C#
应用程序内部启动PowerShell会话,但这似乎是一种非常round回的方式来完成C#
应该没有问题的操作。
我设法通过C#
使用以下代码来做到这一点:
var spSite = new SPSite(@"http://mysharepointsite/");
var taxSession = new TaxonomySession(spSite);
var termStore = taxSession.TermStores["Managed Metadata Service"];
var termStoreGroup = termStore.Groups["People"];
var termSet = termStoreGroup.TermSets["Department"];
var deps = termSet.Terms;
foreach (var dep in deps)
{
MessageBox.Show(dep.Name);
}
这几乎与主要问题中的相同。
重要的是,不要让此代码仅在安装了SharePoint的服务器上运行。当在我的常规开发机器上运行此代码时,我在第1行得到此异常:
Microsoft.SharePoint.dll中发生了类型为'System.TypeInitializationException'的未处理异常
附加信息:'Microsoft.SharePoint.CoreResource'的类型初始值设定项引发了异常。
同样,我只能从SharePoint服务器的问题中运行PowerShell脚本-这很有意义,因为这些脚本似乎正在使用相同的.Net对象,即 TaxonomySession
使用此示例,我还可以使用以下代码从我自己的计算机中检索部门列表:
var clientContext = new ClientContext("http://mysharepointsite/")
{ AuthenticationMode = ClientAuthenticationMode.Default};
var taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
var termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
clientContext.Load(termStore,
store => store.Name,
store => store.Groups.Include(
group => group.Name,
group => group.TermSets.Include(
termSet => termSet.Name,
termSet => termSet.Terms.Include(
term => term.Name)
)
)
);
clientContext.ExecuteQuery();
if (taxonomySession != null)
{
if (termStore != null)
{
foreach (var termGroup in termStore.Groups)
{
foreach (var termSet in termGroup.TermSets)
{
foreach (var term in termSet.Terms)
{
MessageBox.Show(term.Name);
}
}
}
}
}
正如Murray Foxcroft所提到的,这利用了客户端对象模型(CSOM),它使我们可以远程访问分类法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句