private void DisplayProfile() { try { System.Security.PermissionSet ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted); ps.Assert(); Microsoft.SharePoint.SPServiceContext serviceContext = Microsoft.SharePoint.SPServiceContext.Current; UserProfileManager upm = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext); ProfileSubtypePropertyManager pspm = upm.DefaultProfileSubtypeProperties; UserProfile profile = upm.GetUserProfile(true); //_accountName); this.Controls.Add(new Literal() { Text = "<table border='0' > <tr><td>Displayname</td><td>Value</td></tr>" }); string[] props = { "UserName", "FirstName", "LastName", "WorkEmail", "Department" }; foreach (string prop in props) { string text = string.Format("<tr><td>{0}</td><td>{1}</td></tr>", profile[prop].ProfileSubtypeProperty.DisplayName, profile[prop].Value); this.Controls.Add(new Literal() { Text = text }); } string txtEdit = "<tr><td colspan='2'>"; btnEdit = new Button() { Text = "Edit" }; this.Controls.Add(new Literal() { Text = txtEdit }); this.Controls.Add(btnEdit); this.Controls.Add(new Literal() { Text = "</td></tr>" }); btnEdit.Click += new EventHandler(btnEdit_Click); /* foreach (ProfileSubtypeProperty prop in pspm.PropertiesWithSection) { if (prop.IsSection) this.Controls.Add(new Literal() { Text = string.Format("<tr><td colspan='3'><b>Section: {0}</b></td></tr>", prop.DisplayName) }); else { string text = string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", prop.DisplayName, prop.Name, profile[prop.Name].Value); this.Controls.Add(new Literal() { Text = text }); } }*/ this.Controls.Add(new Literal() { Text = "</table>" }); } catch (Exception ex) { this.Controls.Add(new Literal() { Text = ex.ToString() }); } finally { System.Security.CodeAccessPermission.RevertAssert(); } }
void ChangeProfile(string fname, string lname, string email, string org, long recordID) { try { SPSecurity.RunWithElevatedPrivileges(delegate() { //SPIisSettings settings = SPContext.Current.Site.WebApplication.IisSettings[SPContext.Current.Site.Zone]; Microsoft.SharePoint.SPServiceContext serviceContext = Microsoft.SharePoint.SPServiceContext.Current; UserProfileManager upm = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext); UserProfile profile = upm.GetUserProfile(recordID); //_accountName); /* MembershipProvider prov= System.Web.Security.Membership.Providers[settings.FormsClaimsAuthenticationProvider.MembershipProvider]; MembershipUser usr = prov.GetUser(SPContext.Current.Web.CurrentUser.LoginName, false); if (usr != null) { usr.Email = email; System.Web.Security.Membership.UpdateUser(usr); } */ //profile["Title"].Value = fname + " " + lname; profile["FirstName"].Value = fname; profile["LastName"].Value = lname; profile["WorkEmail"].Value = email; profile["Department"].Value = org; profile.Commit(); }); } catch (Exception ex) { throw ex; } finally { // System.Security.CodeAccessPermission.RevertAssert(); } }
void ChangeProfile(string fname, string lname, string email, string org, string orgtype, string position) { HttpContext tempCtx = HttpContext.Current; try { SPSite Site = SPContext.Current.Site; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite ProfileSite = new SPSite(Site.ID)) { //SPIisSettings settings = SPContext.Current.Site.WebApplication.IisSettings[SPContext.Current.Site.Zone]; HttpContext.Current = null; Microsoft.SharePoint.SPServiceContext serviceContext = SPServiceContext.GetContext(ProfileSite); UserProfileManager upm = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext); UserProfile profile = upm.GetUserProfile(Site.RootWeb.CurrentUser.LoginName); //_accountName); /* MembershipProvider prov= System.Web.Security.Membership.Providers[settings.FormsClaimsAuthenticationProvider.MembershipProvider]; MembershipUser usr = prov.GetUser(SPContext.Current.Web.CurrentUser.LoginName, false); if (usr != null) { usr.Email = email; System.Web.Security.Membership.UpdateUser(usr); } */ //profile["Title"].Value = fname + " " + lname; profile["FirstName"].Value = fname; profile["LastName"].Value = lname; profile["WorkEmail"].Value = email; profile["Department"].Value = org; profile["OrganizationType"].Value = orgtype; profile["Position"].Value = position; profile.Commit(); } }); } catch (Exception ex) { throw ex; } finally { HttpContext.Current = tempCtx; } }
string GetFullNameFromProfile() { string FullName = string.Empty; try { System.Security.PermissionSet ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted); ps.Assert(); Microsoft.SharePoint.SPServiceContext serviceContext = Microsoft.SharePoint.SPServiceContext.Current; UserProfileManager upm = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext); ProfileSubtypePropertyManager pspm = upm.DefaultProfileSubtypeProperties; UserProfile profile = upm.GetUserProfile(true); FullName = profile["FirstName"].Value + " " + profile["LastName"].Value; } catch (Exception) { } return FullName; }
/// <summary> /// Updates the profile data. /// </summary> protected void UpdateItemsInProfile(string ListGuid, string ID) { HttpContext currentContext = HttpContext.Current; try { SPSite Site = SPContext.Current.Site; string LoginName = SPContext.Current.Web.CurrentUser.LoginName; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite ProfileSite = new SPSite(Site.ID)) { //fixes the context issue in SharePoint 2010 and 2007 when elevating privileges //remember to set context back in finally statement HttpContext.Current = null; Microsoft.SharePoint.SPServiceContext serviceContext = SPServiceContext.GetContext(ProfileSite); UserProfileManager upm = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext); UserProfile profile = upm.GetUserProfile(LoginName); ProfileValueCollectionBase values = profile.GetProfileValueCollection("UserTools"); values.Add(ListGuid + ":" + ID); profile.Commit(); } }); } catch (Exception ex) { } finally { HttpContext.Current = currentContext; } }