Prerequisites
- Ensure you have SharePopint Server.
- Ensure you have the EPPlus DLL.
Use the following procedure:
-
Create a folder in the C drive and name it “SiteAnalysis”.
-
Create a console application.
-
Open Visual Studio as an Administrator.
-
Click on "File" -> "New" -> "Project...".
-
-
Select Console Application and enter the name as “SiteUser Details” and click on Ok.
-
Go to Solution Explorer and right-click on References and add the following references.
-
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.
-
-
Now copy and paste the following code into the Main program.
- static void Main(string[] args)
- {
- try
- {
-
-
- string siteURL = string.Empty;
-
-
- Console.WriteLine("Please enter the web url to extract User Information List details:");
- siteURL = Console.ReadLine();
- Console.WriteLine("The entered web url is: " + siteURL);
- if (!string.IsNullOrEmpty(siteURL))
- {
-
- GetUserInfoColumns(siteURL);
- }
- else { Console.WriteLine("weburl entered is empty"); }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Error message: " + ex.Message);
- Console.WriteLine("Stack Trace: " + ex.StackTrace);
- Console.ReadLine();
- }
- Console.WriteLine("Operation completed");
- Console.ReadLine();
- }
- public static void GetUserInfoColumns(string WebUrl)
- {
- SPSecurity.RunWithElevatedPrivileges(delegate ()
- {
- using (SPSite site = new SPSite(WebUrl))
- {
- using (SPWeb web = site.OpenWeb())
- {
- SPList userList = web.Lists["User Information List"];
- SPFieldCollection fieldColl = userList.Fields;
-
- GetFieldDetails(fieldColl);
- if (userList.ItemCount > 0)
- {
- itemcoll = userList.Items;
- using (DataTable userDT = new DataTable())
- {
- userDT.Columns.Add("Name");
- userDT.Columns.Add("IsActive");
- foreach (SPListItem item in itemcoll)
- {
- DataRow userDR = userDT.NewRow();
- userDR["Name"] = item["Name"] != null ? Convert.ToString(item["Name"]) : string.Empty;
- userDR["IsActive"] = item["Is Active"] != null ? Convert.ToString(item["Is Active"]) : string.Empty; ;
- Console.WriteLine("Name:{0};UserActive/Inactive:{1}", userDR["Name"], userDR["IsActive"]);
- userDT.Rows.Add(userDR);
- }
- Excel("UserInfoActiveDetails.xls", userDT);
- }
- }
- }
- }
- });
- }
-
- private static void GetFieldDetails(SPFieldCollection fieldColl)
- {
- using (DataTable fieldDT = new DataTable())
- {
- fieldDT.Columns.Add("Name");
- fieldDT.Columns.Add("Type");
- foreach (SPField field in fieldColl)
- {
- DataRow fieldDR = fieldDT.NewRow();
- fieldDR["Name"] = field.Title;
- fieldDR["Type"] = field.TypeAsString;
- Console.WriteLine("Name:{0};Type:{1}", field.Title, field.TypeAsString);
- fieldDT.Rows.Add(fieldDR);
- }
- Excel("UserInfoFieldDetails.xls", fieldDT);
- }
- }
-
- private static void Excel(string filename, DataTable siteDT)
- {
- if (siteDT.Rows.Count > 0)
- {
- string filenamewithpath = "C://SiteAnalysis//" + filename;
- if (File.Exists(filenamewithpath))
- File.Delete(filenamewithpath);
- FileInfo newFile = new FileInfo(filenamewithpath);
- using (ExcelPackage pck = new ExcelPackage(newFile))
- {
- ExcelWorksheet ws = pck.Workbook.Worksheets.Add(filename);
- ws.Cells["A1"].LoadFromDataTable(siteDT, true);
- pck.Save();
- }
- }
- }
-
To get the active user details we will be using the “Is Active” field .
-
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.
Testing
-
Now to run the application click on the "Play" button.
-
Enter the webapplication URL and click on Enter.
Eg: http://servername:8080/
-
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.