SP2010 CAML查询后删除列表项

时间:2019-12-04 15:46:05

标签: powershell sharepoint sharepoint-2010 caml

我想从CAML查询中删除列表项,但目前无法使用foreach循环。 在CAML查询之后删除项目的最佳方法是什么?

$Query = New-Object Microsoft.SharePoint.SPQuery
            $Query.Query = "
                      <Where>
                       <Leq>
                        <FieldRef Name='Created' />
                         <Value Type='DateTime'>
                           <Today OffsetDays='-30' />
                         </Value>
                       </Leq>
                      </Where>
                        <OrderBy>
                          <FieldRef Name='Created' Ascending='False' />
                       </OrderBy>"

            #Get List Items matching the query
            $ListItems = $oList.GetItems($Query)

            write-host "Number of items retrieved:" $ListItems.Count;


            foreach($ListItem in $ListItems)
            {
                write-host "Deleting"+ $listItem.ID + $listItem.Name + $listItem["Created"];
                #$ListItem.delete();

            }

1 个答案:

答案 0 :(得分:0)

请像这样在foreach循环中删除列表项:

$spweb = get-spweb http://sp/sites/devtest 
$oList = $spweb.Lists.TryGetList("List2") 
if ($oList) 
{ 
   $query = New-Object Microsoft.SharePoint.SPQuery
   $query.Query = "
                      <Where>
                       <Leq>
                        <FieldRef Name='Created' />
                         <Value Type='DateTime'>
                           <Today OffsetDays='-30' />
                         </Value>
                       </Leq>
                      </Where>
                        <OrderBy>
                          <FieldRef Name='Created' Ascending='False' />
                       </OrderBy>"

   $ListItems = $oList.GetItems($Query)

   Write-host "Number of items retrieved:" $ListItems.Count;

    foreach($item in $ListItems)
   {
     Write-Host "Deleting Item - $($item.Id)"
     $oList.GetItemById($item.Id).delete()
   }
}