Get the Details of a Site, Subsite, Lists, Library For Migration Purposes in SharePoint

Introduction

This article explains how to get the details of a site, sub-site, list and library count from a Web application using server-side code in SharePoint.

The following are the prerequisites:

  1. SharePopint Server.
  2. The EPPlus DLL.

Use the following procedure:

  1. Create a console application.
  2. Open Visual Studio as an Administrator.
  3. Click on "File" -> "New" -> "Project...".

    new project

    console application

  4. Select Console Application and enter the name as “Site Details” and click on "Ok".

  5. Go to Solution Explorer and right-click on References and add the following references.

    reference
    • Microsoft.SharePoint.dll (this will be available in the 14 hive folder/ISAPI).

    • EPPlus.dll (dowload it from Google) this DLL creats an Excel file.

      recent reference

  6. Now copy and paste the following code into the Main program.
    1. static void Main(string[] args)  
    2. {  
    3.     try  
    4.     {  
    5.         string siteURL = string.Empty;  
    6.         DataTable siteDT = new DataTable();  
    7.         siteDT.Columns.Add("SiteUrl");  
    8.         siteDT.Columns.Add("ListName");  
    9.         siteDT.Columns.Add("ItemCount");  
    10.         Console.WriteLine("Please enter the url to extract the details:");  
    11.         siteURL = Console.ReadLine();  
    12.         Console.WriteLine("The entered site url is: " + siteURL);  
    13.         if (!string.IsNullOrEmpty(siteURL))  
    14.         {  
    15.             using (SPSite site = new SPSite(siteURL))  
    16.             {  
    17.                 DataRow webRow = siteDT.NewRow();  
    18. llWebs loops through the site as well as the subsites.  
    19.                 foreach (SPWeb web in site.AllWebs)  
    20.                 {  
    21.                     webRow["SiteUrl"] = web.Url;  
    22.                     siteDT.Rows.Add(webRow);  
    23.                     Console.WriteLine("Web Name: " + web.Name);  
    24.                     foreach (SPList list in web.Lists)  
    25.                     {  
    26.                         DataRow listRow = siteDT.NewRow();  
    27.                         listRow["ListName"] = list.Title;  
    28.                         listRow["ItemCount"] = list.ItemCount;  
    29.                         siteDT.Rows.Add(listRow);  
    30.                         Console.WriteLine(list.Title + " count is " + list.ItemCount);  
    31.                     }  
    32.   
    33.                 }  
    34.   
    35.   
    36.             }  
    37.             if (siteDT.Rows.Count > 0)  
    38.             {  
    39.                 string filenamewithpath = "C://SiteDetails.xls";  
    40.                 if (File.Exists(filenamewithpath))  
    41.                     File.Delete(filenamewithpath);  
    42.                 FileInfo newFile = new FileInfo(filenamewithpath);  
    43.                 using (ExcelPackage pck = new ExcelPackage(newFile))  
    44.                 {  
    45.                     ExcelWorksheet ws = pck.Workbook.Worksheets.Add("SiteDetails");  
    46.                     ws.Cells["A1"].LoadFromDataTable(siteDT, true);  
    47.                     pck.Save();  
    48.                 }  
    49.             }  
    50.   
    51.         }  
    52.         Console.WriteLine("Press enter to exit");  
    53.         Console.ReadLine();  
    54.     }  
    55.     catch (Exception ex)  
    56.     {  
    57.   
    58.         Console.WriteLine("Error occured:" + ex.Message);  
    59.         Console.WriteLine(ex.StackTrace);  
    60.         Console.ReadLine();  
    61.     }  
    62. }
  7. To make the solution work in the SharePoint 2010 environment, the following properties must be set.

    In the Application tab the Target FrameWork should be “.Net FrameWork 3.5”.

    In the Build tab the Platform Target should be set to “Any CPU”.

    Build

Testing

  1. Now to run the application click on the "Play" button.

    Play

  2. Enter the web application URL and click on Enter.

    Eg: http://servername:8080/

  3. Now you will be able to see the Excel file SiteDetails that will be available in the C drive at C://SiteDetails.xls.

Summary

Thus in this article you saw how to get the details of site, subsite, list and library count from a web application using server-side code in SharePoint.