Thursday, March 1, 2012

SharePoint Powershell List Items

Scenario: SharePoint automation scripts related to list items.
Code:

#Load SharePoint Snap In
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
function xApproveAll-SPItems
{  
 param ($Url, $ListName) 

$web = Get-SPWeb $Url 
$list = $web.Lists.TryGetList($ListName)

$folders = $list.Folders
foreach ($folder in $folders)
{
     $folder["_ModerationStatus"] = 0
     $folder.Update()
}  

$items = $list.Items
foreach ($item in $items)
{
     Write-Host "Approving : " + $intIndex 
     $item["_ModerationStatus"] = 0
     $item.Update()
}  

 $web.Dispose()
 Write-Host "Approved all the documents successfully" -foregroundcolor Green  
}

function xDeleteAll-SPItems
{  
 param ($Url, $ListName) 

$web = Get-SPWeb $Url
$list = $web.Lists.TryGetList($ListName)
$itemCount = $list.ItemCount
for($intIndex = $itemCount; $intIndex -gt 0; $intIndex--) 
{ 
        Write-Host "Deleting : " + $intIndex 
        $list.Items[$intIndex-1].Delete();
}

 $web.Dispose()

}
$url=Read-Host "Enter site url"
$listName=Read-Host "Enter list library name"

xApproveAll-SPItems $url $listName
xDeleteAll-SPItems $url $listName

0 comments: