Wednesday, February 15, 2012

Find Large Lists & Generate Report in SharePoint

Requirement: SharePoint was running with serious performance issues and wanted to get the report, where more than 2000 items stored in List/Library for the entire web application.

When users store large number of items in List or Library, SharePoint will definitely run into serious performance issues. This script will generate the report on List items count for the entire web application.

This code generates the CSV file, with details : List name, Site collection, Site URL, Item count. By this way, we can make sure we control the users activities by generating reports on our site content periodically. we can use this piece of code to generate the report on scheduled basis to get the List item count insights on our environment. It basically loops through all the site collections, Lists and libraries of the given web application, checks whether the items count > 2000, If yes, it logs the details to the CSV file.

Here is the C# code to Find Large Lists & Generate Report in SharePoint
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace FindLargeLists
{
    class Program
    {
        static void Main(string[] args)
        {
            string site;
            StreamWriter SW;
            try
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Enter the Web Application URL:");
                    site = Console.ReadLine();
                }
                else
                {
                    site = args[0];
                }

                SPSite tmpRoot = new SPSite(site);
                SPSiteCollection tmpRootColl = tmpRoot.WebApplication.Sites;

                //Enumerate through each site collection
                foreach (SPSite tmpSite in tmpRootColl)
                {
                    //Enumerate through each sub-site
                    foreach (SPWeb tmpWeb in tmpSite.AllWebs)
                    {
                        foreach (SPList tmpList in tmpWeb.Lists)
                        {
                            if (tmpList.ItemCount > 2000)
                            {
                                //Log the details to a file
                                SW = File.AppendText("c:\\LargeListsInfo.csv");
                                SW.WriteLine(tmpList.Title + "," + tmpRoot.Url  + tmpList.DefaultViewUrl + "," + tmpList.ItemCount);
                                SW.Close();
                            }
                        }                 
                   
                    }
      
                }

                //Dispose of the Root Site Object
                tmpRoot.Dispose();
            }

            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Get Largest Lists", ex.Message);
            }
        }
    }
}
For PowerShell version of the Above code: Find Large SharePoint Lists & Generate Report with PowerShell



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

5 comments :

  1. Do you happen to have the same in PowerShell?

    ReplyDelete
  2. hello,

    a question!

    In which programming language is this script?

    ReplyDelete
  3. You can clean that up by using powershell's piping:

    $rootSiteCollectionUrl = "http://mySharePoint";
    $sa = Start-SPAssignment -Global;
    (Get-SPSite $rootSiteCollectionUrl).WebApplication.Sites | Foreach-Object {$_.AllWebs} | Foreach-Object {$_.Lists} | Where-Object {$_.ItemCount -ge 2000} | Format-Table Title,ItemCount,ParentWebUrl
    $sa | Stop-SPAssignment;


    If you want to make it reusable, you should make a function:
    function Get-SPLargeLists
    {
    param
    (
    [parameter(ValueFromPipeline=$true,Position=1,Mandatory=$true)]
    $rootSiteCollectionUrl
    );
    $sa = Start-SPAssignment -Global;
    (Get-SPSite $rootSiteCollectionUrl).WebApplication.Sites | Foreach-Object {$_.AllWebs} | Foreach-Object {$_.Lists} | Where-Object {$_.ItemCount -ge 2000} | Format-Table Title,ItemCount,ParentWebUrl;
    $sa | Stop-SPAssignment;
    }

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...