1、最近在网上搜集了一下有关处理用户配置文件的资料,整理了一下,大体如下:
string NotePadContent = null; protected override void CreateChildControls() { Literal ltNotePad = new Literal(); m_upcm = GetUPCM(); if (m_upcm != null) { if (!IsExists()) AddPropertyForNotePad(); LoadNotePad(); } } /// <summary> /// 判断用户配置文件中是否已经存在“我的备忘”属性 /// </summary> private bool IsExists() { bool resultValue = false;//不存在 m_upcm = GetUPCM(); if (m_upcm != null) { ProfilePropertyManager m_propManager = m_upcm.ProfilePropertyManager; CoreProperty m_propCore = m_propManager.GetCoreProperties().GetPropertyByName(proName); if (m_propCore != null) resultValue = true;//已存在 } return resultValue; } /// <summary> /// 判断 User Profile Service Application 服务应用程序是否已经正常启动 /// </summary> /// <returns></returns> private UserProfileConfigManager GetUPCM() { UserProfileConfigManager m_upcm = null; try { SPServiceContext myContext = SPServiceContext.GetContext(SPContext.Current.Site); m_upcm = new UserProfileConfigManager(myContext); } catch { RenderError(new Exception("User Profile Service Application未添加或正常启动,请联系系统管理员! ")); } return m_upcm; } /// <summary> /// 用户配置文件中,添加“我的备忘”属性 /// </summary> /// <returns></returns> private bool AddPropertyForNotePad() { bool resultValue = false; try { SPContext.Current.Web.AllowUnsafeUpdates = true; SPSecurity.RunWithElevatedPrivileges(delegate()//模仿管理员的权限进行操作 { using (SPSite curSite = new SPSite(SPContext.Current.Site.ID)) { string AdminUserName = "";//提升权限后的用户登录名 #region 取得提升权限后的用户登录名 using (SPWeb curWeb = curSite.AllWebs[SPContext.Current.Web.ID]) { AdminUserName = curWeb.CurrentUser.LoginName; } #endregion SPServiceContext myContext = SPServiceContext.GetContext(curSite); UserProfileConfigManager m_upcm = new UserProfileConfigManager(myContext); ProfilePropertyManager m_propManager = m_upcm.ProfilePropertyManager; CoreProperty m_propCore = null; try { m_propCore = m_propManager.GetCoreProperties().Create(false); } catch (Exception ex) { RenderError(new Exception(ex.Message + "请将" + AdminUserName + "或 当前网站集管理员设置为 User Profile Service Application 服务应用程序的管理员")); } if (m_propCore != null) { #region 设置要添加的属性的属性 ProfileTypeProperty m_propProfileType = m_propManager.GetProfileTypeProperties(profileType).Create(m_propCore); m_propCore.Name = proName; m_propCore.DisplayName = proDisplayName; m_propCore.Type = PropertyDataType.HTML; m_propCore.Length = 3600; m_propCore.IsAlias = false; m_propCore.IsMultivalued = false; m_propCore.IsSearchable = true; m_propCore.Separator = MultiValueSeparator.Comma; #endregion m_propManager.GetCoreProperties().Add(m_propCore); m_propManager.GetProfileTypeProperties(profileType).Add(m_propProfileType); m_propProfileType.IsEventLog = true;//在新闻源上显示对属性的更改 m_propProfileType.IsVisibleOnEditor = true;//在“编辑详细信息”页面上显示 m_propProfileType.IsVisibleOnViewer = true;//在用户配置文件页的“配置文件属性”部分中显示 m_propProfileType.IsReplicable = false;//不可复制 m_propProfileType.Commit(); #region 默认用户配置子类型(若此处不设置,在管理中心处看不到添加的属性) ProfileSubtypeManager manager = ProfileSubtypeManager.Get(myContext); ICollection m_userSubtypeCollection = manager.GetSubtypesForProfileType(ProfileType.User); foreach (ProfileSubtype subtype in m_userSubtypeCollection) { ProfileSubtypeProperty property2 = subtype.Properties.Create(m_propProfileType); property2.IsUserEditable = true; property2.UserOverridePrivacy = true;//用户可以替代 subtype.Properties.Add(property2); } #endregion } } }); } catch (Exception ex) { RenderError(ex); } return resultValue; } /// <summary> /// 加载 记事本 /// </summary> private void LoadNotePad() { string curUserName = SPContext.Current.Web.CurrentUser.LoginName;//当前用户的登录名 SPSecurity.RunWithElevatedPrivileges(delegate { using (SPSite mySite = new SPSite(SPContext.Current.Site.ID)) { SPServiceContext myContext = SPServiceContext.GetContext(mySite); UserProfileManager myProfile = new UserProfileManager(myContext); if (myProfile.UserExists(curUserName)) { UserProfile user = myProfile.GetUserProfile(curUserName); if (user != null && user["NotePad"].Value != null) NotePadContent = user["NotePad"].Value.ToString().Replace("<p>", "").Replace("</p>", ""); } } }); }
本站技术原创栏目文章均为中睿原创或编译,转载请注明:文章来自中睿,本站保留追究责任的权利。