Share via


how to get the total number of documents in each site collection in a web application?

Question

Friday, October 12, 2012 3:09 AM

I am trying to get number of documents in each sitecollection from a web application.

I am looking for a result with site collection name and number of documents present in site collection.

any ideas??

kukdai

I need it as server side code, i am trying to create a webpart with this.

this is not one time solution.

i dont think i can use powershell script in c#....

any idea how powershell script can be used in powershell

All replies (8)

Friday, October 12, 2012 6:24 AM âś…Answered

hi,

Refer this thread.

http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/97b94245-5bbf-4711-9ac1-be8891d82f88

Also you can refer this thread also. this counts size of documents. please follow steps for retrieving all documents.

https://www.nothingbutsharepoint.com/sites/itpro/Pages/COUNT-and-SIZE-of-all-your-documents-in-SharePoint-2010.aspx

Also try this power shell script (for more info follow this thread http://stsadm.blogspot.in/2010/08/getting-inventory-of-all-sharepoint.html)

Get-DocInventory | Out-GridView
#Get-DocInventory | Export-Csv -NoTypeInformation -Path c:\inventory.csv
write function #Get-DocInventory as follow.
function Get-DocInventory() {
    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
    $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
    foreach ($spService in $farm.Services) {
        if (!($spService -is [Microsoft.SharePoint.Administration.SPWebService])) {
            continue;
        }
        
        foreach ($webApp in $spService.WebApplications) {
            if ($webApp -is [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]) { continue }
            
            foreach ($site in $webApp.Sites) {
                foreach ($web in $site.AllWebs) {
                    foreach ($list in $web.Lists) {
                        if ($list.BaseType -ne "DocumentLibrary") {
                            continue
                        }
                        foreach ($item in $list.Items) {
                            $data = @{
                                "Web Application" = $webApp.ToString()
                                "Site" = $site.Url
                                "Web" = $web.Url
                                "list" = $list.Title
                                "Item ID" = $item.ID
                                "Item URL" = $item.Url
                                "Item Title" = $item.Title
                                "Item Created" = $item["Created"]
                                "Item Modified" = $item["Modified"]
                                "File Size" = $item.File.Length/1KB
                            }
                            New-Object PSObject -Property $data
                        }
                    }
                    $web.Dispose();
                }
                $site.Dispose()
            }
        }
    }
}

Thanks

amol


Friday, October 12, 2012 6:01 AM

Hi

i recommend you Powershell scripts

Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.


Friday, October 12, 2012 8:18 AM

Since SPList object has a property SPList.ItemCount there is no need to iterate through the all list items. You can simple modified the above code in the following way:

              foreach ($site in $webApp.Sites) {
                foreach ($web in $site.AllWebs) {
                    foreach ($list in $web.Lists) {
                        if ($list.BaseType -ne "DocumentLibrary") {
                            continue
                        }
                        else
                        {
                             $data = @{
**                                "Web Application" = $webApp.ToString()**
**                                "Site" = $site.Url**
**                                "Web" = $web.Url**
**                                "list" = $list.Title**
**                                "Item Count" = $list.ItemCount**
**                            }**
                            New-Object PSObject -Property $data
                        }
                    }
                    $web.Dispose();
                }
                $site.Dispose()
            }

Hope this helps!

Dimcho

Dimcho Tsanov


Friday, October 12, 2012 12:30 PM

I NEED TO DISPLAY IT IN A WEBPART, gridview,

do you know any powershell script that i can run in server side code and how to use that i c# any idea.

I cannot use the powershell UI manually not at all

kukdai


Friday, October 12, 2012 12:40 PM

Hi

cretae a new custom list , name it: COUNT_LIST, which will contain items for each library, / site collection ( you can do this manually or , automatically using again:) powershellscript ). Also add to this list a column name it COL_COUNT for example

Now, in the main powershell script ( in my first post ) for each library/site colletcion, update COUNT_LIST's item with that value

You will can use as a webpart/gridview datas from COUNT_LIST

Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.


Friday, October 12, 2012 1:01 PM

If you need to display it in a web part, I would create a 'Site Collection Document Stats' list in SharePoint and have your script add a new item for each site collection and document count.

After you get the document total, you call a function like...

function addItem
{ param ($listName, $siteName, $siteURL, $docCnt)
    $newItem = $listName.items.Add()
    $newItem["Title"] = $siteName
    $newItem["SiteURL"] = $siteURL
    $newItem["DocCount"] = $docCnt   
    $newItem.update()
}

You could schedule this to run once a day or however often you need it updated. When creating the scheduled task, just add the location of your script to the powershell exe.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "c:\scriptpath\scriptname.ps1"

Also, make sure to include the PSSnapin at the beginning of your script.

# Add SharePoint Snapin to PowerShell 
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {   
  Add-PSSnapin Microsoft.SharePoint.PowerShell 
}

Friday, October 12, 2012 1:43 PM

i am sorry to say but there is no way i can use power shell at all i have to deliver it as a client solution and i don't have access to the production servers at all.

if any other option you know of using the spquery, or search please that would help a lot

kukdai


Monday, November 12, 2012 2:44 AM

Hi kukdai,

SharePoint PowerShell object is just based on the server object, if you can access the server site object like spquery, you can try to change the PowerShell code provided into c# code, the code provided is iterate the documents in the site collection, you can use SP object instead to achieve the save function.

Thanks,

Qiao Wei

TechNet Community Support