从特定的视图而不是所有项目检索list.items

时间:2019-05-09 06:34:53

标签: powershell sharepoint view

我正在尝试导出sharepoint 2013 powershell脚本数组中的项目,以便仅从列表中的某个自定义视图中提取该项目。我创建了一个显示“当前月”的视图-仅在当前日历月中创建的项目。以前的所有项目都保留在列表中,但是我们只希望看到并使用本月输入的项目。

我尝试遵循以下内容及其许多变化形式: https://sharepoint.stackexchange.com/questions/134830/powershell-script-to-get-items-only-in-a-particular-view-of-sharepoint-list-libr

$list = $web.Lists["testlist"]
$view = $list.Views["Current Month"]
$items = $list.GetItems($view)

#Array to Hold Result - PSObjects
$ListItemCollection = @()

$items.Items | foreach {

下面的代码可以很好地获取列表中的所有项目,并可以正常发送电子邮件。

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$web = Get-SPWeb -Identity "https://sharepointsite.com"
$list = $web.Lists["testlist"]

#Array to Hold Result - PSObjects
$ListItemCollection = @()

$list.Items | foreach {
    $ExportItem = New-Object PSObject 
    $ExportItem | Add-Member -MemberType NoteProperty -Name "Name" -Value ($_["Name"] -replace '^\d+;#')
    $ExportItem | Add-Member -MemberType NoteProperty -Name "Reason" -Value $_["Reason"]
    $ExportItem | Add-Member -MemberType NoteProperty -Name "Example" -Value $_["Example"]
    $ExportItem | Add-Member -MemberType NoteProperty -Name "Created" -Value $_["Created"]

    $ListItemCollection += $ExportItem
}

$web.Dispose()

$emailbody = $(cat C:\temp\emailbody.txt) + $ListItemCollection

#Email formatting
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"

$SMTPServer = "mail.com"
$EmailFrom = "test@mail.com" 
$EmailTo = "joel@mail.com"
$EmailSubject = "Test Email"

$Message = New-Object System.Net.Mail.MailMessage $EmailFrom, $EmailTo
$Message.Subject = $EmailSubject
$Message.IsBodyHTML = $true
$message.Body = $emailbody + ($ListItemCollection | ConvertTo-Html -Head $style | Out-String)
$SMTP = New-Object Net.Mail.SmtpClient($SMTPServer)
$SMTP.Send($Message)

当我使用上面采样的代码时,它会为3个添加成员行中的每一个给出错误: 无法索引为空数组。

2 个答案:

答案 0 :(得分:0)

有什么机会您可以举一个$ list.Items为例?

如何使用foreach($ list.items中的$ item)?

foreach ( $item in $list.Items ) {
    $ExportItem = New-Object PSObject 
    $ExportItem | Add-Member -MemberType NoteProperty -Name "Name" -Value $( $item.name -replace '^\d+;#' )
    $ExportItem | Add-Member -MemberType NoteProperty -Name "Reason" -Value $item.reason
    $ExportItem | Add-Member -MemberType NoteProperty -Name "Example" -Value $item.example
    $ExportItem | Add-Member -MemberType NoteProperty -Name "Created" -Value $item.created

   $ListItemCollection += $ExportItem
}

foreach(){}和... | foreach {}的行为有所不同,我知道这样做是有道理的,无论如何,我总是从foreach(){}

中得到想要的东西

答案 1 :(得分:0)

好的,这是可行的:

$view = $list.Views["Current Month"] $items = $list.GetItems($view)

保存结果的数组-PSObjects

$ListItemCollection = @()

获取所有列表项

$items | foreach {

当我用它来获取整个列表/所有项目时,它使用:

保存结果的数组-PSObjects

$ListItemCollection = @()

获取所有列表项

$list.Items | foreach {

从最后一行删除.Items引用就是解决办法。