Obtaining Field Details and Active User's Information in SharePoint

Prerequisites

  1. Ensure you have SharePopint Server.
  2. Ensure you have the EPPlus DLL.

Use the following procedure:

  1. Create a folder in the C drive and name it “SiteAnalysis”.

  2. Create a console application.

  3. Open Visual Studio as an Administrator.

  4. Click on "File" -> "New" -> "Project...".

    new project


  5. console application

  6. Select Console Application and enter the name as “SiteUser Details” and click on Ok.

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

    add reference

    • Microsoft.SharePoint.dll (this will be available in the 14 hive folder/ISAPI)
      EPPlus.dll (dowload it from Google). The DLL is used for creating an Excel thing.


  8. recent reference

  9. Now copy and paste the following code into the Main program.

    1. static void Main(string[] args)  
    2. {  
    3.     try  
    4.     {  
    5.         //Console.WriteLine("Extract Site details Console:");  
    6.         //Console.ReadLine();  
    7.         string siteURL = string.Empty;  
    8.   
    9.   
    10.         Console.WriteLine("Please enter the web url to extract User Information List details:");  
    11.         siteURL = Console.ReadLine();  
    12.         Console.WriteLine("The entered web url is: " + siteURL);  
    13.         if (!string.IsNullOrEmpty(siteURL))  
    14.         {  
    15.   
    16.             GetUserInfoColumns(siteURL);  
    17.         }  
    18.         else { Console.WriteLine("weburl entered is empty"); }  
    19.     }  
    20.     catch (Exception ex)  
    21.     {  
    22.         Console.WriteLine("Error message: " + ex.Message);  
    23.         Console.WriteLine("Stack Trace: " + ex.StackTrace);  
    24.         Console.ReadLine();  
    25.     }  
    26.     Console.WriteLine("Operation completed");  
    27.     Console.ReadLine();  
    28. }  
    29. public static void GetUserInfoColumns(string WebUrl)  
    30. {  
    31.     SPSecurity.RunWithElevatedPrivileges(delegate ()  
    32.     {  
    33.         using (SPSite site = new SPSite(WebUrl))  
    34.         {  
    35.             using (SPWeb web = site.OpenWeb())  
    36.             {  
    37.                 SPList userList = web.Lists["User Information List"];  
    38.                 SPFieldCollection fieldColl = userList.Fields;  
    39.   
    40.                 GetFieldDetails(fieldColl);  
    41.                 if (userList.ItemCount > 0)  
    42.                 {  
    43.                     itemcoll = userList.Items;  
    44.                     using (DataTable userDT = new DataTable())  
    45.                     {  
    46.                         userDT.Columns.Add("Name");  
    47.                         userDT.Columns.Add("IsActive");  
    48.                         foreach (SPListItem item in itemcoll)  
    49.                         {  
    50.                             DataRow userDR = userDT.NewRow();  
    51.                             userDR["Name"] = item["Name"] != null ? Convert.ToString(item["Name"]) : string.Empty;  
    52.                             userDR["IsActive"] = item["Is Active"] != null ? Convert.ToString(item["Is Active"]) : string.Empty; ;  
    53.                             Console.WriteLine("Name:{0};UserActive/Inactive:{1}", userDR["Name"], userDR["IsActive"]);  
    54.                             userDT.Rows.Add(userDR);  
    55.                         }  
    56.                         Excel("UserInfoActiveDetails.xls", userDT);  
    57.                     }  
    58.                 }  
    59.             }  
    60.         }  
    61.     });  
    62. }  
    63.   
    64. private static void GetFieldDetails(SPFieldCollection fieldColl)  
    65. {  
    66.     using (DataTable fieldDT = new DataTable())  
    67.     {  
    68.         fieldDT.Columns.Add("Name");  
    69.         fieldDT.Columns.Add("Type");  
    70.         foreach (SPField field in fieldColl)  
    71.         {  
    72.             DataRow fieldDR = fieldDT.NewRow();  
    73.             fieldDR["Name"] = field.Title;  
    74.             fieldDR["Type"] = field.TypeAsString;  
    75.             Console.WriteLine("Name:{0};Type:{1}", field.Title, field.TypeAsString);  
    76.             fieldDT.Rows.Add(fieldDR);  
    77.         }  
    78.         Excel("UserInfoFieldDetails.xls", fieldDT);  
    79.     }  
    80. }  
    81.   
    82. private static void Excel(string filename, DataTable siteDT)  
    83. {  
    84.     if (siteDT.Rows.Count > 0)  
    85.     {  
    86.         string filenamewithpath = "C://SiteAnalysis//" + filename;  
    87.         if (File.Exists(filenamewithpath))  
    88.             File.Delete(filenamewithpath);  
    89.         FileInfo newFile = new FileInfo(filenamewithpath);  
    90.         using (ExcelPackage pck = new ExcelPackage(newFile))  
    91.         {  
    92.             ExcelWorksheet ws = pck.Workbook.Worksheets.Add(filename);  
    93.             ws.Cells["A1"].LoadFromDataTable(siteDT, true);  
    94.             pck.Save();  
    95.         }  
    96.     }  
    97. }  

     

  10. To get the active user details we will be using the “Is Active” field .

  11. To make the solution work in the SharePoint 2010 environment, the following properties must be set.

  • In the Application tab select ".Net Framework 3.5" as the Target Framework.
  • In the Build tab select "Any CPU" as the Platform Target.

    configuration

Testing

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

    run application

  2. Enter the webapplication URL and click on Enter.
    Eg: http://servername:8080/

  3. Now you will be able to see the Excel file UserInfoFieldDetails and UserInfoActiveDetails that will be available in the C drive in the Site Analysis folder C://SiteAnalysis/.

Summary

Thus in this article you saw how to get the field details and active user's information from the User Information List in SharePoint.