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 a large number of items in a 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. In this way, we can make sure we control the user activities by generating reports on our site content periodically. we can use this piece of code to generate the report on a 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 and 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

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

5 thoughts on “Find Large Lists & Generate Report in SharePoint

  • You can clean that up by using powershell’s piping:

    $rootSiteCollectionUrl = “https://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;
    }

    Reply
  • hello,

    a question!

    In which programming language is this script?

    Reply
  • Do you happen to have the same in PowerShell?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *