我正在为SharePoint中的多个库创建视图。我可以从下面的代码成功地为一个库创建视图。但是当我循环运行代码时,我陷入了困境。下面是我的代码
代码
Add-PSSnapin "Microsoft.SharePoint.Powershell" -EA SilentlyContinue
$subSiteURL = @("http://test.test.com/departments/Collections", "http://test.test.com/departments/Events")
$listName = @("Collections","Events")
for ($i=0; $i -lt $subSiteURL.length; $i++) {
$SPWeb = Get-SPWeb $subSiteURL[$i]
$ListName = $listName[$i]
$List = $SPWeb.Lists[$ListName]
$ViewTitle = "WebPart View"
$viewFields = New-Object System.Collections.Specialized.StringCollection
$viewFields.Add("DocIcon")
$viewFields.Add("LinkFilenameNoMenu")
$viewQuery = "<GroupBy Collapse='FALSE' GroupLimit='100'> <FieldRef Name='Category' Ascending='True'/> <FieldRef Name='Sub_x002d_Category' Ascending='True'/> </GroupBy> <OrderBy> <FieldRef Name='FileLeafRef' /></OrderBy> <Where><Or><Leq><FieldRef Name='Start_x0020_Date' /><Value Type='DateTime'>[Today]</Value></Leq><And><Geq><FieldRef Name='Expiry_x0020_Date' /><Value Type='DateTime'>[Today]</Value></Geq><Eq><FieldRef Name='Always_x0020_Display' /><Value Type='Boolean'>Yes</Value></Eq></And></Or></Where>"
$viewRowLimit = 999
$viewPaged = $false
$viewDefaultView = $false
$newView = $list.Views.Add($viewTitle, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView)
$newView.TabularView = $False
$newView.Scope = "Recursive"
$newView.Update()
}
错误
You cannot call a method on a null-valued expression. At D:\\CreateDocLibrariesViews.ps1:24 char:28 + $newView = $list.Views.Add <<<< ($viewTitle, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView) + CategoryInfo : InvalidOperation: (Add:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
答案 0 :(得分:0)
尝试以下脚本:
Add-PSSnapin "Microsoft.SharePoint.Powershell" -EA SilentlyContinue
$subSiteURL = @("http://sp", "http://sp:12001")
$listNames = @("MyDoc")
for ($i=0; $i -lt $subSiteURL.length; $i++) {
Write-Host $subSiteURL[$i]
$SPWeb = Get-SPWeb $subSiteURL[$i]
for ($j=0; $j -lt $listNames.length; $j++) {
$ListName = $listNames[$j]
Write-Host $ListName
$List = $SPWeb.Lists[$ListName]
$ViewTitle = "WebPart View"
$viewFields = New-Object System.Collections.Specialized.StringCollection
$viewFields.Add("DocIcon")
$viewFields.Add("LinkFilenameNoMenu")
$viewQuery = "<GroupBy Collapse='FALSE' GroupLimit='100'> <FieldRef Name='Category' Ascending='True'/></GroupBy> <OrderBy> <FieldRef Name='FileLeafRef' /></OrderBy>"
$viewRowLimit = 999
$viewPaged = $false
$viewDefaultView = $false
$newView = $list.Views.Add($viewTitle, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView)
$newView.TabularView = $False
$newView.Scope = "Recursive"
$newView.Update()
}
}
Write-Host "done"