private void AssignRoles(SPWeb web, SPListItem item, SPRoleDefinition roleDef) { foreach (string principalName in Principals) { SPPrincipalInfo principalInfo = SPUtility.ResolvePrincipal(web.Site.WebApplication, null, principalName, SPPrincipalType.All, SPPrincipalSource.All, false); if (principalInfo != null) { SPRoleAssignment roleAssign = new SPRoleAssignment(principalInfo.LoginName, principalInfo.Email, principalInfo.DisplayName, ""); roleAssign.RoleDefinitionBindings.Add(roleDef); item.RoleAssignments.Add(roleAssign); } else { try { SPPrincipal group = web.SiteGroups[principalName]; SPRoleAssignment roleAssign = new SPRoleAssignment(group); roleAssign.RoleDefinitionBindings.Add(roleDef); item.RoleAssignments.Add(roleAssign); } catch (Exception ex) { SPRoleAssignment roleAssign = new SPRoleAssignment(principalName, "", "", ""); roleAssign.RoleDefinitionBindings.Add(roleDef); item.RoleAssignments.Add(roleAssign); } } } }
public SPUser GetUserProfileByDisplayName(String displayName) { SPPrincipalInfo pinfo = SPUtility.ResolvePrincipal(web, displayName, SPPrincipalType.User, SPPrincipalSource.All, web.Users, false); SPUser user = web.Users[pinfo.LoginName]; return(user); }
public bool IsVisibleTo(SPUser currentUser) { var isVisible = false; if (currentUser.IsSiteAdmin || currentUser.UserToken.IsSystemAccount) { return(true); } var context = GetContext(); var settings = GetSettings(context); if (settings != null) { if (settings.VisibleTo.Length > 0) { var visibleToUsers = settings.VisibleTo.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (var user in visibleToUsers) { var usersplit = user.Split(new char[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries); if (usersplit.Length == 2) { SPPrincipalInfo visiblePrincipal = null; var key = usersplit[1]; try { visiblePrincipal = SPUtility.ResolvePrincipal(context, key, SPPrincipalType.All, SPPrincipalSource.All, null, false); } catch (Exception) { }; if (visiblePrincipal == null) { continue; } else if (visiblePrincipal.PrincipalType == SPPrincipalType.SecurityGroup || visiblePrincipal.PrincipalType == SPPrincipalType.SharePointGroup) { isVisible = isMemberOfGroup(context, visiblePrincipal.LoginName, currentUser.LoginName); } else if (visiblePrincipal.PrincipalType == SPPrincipalType.User) { if (visiblePrincipal.LoginName.ToLower().Equals(currentUser.LoginName.ToLower())) { isVisible = true; } } if (isVisible) { break; } } } } } context.Dispose(); return(isVisible); }
protected SPPrincipalInfoInstance(ObjectInstance prototype, SPPrincipalInfo principalInfo) : base(prototype) { if (principalInfo == null) { throw new ArgumentNullException("principalInfo"); } m_principalInfo = principalInfo; }
public SPPrincipalInfoInstance(ScriptEngine engine, SPPrincipalInfo principalInfo) : this(engine) { if (principalInfo == null) { throw new JavaScriptException(engine, "Error", "$camelCasedName must be specified."); } m_principalInfo = principalInfo; }
/// <summary> /// Check if groupName is an Active directory group /// </summary> /// <param name="groupName"></param> /// <returns></returns> public bool IsADGroup(string groupName) { SPPrincipalInfo userInfo = SPUtility.ResolvePrincipal(currentWeb, groupName, SPPrincipalType.SecurityGroup, SPPrincipalSource.All, null, false); if (userInfo != null) { return(true); } return(false); }
public static List <string> GetLoginsOfGroupMembers(string group, SPWeb curreweb, bool recursive) { List <string> list = new List <string>(); SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite sPSite = new SPSite(curreweb.Site.ID)) { using (SPWeb sPWeb = sPSite.OpenWeb(curreweb.ID)) { try { SPPrincipalInfo[] principalsInGroup; try { bool flag; principalsInGroup = SPUtility.GetPrincipalsInGroup(sPWeb, group, 2147483646, out flag); } catch (NullReferenceException) { throw new Exception("Error querying for group members. Ensure the connectivity between the application pool account and the domain that is being queried is configured correctly."); } SPPrincipalInfo[] array = principalsInGroup; for (int i = 0; i < array.Length; i++) { SPPrincipalInfo sPPrincipalInfo = array[i]; if (recursive) { if (sPPrincipalInfo.PrincipalType == SPPrincipalType.User) { list.Add(sPPrincipalInfo.LoginName.ToLower()); } else { list.AddRange(GetLoginsOfGroupMembers(sPPrincipalInfo.LoginName, sPWeb, true)); } } else { list.Add(sPPrincipalInfo.LoginName.ToLower()); } } } catch (Exception ex) { throw ex; } } } }); return(list); }
private void getEmps() { try { SPSecurity.RunWithElevatedPrivileges(delegate() { SPSite oSite = new SPSite(SPContext.Current.Web.Url); SPWeb spWeb = oSite.OpenWeb(); SPPrincipalInfo pinfo = SPUtility.ResolvePrincipal(spWeb, strEmpDisplayName, SPPrincipalType.User, SPPrincipalSource.All, null, false); SPServiceContext serviceContext = SPServiceContext.GetContext(oSite); UserProfileManager userProfileMgr = new UserProfileManager(serviceContext); UserProfile cUserProfile = userProfileMgr.GetUserProfile(pinfo.LoginName); List <UserProfile> directReports = new List <UserProfile>(cUserProfile.GetDirectReports()); foreach (UserProfile up in directReports) { DataRow row = tblEmps.NewRow(); if (up.GetProfileValueCollection("AboutMe")[0] != null && up.GetProfileValueCollection("AboutMe")[0].ToString() != string.Empty) { row["EmpName"] = up.GetProfileValueCollection("AboutMe")[0].ToString(); } else { row["EmpName"] = up.DisplayName; } row["EnglishName"] = up.DisplayName; row["EmpJob"] = up.GetProfileValueCollection("Title")[0].ToString(); tblEmps.Rows.Add(row); } }); } catch (Exception) { } }
private void getEmps() { try { SPSecurity.RunWithElevatedPrivileges(delegate() { SPSite oSite = new SPSite(SPContext.Current.Web.Url); SPWeb spWeb = oSite.OpenWeb(); SPPrincipalInfo pinfo = SPUtility.ResolvePrincipal(spWeb, strEmpDisplayName, SPPrincipalType.User, SPPrincipalSource.All, null, false); SPServiceContext serviceContext = SPServiceContext.GetContext(oSite); UserProfileManager userProfileMgr = new UserProfileManager(serviceContext); UserProfile cUserProfile = userProfileMgr.GetUserProfile(pinfo.LoginName); List <UserProfile> directReports = new List <UserProfile>(cUserProfile.GetDirectReports()); foreach (UserProfile up in directReports) { DataRow row = tblEmps.NewRow(); if (up.GetProfileValueCollection("AboutMe")[0] != null && up.GetProfileValueCollection("AboutMe")[0].ToString() != string.Empty) { row["EmpName"] = up.GetProfileValueCollection("AboutMe")[0].ToString(); } else { row["EmpName"] = up.DisplayName; } row["EnglishName"] = up.DisplayName; row["EmpJob"] = up.GetProfileValueCollection("Title")[0].ToString(); string Active_Set_Goals_Year = EnableYear_DAL.get_Active_Set_Goals_Year(); string empEmail = up.GetProfileValueCollection("WorkEmail")[0].ToString(); SPUser sp = spWeb.SiteUsers.GetByEmail(empEmail); string Emp_Application_Status = Dashboard_DAL.get_Emp_Application_Status(sp, Active_Set_Goals_Year)[0]; string Emp_Application_Status_Ar = string.Empty; if (Emp_Application_Status == "Objectives not set") { Emp_Application_Status_Ar = "لم يتم وضع الأهداف بعد"; } else if (Emp_Application_Status == "Objectives_set_by_Emp") { Emp_Application_Status_Ar = "تم وضع الأهداف - بإنتظار اعتماد المدير المباشر"; } else if (Emp_Application_Status == "Objectives_approved_by_DM") { Emp_Application_Status_Ar = "اعتمد المدير المباشر الأهداف"; } else if (Emp_Application_Status == "Objectives_approved_by_Dept_Head") { Emp_Application_Status_Ar = "اعتمد مدير الإدارة الأهداف"; } row["Emp_Application_Status"] = Emp_Application_Status_Ar; tblEmps.Rows.Add(row); } }); } catch (Exception) { } }
public static Emp get_Emp_Info(string strEmpDisplayName) { Emp intended_Emp = new Emp(); SPSecurity.RunWithElevatedPrivileges(delegate() { SPSite site = SPContext.Current.Site; SPWeb web = site.OpenWeb(); SPPrincipalInfo pinfo = SPUtility.ResolvePrincipal(web, strEmpDisplayName, SPPrincipalType.User, SPPrincipalSource.All, null, false); SPServiceContext serviceContext = SPServiceContext.GetContext(site); UserProfileManager userProfileMgr = new UserProfileManager(serviceContext); UserProfile cUserProfile = userProfileMgr.GetUserProfile(pinfo.LoginName); intended_Emp.login_name_to_convert_to_SPUser = pinfo.LoginName; intended_Emp.Emp_DisplayName = pinfo.DisplayName; if (cUserProfile.GetProfileValueCollection("AboutMe")[0] != null) { intended_Emp.Emp_ArabicName = cUserProfile.GetProfileValueCollection("AboutMe")[0].ToString(); } else { intended_Emp.Emp_ArabicName = string.Empty; } intended_Emp.Emp_email = pinfo.Email; //lblEmpName.Text = emp.Name; intended_Emp.Emp_JobTitle = pinfo.JobTitle; //lblEmpJob.Text = cUserProfile.GetProfileValueCollection("Title")[0].ToString(); if (pinfo.Department != null) { intended_Emp.Emp_Department = pinfo.Department; } else { intended_Emp.Emp_Department = string.Empty; } //lblEmpDept.Text = cUserProfile.GetProfileValueCollection("Department")[0].ToString(); /*I used the "Fax" field to store the "Emp_Rank" becuase there is no "Emp_Rank" field in SharePoint profile fields */ // الدرجة الوظيفية وهى من 1-14 حيث أنها تؤدى إلى اختلاف طريقة التقييم object rank = cUserProfile.GetProfileValueCollection("Fax")[0]; if (rank != null && rank.ToString() != string.Empty) { intended_Emp.Emp_Rank = cUserProfile.GetProfileValueCollection("Fax")[0].ToString(); } else { intended_Emp.Emp_Rank = "4"; } string DM_value = string.Empty; string Dept_Head_value = string.Empty; if (cUserProfile.GetProfileValueCollection("Manager")[0] != null) { DM_value = cUserProfile.GetProfileValueCollection("Manager")[0].ToString(); } if (DM_value != string.Empty) { UserProfile DM_UserProfile = userProfileMgr.GetUserProfile(DM_value); intended_Emp.DM_name = DM_UserProfile.DisplayName; intended_Emp.DM_email = DM_UserProfile["WorkEmail"].ToString(); if (DM_UserProfile.GetProfileValueCollection("Manager")[0] != null) { Dept_Head_value = DM_UserProfile.GetProfileValueCollection("Manager")[0].ToString(); } if (Dept_Head_value != string.Empty) { UserProfile Dept_Head_UserProfile = userProfileMgr.GetUserProfile(Dept_Head_value); intended_Emp.Dept_Head_name = Dept_Head_UserProfile.DisplayName; intended_Emp.Dept_Head_email = Dept_Head_UserProfile["WorkEmail"].ToString(); //============================================ // "*****@*****.**" is the test "General Director" SPGroup grp = web.SiteGroups["المدير العام"]; if (intended_Emp.Dept_Head_email == grp.Users[0].Email || intended_Emp.Dept_Head_email == "*****@*****.**") { intended_Emp.EmpHierLvl = "2"; } else { intended_Emp.EmpHierLvl = "1"; } //============================================= } else { intended_Emp.EmpHierLvl = "3"; } } else { intended_Emp.EmpHierLvl = "4"; } }); return(intended_Emp); }