SharePoint 2013中的导出/导入术语集

时间:2017-05-19 06:00:01

标签: powershell sharepoint sharepoint-2013

出于导航目的,我们使用术语集。

我需要创建一个PowerShell来获取包含特定术语组的URL的术语集。 这是我的剧本

Set-ExecutionPolicy -Scope CurrentUser Unrestricted
#Specify admin user and SharePoint site URL
##update login 
$User = "corporate\spuser"
##update siteUrl
$Site = "http://sp13" 

#Adding references to SharePoint client assemblies 

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"

$Pwd = "xvcx23423"
$GroupName = "India" 
$TermSetName ="India_Global"

#Recursive function to get terms

function GetTerms([Microsoft.SharePoint.Client.Taxonomy.Term] $Term,[String]$ParentTerm,[int] $Level)
{
  $Terms = $Term.Terms;
  $Context.Load($Terms)
  $Context.ExecuteQuery();
  if($ParentTerm)
  {
   $ParentTerm = $ParentTerm + "," + $Term.Name;
  }
  else
  {
   $ParentTerm = $Term.Name;
  }

  Foreach ($SubTerm in $Terms)
  {
     $Level = $Level + 1;
     #up to 7 terms levels are written
     $NumofCommas =  7 - $Level;
     $commas ="";

     For ($i=0; $i -lt $NumofCommas; $i++)  
     {
        $commas = $commas + ",";
     }

    $file.Writeline("," + "," + "," + "," + $Term.Description + "," + $ParentTerm + "," + $SubTerm.Name + $commas );
     GetTerms -Term $SubTerm -ParentTerm $ParentTerm -Level $Level;
  } 
}

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)

$Credentials =  New-Object System.Net.NetworkCredential($User,$Pwd);

$Context.Credentials = $Credentials

$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context)

$Context.Load($MMS)

$Context.ExecuteQuery()
 
#Get Term Stores

$TermStores = $MMS.TermStores

$Context.Load($TermStores)

$Context.ExecuteQuery()
 
$TermStore = $TermStores[0]

$Context.Load($TermStore)
$Context.ExecuteQuery()

#Get Groups
$Group = $TermStore.Groups.GetByName($GroupName)
$Context.Load($Group)
$Context.ExecuteQuery()


#Bind to Term Set
$TermSet = $Group.TermSets.GetByName($TermSetName)
$Context.Load($TermSet)
$Context.ExecuteQuery() 
 
#Create the file and add headings
$OutputFile = "Output File Path1.csv"
$file = New-Object System.IO.StreamWriter($OutputFile)

$file.Writeline("Term Set Name,Term Set Description,LCID,Available for Tagging,Term Description,Level 1 Term,Level 2 Term,Level 3 Term,Level 4 Term,Level 5 Term,Level 6 Term,Level 7 Term");
 
$Terms = $TermSet.Terms
$Context.Load($Terms);
$Context.ExecuteQuery();
$lineNum = 1;
Foreach ($Term in $Terms)
{  
  if($lineNum -eq 1)
  {
   ##output term properties on first line only
   $file.Writeline($TermSet.Name + "," + $TermSet.Description + "," + $TermStore.DefaultLanguage + "," + $TermSet.IsAvailableForTagging + "," + $Term.Description + "," + $Term.Name + "," + "," + "," + "," + "," + "," );
  }
  else
  {
    $file.Writeline("," + "," + "," + "," + $Term.Description + "," + $Term.Name + "," + "," + "," + "," + "," + "," );
  }
  $lineNum = $lineNum + 1;
  $TermTreeLevel  = 1; 
  GetTerms -Term $Term -Level $TermTreeLevel -ParentTerm "";
}

 $file.Flush();
 $file.Close(); 

知道如何获取术语集的URL。

是否有更好的方法来导出/导入特定组的术语集。

1 个答案:

答案 0 :(得分:0)

您可以使用TaxonomySession获取术语设置友好URL:

SPSite site = SPSite.Current.Site;
TaxonomySession servis = new TaxonomySession(site);
siteNavigationStore = servis.DefaultSiteCollectionTermStore;
siteNavigationGroup = sitenavigationstore.GetSiteCollectionGroup(site);
siteNavigationSet = sitenavigationgrup.TermSets["Term Set Name"];
NavigationTerm navigatedTerm = NavigationTerm.GetAsResolvedByWeb(navigationterm, site.RootWeb, StandardNavigationProviderNames.GlobalNavigationTaxonomyProvider);
navigatedTerm.GetWebRelativeFriendlyUrl();

GetWebRelativeFriendlyUrl返回友好网址