Monday, 10 November 2014

Number of item in List in SharePoint Site throughout web application

Introduction

Number of item in SharePoint list through out web application

Building the Sample

Script need to run where SharePoint Server is installed
Description
Get Spwepapplication =>spsite =>spweb =>webs
Then get all list in webs and get the count of item in lsit
$listitem = $List.Item.Count
 Please find below code for finding list item in all list in Web Application


#parameter declaration
param
(
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)] [System.String] $WebAppUrl,
[Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)] [System.String] $OutputFilePath
)
#check and add snappin
$snapin = Get-PSSnapin | Where-Object {
$_.Name -eq "Microsoft.SharePoint.Powershell"
}
if($snapin -eq $null){
Write-Host "Loading SharePoint Powershell Snnapin......."
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
#Creating table according to column values required
$startTime = Get-Date
Write-Host Script Starting time at $startTime -ForegroundColor Red
$table = New-Object system.Data.DataTable "Orphan Objects"
$WebTitle = New-Object system.Data.DataColumn WebTitle,([string])
$WebUrl = New-Object system.Data.DataColumn WebUrl,([string])
$ListTitle = New-Object system.Data.DataColumn ListTitle, ([String])
$ListUrl = New-Object system.Data.DataColumn ListUrl, ([String])
$ListItemCount = New-Object system.Data.DataColumn ListItemCount, ([String])
$table.columns.add($WebTitle)
$table.columns.add($WebUrl)
$table.columns.add($ListTitle)
$table.columns.add($ListUrl)
$table.columns.add($ListItemCount)
#iterating webapplication => spsite=> spweb=>web =>List
$allWebs = Get-SPWebApplication $WebAppUrl | Get-SPSite -Limit All | Get-SPWeb -Limit All
Foreach($webs in $allWebs)
{
foreach($List in  $webs.Lists)
{
$ListItemCount = $List.Items.Count
if($ListItemCount -gt 5000)
{
#write result to command prompt
 Write-Host List named $List.Title  has $ListItemCount records at $webs.Title -ForegroundColor Green
#write result to table
 $row = $table.NewRow()
           $row.WebTitle = $webs.Title
     $row.WebUrl = $webs.Url
     $row.ListTitle = $List.Title
     $row.ListUrl = $List.RootFolder.Url
     $row.ListItemCount = $ListItemCount
        $table.Rows.Add($row)
           $table.AcceptChanges()
}
}
}
$endTime = Get-Date
Write-Host Script Ending time at $endTime -ForegroundColor Red
#exporting data table to file
$table | Select-Object WebTitle, WebUrl, ListTitle, ListUrl, ListItemCount | Export-Csv $OutputFilePath -NoTypeInformation

No comments:

Post a Comment