1. Copy your excel sheet \bin\debug\manager.xls ( refer screen shot as bellow)
2. Note : Sheet1(A column-no header) add user email address ( refer screen shot bellow)
using Microsoft.Office.Interop.Excel;
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AddUserToSharePoint
{
class Program
{
static void Main(string[] args)
{
{
//Your Code
Console.Clear();
Console.WriteLine("Option 1 => Department Manager");
Console.WriteLine("Option 2 => Department Senior Managers");
Console.WriteLine("Option 3 => General Managers");
Console.WriteLine("Option 4 => HR Director");
Console.WriteLine("Option 5 => HR Executive");
Console.WriteLine("Option 6 => HR Portal Site Administrators");
Console.WriteLine("Option 7 => HR Portal(General) Users");
Console.WriteLine("Option 8 => Regional HR Manager");
Console.WriteLine("Option 9 => Vice President & General Manager");
Console.WriteLine("Enter Option Number");
Console.Write("input : ");
int caseSwitch = Convert.ToInt32(Console.ReadLine());
switch (caseSwitch)
{
case 1:
Console.WriteLine("You Choose : Option 1 => Department Manager...");
insertUserToSharePointGroup(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\Excel\\Department Manager.xlsx", 10); //groupid
break;
case 2:
Console.WriteLine("You Choose : Option 2 => Department Senior Managers...");
insertUserToSharePointGroup(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\Excel\\Department Senior Managers.xlsx", 13);
break;
case 3:
Console.WriteLine("You Choose : Option 3 => General Managers...");
insertUserToSharePointGroup(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\Excel\\General Managers.xlsx", 14);
break;
case 4:
Console.WriteLine("You Choose : Option 4 => HR Director...");
insertUserToSharePointGroup(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\Excel\\HR Director.xlsx", 15);
break;
case 5:
Console.WriteLine("You Choose : Option 5 => HR Executive...");
insertUserToSharePointGroup(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\Excel\\HR Executive.xlsx", 11);
break;
case 6:
Console.WriteLine("You Choose : Option 6 => HR Portal Site Administrators...");
insertUserToSharePointGroup(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\Excel\\HR Portal Site Administrators.xlsx", 8);
break;
case 7:
Console.WriteLine("You Choose : Option 7 => HR Portal(General) Users...");
insertUserToSharePointGroup(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\Excel\\GeneralUsers.xlsx", 9);//group id
break;
case 8:
Console.WriteLine("You Choose : Option 8 => Regional HR Manager...");
insertUserToSharePointGroup(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\Excel\\Regional HR Manager.xlsx", 12);
break;
case 9:
Console.WriteLine("You Choose : Option 10 => Vice President & General Manager...");
insertUserToSharePointGroup(System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "\\Excel\\Vice President and General Manager.xlsx", 16);
break;
default:
Console.WriteLine("Value didn’t match earlier.");
break;
}
Console.WriteLine("--------------------------------------------------------------------------------");
Console.WriteLine("Successfully completed- all User's add in SharePoint HR Portal");
Console.WriteLine("Do you want to continue (Y/N)? ");
} while (Console.ReadKey().KeyChar != 'Y');
}
public static void insertUserToSharePointGroup(string FilePath,int groupNameId)
{
try
{
var allDomains = Forest.GetCurrentForest().Domains.Cast<Domain>();
int x = 0; int y = 0;
Microsoft.Office.Interop.Excel.Application myApp = newMicrosoft.Office.Interop.Excel.Application();
myApp.Visible = false; //dont open
Microsoft.Office.Interop.Excel.Workbook workbook = myApp.Workbooks.Open(FilePath,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet sheet = workbook.Sheets[1];
//range1.Cells represents all the columns/rows
Range range1 = sheet.UsedRange.Columns[1];
int count = sheet.UsedRange.Columns[1].Rows.Count;
// range1 = sheet.UsedRange;
foreach (Range r in range1.Cells)
{
// r is the range of the corresponding cell
if (r.Value != null)
{
foreach (Domain d in allDomains)
{
try
{
Thread.Sleep(500);
x++;
var searcher = new DirectorySearcher(newDirectoryEntry("LDAP://" + d.Name));
searcher.Filter = "(&(ObjectClass=user)(mail=" + r.Value + "))";
SearchResult result = searcher.FindOne();
ResultPropertyCollection myResult;
Console.Write("Count : " + x);
//If found in Active Directory
if (result != null)
{
Thread.Sleep(500);
myResult = result.Properties;
y++;
string ADName = result.Properties["samaccountname"][0].ToString();
ClientContext clientContext = newClientContext("http://Win:2016:8080/hr/");
GroupCollection collGroup = clientContext.Web.SiteGroups;
Group oGroup = collGroup.GetById(groupNameId);//HR user group
UserCreationInformation userCreationInfo = newUserCreationInformation();
userCreationInfo.Email = r.Value;
userCreationInfo.LoginName = "i:0#.w|utccgl\\" + ADName + "";
User oUser = oGroup.Users.Add(userCreationInfo);
clientContext.ExecuteQuery();
Console.WriteLine(" ==>User Add to Group : " + y + " User ID => " + ADName + " : Email =>" + r.Value + " : Domain :" + d.Name);
}
}
catch
{
Console.WriteLine("Can't found ADName :" + r.Value);
continue; }
}
}
}
if (x == 0)
{
Console.WriteLine("Can't found Logindomain");
}
}
catch(Exception ex)
{
Console.WriteLine("Can't found Logindomain :" + ex.Message);
}
}
}
}
No comments:
Post a Comment