Manage Group permission programmatically
Hi All, I'm writing this post after long time.... but try to update blog frequently.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
namespace GroupManagerDept
{
public partial class GManagerAdd : System.Web.UI.UserControl
{
PeopleEditor pe;
protected void Page_Load(object sender, EventArgs e)
{
btnADD.Click += new EventHandler(btnADD_Click);
btnClose.Click += new EventHandler(btnClose_Click);
if (!Page.IsPostBack)
{
GroupList();
}
pe = new PeopleEditor();
Panel panPeopleEditor = new Panel();
panPeopleEditor.Style.Add("padding", "1px");
panPeopleEditor.Style.Add("background-color", "#A9D0F5");
panPeopleEditor.Controls.Add(pe);
AddPeople1.Controls.Add(panPeopleEditor);
}
private string GetEmpDept(string LoginID)
{
string EmpID = "", EmployeeDept = "";
string[] UserLoginID;
if (LoginID.Contains("\\"))
{
UserLoginID = LoginID.Split('\\');
EmpID = UserLoginID[1];
}
else if (LoginID.Contains(":"))
{
UserLoginID = LoginID.Split(':');
EmpID = UserLoginID[1];
}
UserProfileManager profileManager = GetAllUserProfile();
foreach (Microsoft.Office.Server.UserProfiles.UserProfile userProfile in profileManager)
{
if (EmpID.ToLower().Equals(Convert.ToString(userProfile[PropertyConstants.UserName].Value).ToLower()))
{
EmployeeDept = Convert.ToString(userProfile[PropertyConstants.Department].Value).ToUpper();
if (EmployeeDept == "")
{
EmployeeDept = "NoDept";
}
}
if (EmployeeDept != "")
break;
}
return EmployeeDept;
}
private void addGroup(string GroupName)
{
ddGroup.Items.Add(new ListItem(GroupName));
}
private void addUserToGroup(string userLoginName, string userGroupName)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spSite = new SPSite(Page.Request.Url.ToString()))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
try
{
spWeb.AllowUnsafeUpdates = true;
SPUser spUser = spWeb.EnsureUser(userLoginName);
if (spUser != null)
{
string testCond = (userExistInGroup(userLoginName, userGroupName));
SPGroup spGroup = spWeb.Groups[userGroupName];
if (spGroup != null && (testCond != "found"))
{
spGroup.AddUser(spUser);
lblMsg.Text = "User added successfully !";
}
else
{
lblMsg.Text = "User already exist !";
}
}
}
catch (Exception ex)
{
lblMsg.Text = "An error occured while adding user!";
}
finally
{
spWeb.AllowUnsafeUpdates = false;
}
}
}
});
}
private string userExistInGroup(string userLoginName, string userGroupName)
{
string ReturnValue = "notfound";
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spSite = new SPSite(Page.Request.Url.ToString()))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
spWeb.AllowUnsafeUpdates = true;
SPUser spUser = spWeb.EnsureUser(userLoginName);
if (spUser != null)
{
SPGroup spGroup = spWeb.Groups[userGroupName];
if (spGroup != null)
{
foreach (SPUser user in spGroup.Users)
{
if ((Convert.ToString(user.LoginName)).ToLower().Equals(userLoginName.ToLower()))
ReturnValue = "found";
if (ReturnValue != "notfound")
break;
}
}
}
}
}
});
return ReturnValue;
}
private void GroupList()
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = SPContext.Current.Site)
{
using (SPWeb myWeb = site.OpenWeb())
{
string DepartmentName = GetEmpDept(Convert.ToString(myWeb.CurrentUser.LoginName));
SPList list = myWeb.Lists["
string strDept = string.Empty;
string[] strDeptArray;
foreach (SPListItem listItem in list.Items)
{
strDept = Convert.ToString(listItem["UserDepartments"]);
strDeptArray = strDept.Split(',');
foreach (string str in strDeptArray)
{
if (str.ToLower().Equals(DepartmentName.ToLower()))
{
addGroup(Convert.ToString(listItem["Title"]));
}
}
}
}
}
});
}
private UserProfileManager GetAllUserProfile()
{
SPSite site = SPContext.Current.Site;
SPWeb spWeb = site.OpenWeb();
ServerContext context = ServerContext.GetContext(SPContext.Current.Site);
if (HttpContext.Current != null)
{
if (HttpContext.Current.Items["HttpHandlerSPWeb"] == null)
HttpContext.Current.Items["HttpHandlerSPWeb"] = spWeb;
if (HttpContext.Current.Items["Microsoft.Office.ServerContext"] == null)
HttpContext.Current.Items["Microsoft.Office.ServerContext"] = context;
}
UserProfileManager profileManager = new UserProfileManager(context);
return profileManager;
}
protected void btnADD_Click(object sender, EventArgs e)
{
foreach (string ent in pe.CommaSeparatedAccounts.Split(','))
{
addUserToGroup(ent, ddGroup.SelectedItem.Text);
}
pe.Entities.Clear();
}
protected void btnClose_Click(object sender, EventArgs e)
{
Response.Redirect("
}
}
}
Post a Comment