We will going to create a Utility to get all the admin users on the Sharepoint site. This utility can be either a window application or Console application.
We will create a Window application.
1. Create a New Project
2. Select Windows and then Windows Forms. Mention the name of the Project.
(I mentioned the name as "WindowAppUserPermissions")
3. A Project is created and a blank form is visible on the screen. Click
on the Form and press F4 on the keyboard, a property window opens. Now
in the Text property enter the name of the Form like "User Permission
Utility" and also increase the the size by inserting the value in size
property.
4. Drag & Drop a Label and a TextBox from Toolbox and change the Text Property of Label to "Enter the Filename (e.g., c:\filename.txt)" and change the Name property of TextBox to "txtFilename".
5. Now in the same way Drag & Drop two buttons on the Form and change the Name property to "btnGetUsers" and "btnExit" and the Text property to "Get Users" and "Exit".
6. Add one more Label to show the error and name it "lblError".
7. Now press F7 to see the code file (Form1.cs) and paste the below code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
namespace WindowAppUserPermissions
{
public partial class Form1 : Form
{
private string NewFile = "C:\\siteUsers.txt";
public Form1()
{
InitializeComponent();
}
private void btnGetUsers_Click(object sender, EventArgs e)
{
// Check if TextBox is null
if (String.IsNullOrEmpty(txtFilename.Text))
{
lblError.Text = "Please Enter Filename with complete location.";
}
else
{
ReadTxtFile(txtFilename.Text);
}
}
/// <summary>
/// This Method reads the URL of site one by one from Text File and calls the GetUsers method to find the Admin users
/// and write it in a new Text file
/// </summary>
/// <param name="filename"></param>
protected void ReadTxtFile(string filename)
{
try
{
string FILE_NAME = filename;
//Using the given path to check whether the file exists or not
if (!File.Exists(FILE_NAME))
{
lblError.Text = "File does not exist.";
}
else
{
// Read the URL file in StreamReader object
using (StreamReader sr = File.OpenText(FILE_NAME))
{
String input;
//Write a New file
StreamWriter sw = new StreamWriter(NewFile);
while ((input = sr.ReadLine()) != null)
{
GetUsers(input, sw);
}
sw.Close();
}
}
}
catch (Exception ex)
{
lblList.Text = lblList.Text + "\n" + ex.Message;
}
}
protected void GetUsers(string txt, StreamWriter sw)
{
try
{
int count = 0;
string strSite = txt;
if (!string.IsNullOrEmpty(strSite))
{
using (SPSite objSite = new SPSite(strSite))
{
using (SPWeb objWeb = objSite.OpenWeb())
{
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("############ Site Name: " + objWeb.Title + " ############");
sw.WriteLine("");
sw.WriteLine("URL: "+ objWeb.Url.ToString());
//Get all the users of the website in a collection
SPUserCollection allUsers = objWeb.Users;
// Checking each user from Users collection
foreach (SPUser objUser in allUsers)
{
SPRoleCollection allGroups = objUser.Roles;
foreach (SPRole objGroup in allGroups)
{
//
// PermissionMask Summary:
// Gets or sets the permission mask for the site group.
//
// PermissionMask Returns:
// A Microsoft.SharePoint.SPRights value that specifies rights for the permission
// mask.
SPRights rights = objGroup.PermissionMask;
// SPRights.FullMask Summary:
// Value: -1. Has all permissions on the Web site. Not available through the
// user interface.
if (rights == SPRights.FullMask)
{
count++;
// Writing the Name of the Admin User and login ID
string strValue = count + ". Name: " + objUser.Name + " | Login ID :" + objUser.LoginName;
sw.WriteLine("");
sw.WriteLine(strValue);
}
}
}
//Get all the Groups of the website in a collection
SPGroupCollection allGroups2 = objWeb.Groups;
// Checking each Group from Groups collection
foreach (SPGroup objGroup in allGroups2)
{
try
{
SPRoleCollection objroleCollection = objGroup.Roles;
foreach (SPRole objRole in objroleCollection)
{
//
// PermissionMask Summary:
// Gets or sets the permission mask for the site group.
//
// PermissionMask Returns:
// A Microsoft.SharePoint.SPRights value that specifies rights for the permission
// mask.
SPRights rights = objRole.PermissionMask;
// SPRights.FullMask Summary:
// Value: -1. Has all permissions on the Web site. Not available through the
// user interface.
if (rights == SPRights.FullMask)
{
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("**** Group: " + objGroup.Name);
#region
SPUserCollection allUsers2 = objGroup.Users;
foreach (SPUser objUser in allUsers2)
{
count++;
string strValue = count + ". Name: " + objUser.Name + " | Login ID: " + objUser.LoginName;
sw.WriteLine("");
sw.WriteLine(strValue);
}
#endregion
}
}
}
catch (Exception ex)
{
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("Error: "+ ex.Message);
}
}
if (count == 0)
{
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("No User Found.");
}
}
}
}
}
catch (Exception ex)
{
sw.WriteLine("");
sw.WriteLine("");
sw.WriteLine("Error: " + ex.Message);
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
8. Now Build Solution or Press F6 to build the solution and then run the exe file under the bin folder. to get the Admin users of the site.
NOTE:
- Run this utility on the machine on which the site is hosted and login with Admin rights.
- Create a text file and write the URL of the Sharepoint sites in each line
No comments:
Post a Comment