/// <summary> /// Get the localized label of User Profile Properties /// Option 1 => Get the specific property to find the label (much better performance) /// </summary> /// <param name="userProfilePropertyName"></param> /// <reference> /// 1. http://nikpatel.net/2013/12/26/code-snippet-programmatically-retrieve-localized-user-profile-properties-label/ /// </reference> /// <returns></returns> public string getLocalizedUserProfilePropertiesLabel(string userProfilePropertyName, SPWeb currentWeb) { string localizedLabel = string.Empty; try { //Get the handle of User Profile Service Application for current site (web application) SPSite site = SPContext.Current.Site; SPServiceContext context = SPServiceContext.GetContext(site); UserProfileConfigManager upcm = new UserProfileConfigManager(context); //Access the User Profile Property manager core properties ProfilePropertyManager ppm = upcm.ProfilePropertyManager; CorePropertyManager cpm = ppm.GetCoreProperties(); //Get the core property for user profile property and get the localized value CoreProperty cp = cpm.GetPropertyByName(userProfilePropertyName); if (cp != null) { localizedLabel = cp.DisplayNameLocalized[System.Globalization.CultureInfo.CurrentUICulture.LCID]; } } catch (Exception err) { LogErrorHelper objErr = new LogErrorHelper(LIST_SETTING_NAME, currentWeb); objErr.logSysErrorEmail(APP_NAME, err, "Error at getLocalizedUserProfilePropertiesLabel function"); objErr = null; SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(APP_NAME, TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, err.Message.ToString(), err.StackTrace); return(string.Empty); } return(localizedLabel); }
static void MaritalStatus() { //Code example adds a new property called Marital Status. using (SPSite site = new SPSite("http://servername")) { //SPServiceContext context = SPServiceContext.GetContext(site); //ClientContext context = new ClientContext(site); //ServerContext context = ServerContext.GetContext(site); UserProfileConfigManager upcm = new UserProfileConfigManager(); try { ProfilePropertyManager ppm = upcm.ProfilePropertyManager; // create core property CorePropertyManager cpm = ppm.GetCoreProperties(); CoreProperty cp = cpm.Create(false); cp.Name = "MaritalStatus"; cp.DisplayName = "Marital Status"; cp.Type = PropertyDataType.StringSingleValue; cp.Length = 100; cpm.Add(cp); // create profile type property ProfileTypePropertyManager ptpm = ppm.GetProfileTypeProperties(ProfileType.User); ProfileTypeProperty ptp = ptpm.Create(cp); ptpm.Add(ptp); // create profile subtype property ProfileSubtypeManager psm = ProfileSubtypeManager.Get(); ProfileSubtype ps = psm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User)); ProfileSubtypePropertyManager pspm = ps.Properties; ProfileSubtypeProperty psp = pspm.Create(ptp); psp.PrivacyPolicy = PrivacyPolicy.OptIn; psp.DefaultPrivacy = Privacy.Organization; pspm.Add(psp); } catch (DuplicateEntryException e) { Console.WriteLine(e.Message); Console.Read(); } catch (System.Exception e2) { Console.WriteLine(e2.Message); Console.Read(); } } }
protected virtual CoreProperty GetCurrentCoreProperty(SPSite site, CorePropertyDefinition definition) { CoreProperty result = null; var serverContext = SPServiceContext.GetContext(site); var profileManager = new UserProfileManager(serverContext); var profilePropertiesManager = new UserProfileConfigManager(serverContext).ProfilePropertyManager; var corePropertiesManager = profilePropertiesManager.GetCoreProperties(); // would return NULL, no try-catch is required result = corePropertiesManager.GetPropertyByName(definition.Name); return(result); }
static void AddProfileProperty(string name, string displayName, bool isMultidValue) { using (SPSite oSite = new SPSite("http://servername")) { //SPServiceContext oServContext = SPServiceContext.GetContext(oSite); ClientContext clientContext = new ClientContext("http://svg-ronald:8080/"); ProfilePropertyManager prfPropMgr; ProfileSubtypeManager prfTypeMgr; ProfileSubtypePropertyManager prftypePropMgr; ProfileTypePropertyManager typPropMgr; ProfileSubtypeProperty prfTypeProp; ProfileTypeProperty prfProp; ProfileSubtype prfType; CorePropertyManager corePropMgr; CoreProperty coreProp; //prfPropMgr = new UserProfileConfigManager(oServContext).ProfilePropertyManager; prfPropMgr = new UserProfileConfigManager().ProfilePropertyManager; //prfProp.Name = name; corePropMgr = prfPropMgr.GetCoreProperties(); coreProp = corePropMgr.Create(false); coreProp.Name = name; coreProp.DisplayName = displayName; coreProp.IsMultivalued = isMultidValue; coreProp.Type = PropertyDataType.StringMultiValue; coreProp.Length = 1024; corePropMgr.Add(coreProp); typPropMgr = prfPropMgr.GetProfileTypeProperties(ProfileType.User); prfProp = typPropMgr.Create(coreProp); prfProp.IsVisibleOnViewer = true; typPropMgr.Add(prfProp); //prftypePropMgr.Add(prfTypeProp); prftypePropMgr = prfPropMgr.GetProfileSubtypeProperties(name); prfTypeProp = prftypePropMgr.Create(prfProp); prfTypeProp.IsUserEditable = true; prfTypeProp.DefaultPrivacy = Privacy.Public; prfTypeProp.UserOverridePrivacy = true; } }
public void CreateUIDProperty() { var propManager = _propertyManager.GetCoreProperties(); var property = propManager.GetPropertyByName(UUID_PROPERTY); if (property != null) { return; } Logger.WriteInfo("Creating property '" + UUID_PROPERTY + "' in profile service"); var profilePropMgr = new UserProfileConfigManager(_context).ProfilePropertyManager; var corePropManager = profilePropMgr.GetCoreProperties(); // Create the property. var coreProp = corePropManager.Create(false); coreProp.Name = UUID_PROPERTY; coreProp.DisplayName = "SPC UID"; coreProp.Type = PropertyDataType.String; coreProp.Length = 36; coreProp.IsMultivalued = false; coreProp.IsSearchable = true; corePropManager.Add(coreProp); // Create a profile type property and make the core property visible in the Details section page. var typePropManager = profilePropMgr.GetProfileTypeProperties(ProfileType.User); var typeProp = typePropManager.Create(coreProp); typeProp.IsVisibleOnViewer = true; typePropManager.Add(typeProp); // Create a profile subtype property. var subtypeManager = ProfileSubtypeManager.Get(_context); var subtype = subtypeManager.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User)); var subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties(subtype.Name); var subtypeProp = subtypePropMgr.Create(typeProp); subtypeProp.IsUserEditable = false; subtypeProp.DefaultPrivacy = Privacy.Public; subtypeProp.UserOverridePrivacy = true; subtypePropMgr.Add(subtypeProp); Logger.WriteInfo("Created SPC UID property in profile service"); }
protected virtual CoreProperty CreateNewCoreProperty(SPSite site, CorePropertyDefinition definition) { var serverContext = SPServiceContext.GetContext(site); var profileManager = new UserProfileManager(serverContext); var profilePropertiesManager = new UserProfileConfigManager(serverContext).ProfilePropertyManager; var corePropertiesManager = profilePropertiesManager.GetCoreProperties(); var coreProp = corePropertiesManager.Create(false); coreProp.Name = definition.Name; coreProp.DisplayName = definition.DisplayName; coreProp.Type = definition.Type; if (!string.IsNullOrEmpty(definition.Description)) { coreProp.Description = definition.Description; } if (definition.Length.HasValue) { coreProp.Length = definition.Length.Value; } if (definition.IsAlias.HasValue) { coreProp.IsAlias = definition.IsAlias.Value; } if (definition.IsSearchable.HasValue) { coreProp.IsSearchable = definition.IsSearchable.Value; } if (definition.IsMultivalued.HasValue) { coreProp.IsMultivalued = definition.IsMultivalued.Value; } corePropertiesManager.Add(coreProp); return(coreProp); }
protected override void Fill() { try { if (SPA.User.Count() == 0) { UserProfileManager userProfileManager = new UserProfileManager(SPServiceContext.GetContext(SPContext.Current.Site)); ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(SPServiceContext.GetContext(SPContext.Current.Site)).ProfilePropertyManager; ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile"); UserProfile userProfile = userProfileManager.GetUserProfile(Context.User.Identity.Name.Replace("i:0#.w|", "").Replace("0#.w|", "")); hfUserProfileRecordID.Value = (userProfile.RecordId.ToString()); IEnumerator<ProfileSubtypeProperty> userProfileSubtypeProperties = subtypePropMgr.GetEnumerator(); while (userProfileSubtypeProperties.MoveNext()) { string propName = userProfileSubtypeProperties.Current.Name; ProfileValueCollectionBase values = userProfile.GetProfileValueCollection(propName); if (values.Count > 0) { // Handle multivalue properties. foreach (var value in values) { switch (propName) { case "AccountName": lblAccountNameView.Text = value.ToString(); break; case "FirstName": lblFirstNameView.Text = value.ToString(); break; case "LastName": lblLastNameView.Text = value.ToString(); break; case "PreferredName": lblPreferredNameView.Text = value.ToString(); break; case "UserProfile_GUID": lblUserProfileGuidView.Text = value.ToString(); break; case "SPS-UserPrincipalName": lblUserPrincipalNameView.Text = value.ToString(); break; case "SPS-DistinguishedName": lblDistinguishedNameView.Text = value.ToString(); break; } if (ShowDebug) lblErrorMessage.Text += string.Format("Name: {0} Value: {1}</br>", propName, value.ToString()); } } } } else { trAccountName.Visible = false; trLastName.Visible = false; trFirstName.Visible = false; trPreferredName.Visible = false; trUserProfile_GUID.Visible = false; trUserPrincipalName.Visible = false; trDistinguishedName.Visible = false; lblMessageView.CssClass = "ms-error"; lblMessageView.Text = "There are already users in the data system. This option is only available when no user records exist."; btnSave.Visible = false; } } catch (Exception ex) { SPA.Error.WriteError(ex); lblErrorMessage.Text += string.Format("ERROR: {0}", ex.ToString()); } }
protected void Page_Load(object sender, EventArgs e) { string html = ""; string userValue = ""; UserProfileValueCollection valueCollection = null; object valueObject = null; _site = SPContext.Current.Site; _serviceContext = SPServiceContext.GetContext(_site); _userProfileConfigManager = new UserProfileConfigManager(_serviceContext); _profilePropertyManager = _userProfileConfigManager.ProfilePropertyManager; _profileManager = new UserProfileManager(_serviceContext); _profileSubtypePropertyManager = _profileManager.DefaultProfileSubtypeProperties; // if you need another profile subtype //_profileSubtypePropertyManager = _profilePropertyManager.GetProfileSubtypeProperties("ProfileSubtypeName"); _corePropertyManager = _profilePropertyManager.GetCoreProperties(); _profileTypePropertyManager = _profilePropertyManager.GetProfileTypeProperties(ProfileType.User); UserProfile profile = _profileManager.GetUserProfile(true); html += "<h1>First listing out all of the property types themselves</h1>"; html += "<h2>ProfileSubtypeProperty list</h2>"; html += "<table cellspacing='2' border='1'>"; html += "<tr><th>Section/Property</th><th>Name</th><th>Display Name</th><th>Type Property Name</th><th>Core Property Name</th><th>Display Order</th><th>Current User's Value</th></tr>"; foreach (ProfileSubtypeProperty profileSubtypeProperty in _profileSubtypePropertyManager.PropertiesWithSection) { userValue = ""; if (!profileSubtypeProperty.IsSection) { userValue = "<i>(none)</i>"; valueCollection = profile[profileSubtypeProperty.Name]; if (valueCollection != null) { valueObject = valueCollection.Value; if (valueObject != null) { userValue = valueObject.ToString(); } } } html += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>", profileSubtypeProperty.IsSection ? "Section" : "Property", profileSubtypeProperty.Name, profileSubtypeProperty.DisplayName, profileSubtypeProperty.TypeProperty.Name, profileSubtypeProperty.CoreProperty.Name, profileSubtypeProperty.DisplayOrder, userValue); } html += "</table>"; html += "<h2>ProfileTypeProperty list</h2>"; html += "<table cellspacing='2' border='1'>"; html += "<tr><th>Section/Property</th><th>Name</th><th>Core Property Name</th></tr>"; foreach (ProfileTypeProperty profileTypeProperty in _profileTypePropertyManager.PropertiesWithSection) { html += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>", profileTypeProperty.IsSection ? "Section" : "Property", profileTypeProperty.Name, profileTypeProperty.CoreProperty.Name); } html += "</table>"; html += "<h2>CoreProperty list</h2>"; html += "<table cellspacing='2' border='1'>"; html += "<tr><th>Section/Property</th><th>(Core Property) Name</th><th>Display Name</th><th>Type</th></tr>"; foreach (CoreProperty coreProperty in _corePropertyManager.PropertiesWithSection) { html += string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>", coreProperty.IsSection ? "Section" : "Property", coreProperty.Name, coreProperty.DisplayName, coreProperty.Type); //, //coreProperty.UseCount //"BUG!" ); } html += "</table>"; UserProfileValueCollection values = profile[PropertyConstants.PictureUrl]; if (values.Count > 0) { // Author Image: {37A5CA4C-7621-44d7-BF3B-583F742CE52F} SPFieldUrlValue urlValue = new SPFieldUrlValue(values.Value.ToString()); html += "<p><b>PictureUrl = " + urlValue.Url + "</b></p>"; } html += "<p><b>User account = " + profile[PropertyConstants.AccountName].Value.ToString() + "</b></p>"; Content.Text = html; }
public void CreateProperties(IEnumerable<UserProfileProperty> properties) { var configManager = new UserProfileConfigManager(spContext); var profilepropertyManager = configManager.ProfilePropertyManager; var corePropertyManager = profilepropertyManager.GetCoreProperties(); var propertyTypeCollection = configManager.GetPropertyDataTypes(); foreach (var property in properties) { var existProperty = corePropertyManager.GetPropertyByName(property.Name); if (existProperty == null) { var propertyType = GetPropertyType(propertyTypeCollection, property.Type); if (propertyType == null) { spLogger.DebugFormat( "Property {0} was not created, because type {1} doesn't exist", property.Name, property.Type); continue; } var coreProperty = corePropertyManager.Create(false); coreProperty.Name = property.Name; coreProperty.DisplayName = property.DisplayName; coreProperty.Type = propertyType.Name; coreProperty.IsMultivalued = property.IsMultiValue; if (property.IsMultiValue) { coreProperty.Separator = MultiValueSeparator.Semicolon; } corePropertyManager.Add(coreProperty); var profileTypePropertyManager = profilepropertyManager.GetProfileTypeProperties(ProfileType.User); var profileTypeProperty = profileTypePropertyManager.Create(coreProperty); profileTypeProperty.IsVisibleOnEditor = true; profileTypeProperty.IsVisibleOnViewer = true; profileTypePropertyManager.Add(profileTypeProperty); var profileSubTypeManager = ProfileSubtypeManager.Get(spContext); var profileSubType = profileSubTypeManager.GetProfileSubtype( ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User)); var profileSubTypeProperties = profileSubType.Properties; var profileSubTypeProperty = profileSubTypeProperties.Create(profileTypeProperty); profileSubTypeProperty.PrivacyPolicy = PrivacyPolicy.OptIn; profileSubTypeProperty.DefaultPrivacy = Privacy.Public; profileSubTypeProperty.IsUserEditable = true; profileSubTypeProperties.Add(profileSubTypeProperty); spLogger.DebugFormat("Property {0} was created", property.Name); } else { spLogger.DebugFormat("Property {0} already exists", property.Name); } } }
public void RemoveProperties(IEnumerable<string> propertyNameCollection) { var configManager = new UserProfileConfigManager(spContext); var profilepropertyManager = configManager.ProfilePropertyManager; var corePropertyManager = profilepropertyManager.GetCoreProperties(); foreach (var propertyName in propertyNameCollection) { var existProperty = corePropertyManager.GetPropertyByName(propertyName); if (existProperty != null) { corePropertyManager.RemovePropertyByName(propertyName); spLogger.DebugFormat("Property {0} was removed.", propertyName); } else { spLogger.DebugFormat("Property {0} wasn't removed, because it doesn't exist.", propertyName); } } }
protected override void Page_Load(object sender, EventArgs e) { try { base.Page_Load(sender, e); SetupContribute(); ReadParameters(); btnSave.OnClientClick = jsActionSave; jsActionCancel = string.Format("rbnCancelClick('{0}/{1}?filter={2}'); return false;", SPContext.Current.Web.Url, Pages.Users.PAGE_URL, Filter); btnCancel.OnClientClick = jsActionCancel; ibtnRibbonCancel.OnClientClick = jsActionCancel; lbtnRibbonCancel.OnClientClick = jsActionCancel; if (spePickUser.ResolvedEntities.Count > 0) { foreach (PickerEntity entity in spePickUser.ResolvedEntities) { lblUserNameView.Text = entity.Claim.Value; UserProfileManager userProfileManager = new UserProfileManager(SPServiceContext.GetContext(SPContext.Current.Site)); ProfilePropertyManager profilePropMgr = new UserProfileConfigManager(SPServiceContext.GetContext(SPContext.Current.Site)).ProfilePropertyManager; ProfileSubtypePropertyManager subtypePropMgr = profilePropMgr.GetProfileSubtypeProperties("UserProfile"); UserProfile userProfile = userProfileManager.GetUserProfile(entity.Claim.Value); hfUserProfileRecordID.Value = (userProfile.RecordId.ToString()); try { IEnumerator <ProfileSubtypeProperty> userProfileSubtypeProperties = subtypePropMgr.GetEnumerator(); while (userProfileSubtypeProperties.MoveNext()) { string propName = userProfileSubtypeProperties.Current.Name; ProfileValueCollectionBase values = userProfile.GetProfileValueCollection(propName); if (values.Count > 0) { // Handle multivalue properties. foreach (var value in values) { switch (propName) { case "FirstName": txtFirstName.Text = value.ToString(); break; case "LastName": txtLastName.Text = value.ToString(); break; case "PreferredName": txtPreferredName.Text = value.ToString(); break; case "UserProfile_GUID": lblSPObjectGuidView.Text = value.ToString(); break; //case "SPS-UserPrincipalName": // lblUserPrincipalNameView.Text = value.ToString(); // break; //case "SPS-DistinguishedName": // lblDistinguishedNameView.Text = value.ToString(); // break; } if (ShowDebug) { lblErrorMessage.Text += string.Format("Name: {0} Value: {1}</br>", propName, value.ToString()); } } } } } catch { txtLastName.Text = "Profile Not Found"; txtFirstName.Text = "Profile Not Found"; } } } if (!IsPostBack) { ibtnRibbonDelete.OnClientClick = "return confirm('Are you sure you want to delete this item?');"; lbtnRibbonDelete.OnClientClick = "return confirm('Are you sure you want to delete this item?');"; Fill(); } else { HandlePostBack(); } } catch (Exception ex) { SPA.Error.WriteError(ex); if (ShowDebug) { lblErrorMessage.Text = ex.ToString(); } } }