本文介绍了将CSV数据导入到termstore + Powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将CS​​V数据导入SharePoint中央管理术语库.我已经尝试了下面的代码,但由于您无法在空值表达式上调用方法而收到错误".

I am trying to import a CSV data to SharePoint central admin term store. I have tried the below code, but I am getting error as "You cannot call a method on a null-valued expression.

+            $ termgroup = $ termstore.CreateGroup($ group)
+                          
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo         :InvalidOperation:(:) [],RuntimeException
    + FullyQualifiedErrorId:InvokeMethodOnNull".

+             $termgroup = $termstore.CreateGroup($group)
+            
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull ".

该错误也没有记录在自定义日志记录中.

Also the error is not getting logged in the custom logging.

#install.ps1 param ([string]$centralAdminUrl = "http://Central Admin Path", [string] $termStoreName = "MMS_Test", [string]$configpath = "path\abc.csv" ) Add-PSSnapin microsoft.sharepoint.powershell $maxLevels = 5 $actions = @("add","update","delete") $csvDictionary = New-Object 'system.collections.generic.dictionary[int,object]' $caSite = get-spsite -Identity $centralAdminUrl $taxSession = Get-SPTaxonomySession -Site $caSite $termStore = $taxSession.TermStores[0] $termGroups= $null; if ($termStore -eq $null) { $termStore = $taxSession.DefaultSiteCollectionTermStore $termGroups = $termStore.Groups } function ImportCsv() { $id = 0; Import-Csv -Delimiter "," -Path $configpath | % { $obj = @{}; $obj.Id = ++$id; $obj.TermGroup = $_.TermGroup $obj.TermSet = $_.TermSet $obj.Term = $_.Term $obj.UpdatedTitle = $_.UpdatedTitle $obj.Url = $_.Url $obj.FriendlyUrl = $_.FriendlyUrl $obj.CatalogTargetUrl = $_.CatalogTargetUrl $obj.CatalogChildTargetUrl = $_.CatalogChildTargetUrl $obj.TargetUrl = $_.TargetUrl $obj.ChildTargetUrl = $_.ChildTargetUrl $obj.LastModified = $_.LastModified $obj.Status = "" $obj.Action = $null; if ($_.Action -ne $null) { $obj.Action = $_.Action.tostring().tolower() } $object = new-object -TypeName PSObject -Property $obj $csvDictionary.Add($id, $object); } } function ProcessCsv() { $csvDictionaryFiltered = $csvDictionary.Values foreach($obj in $csvDictionaryFiltered) { addNewTerm -termobj $obj -EA 0 -EV Err if ( $Err ) { ConfigureLogger LogError $_.Name LogError $Err[0].Exception Write-Host -f Red "Failed to activate feature :" $_.Name Write-Host -f DarkRed "Please see log file for more information." Write-Host -f DarkRed $currentDir"\log\PowershellScripts.log" } } } function addNewTerm($termobj) { [String]$group = $obj.TermGroup [String]$set = $obj.TermSet [String]$Level1 = $obj.Term $termgroup = $termstore.Groups | where { $_.Name -eq $group } if ($termgroup -eq $null) { $termgroup = $termstore.CreateGroup($group) } $termset = $termgroup.TermSets | where { $_.Name -eq $set } if ($termset -eq $null) { $termset = $termgroup.CreateTermSet($set) } if($Level1) { $level1term = createTerm -currentlevelTerm $termset -levelName $Level1 -EA 0 -EV Err if ( $Err ) { ConfigureLogger LogError $_.Name LogError $Err[0].Exception Write-Host -f Red "Failed to activate feature :" $_.Name Write-Host -f DarkRed "Please see log file for more information." Write-Host -f DarkRed $currentDir"\log\PowershellScripts.log" } } } function createTerm($currentlevelTerm,$levelName) { $nextLevelTerm = $currentlevelTerm.Terms | Where { $_.Name -eq $levelName } -EA 0 -EV Err if ( $Err ) { ConfigureLogger LogError $_.Name LogError $Err[0].Exception Write-Host -f Red "Failed to activate feature :" $_.Name Write-Host -f DarkRed "Please see log file for more information." Write-Host -f DarkRed $currentDir"\log\PowershellScripts.log" } $nextLevelTerm = $currentlevelTerm.CreateTerm($levelName, 1033) $termStore.CommitAll(); $termStore.UpdateCache(); $termStore.FlushCache();

返回$ nextLevelTerm}功能主要{ImportCsv 加工Csv}主要的函数LogError($ message){ $ date =获取日期 $ outContent =" [$ date]`tError`t`t $ message`n' 添加内容"Log \ $ Script:logFile" $ outContent}函数ConfigureLogger(){ if((测试路径日志)-eq $ false) { $ LogFolderCreationObj =新项目-名称日志-类型目录 } $ Script:logFile =" PowershellScripts.log" 添加内容"Log \ $ logFile" <日期t< tCategory> tDetails>}$ ErrorActionPreference =停止"$ spAdminServiceName ="SPAdminV4";Write-Host -f White按Enter键退出..."读取主机

return $nextLevelTerm}function Main{ ImportCsv ProcessCsv}Mainfunction LogError($message){ $date= Get-Date $outContent = "[$date]`tError`t`t $message`n" Add-Content "Log\$Script:logFile" $outContent}function ConfigureLogger(){ if((Test-Path Log) -eq $false) { $LogFolderCreationObj=New-Item -Name Log -type directory } $Script:logFile="PowershellScripts.log" Add-Content "Log\$logFile" "Date`t`t`tCategory`t`tDetails"}$ErrorActionPreference = "Stop"$spAdminServiceName = "SPAdminV4"Write-Host -f White "Press Enter key to exit..."Read-Host

该如何解决?

谢谢

  

  

推荐答案

Param([string]




这篇关于将CSV数据导入到termstore + Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 04:28