I'm trying to create a Web Part that will conditionally display its contents if the current user has permission to see a specific user profile property. Here's what I've got so far. It seems that I can use the ProfileSubtypeProperty privacy settings, but I'm not sure what to do. I'd like to hide the content when either 1) the user cannot override the privacy settings and the default privacy setting doesn't allow the current user to view the profile property or 2) the user can override the privacy settings and has done so so that the current user can't see the profile property.
The first part, I think I can figure out using UserOverridePrivacy and DefaultPrivacy. The second part is what I need help with. How can I check to see what whether or not the property should be visible?
This stems from an earlier question I'd asked.
Control code:
[ParseChildren(true)] [PersistChildren(false)] [ToolboxItemAttribute(false)] public class UppvWebPart : WebPart { public class UppvWebPartTemplate : WebControl, INamingContainer { protected override void Render(HtmlTextWriter writer) { this.RenderContents(writer); } } protected override void CreateChildControls() { String accountName = HttpContext.Current.Request["accountname"]; if (UvvpWebTemplate != null && PropertyName != null && accountName != null) { SPWeb thisWeb = SPContext.Current.Web; SPServiceContext context = SPServiceContext.GetContext(thisWeb.Site); UserProfileConfigManager upcm = new UserProfileConfigManager(context); ProfilePropertyManager ppm = upcm.ProfilePropertyManager; UserProfileManager upm = new UserProfileManager(context); if (upm.UserExists(accountName)) { UserProfile userProfile = upm.GetUserProfile(accountName); ProfileSubtypeProperty psp = userProfile.Properties.GetPropertyByName(PropertyName); if( /* what to do with psp? */) { ProfileValueCollectionBase pv = userProfile.GetProfileValueCollection(PropertyName); if (pv != null) { foreach (String v in pv) { HttpContext.Current.Response.Output.WriteLine("<p>{0}</p>", v); } Control container = new Control(); this.Controls.Add(container); UvvpWebTemplate.InstantiateIn(container); } } } } } public virtual String PropertyName { set; get; } [Browsable(false)] [DefaultValue(null)] [PersistenceMode(PersistenceMode.InnerProperty)] [TemplateContainer(typeof(UppvWebPartTemplate))] public virtual ITemplate UvvpWebTemplate { set; get; } }
And here is the usage:
<UppvWebPart:UppvWebPart PropertyName="Hobbies" runat="server" Description="My WebPart" Title="UppvWebPart" __MarkupType="vsattributemarkup" __WebPartId="{339ea49b-f2b0-4bd6-a029-73ead84e4915}" WebPart="true" __designer:IsClosed="false" id="UppvWebPart1"> <UvvpWebTemplate><div id="Bam1Div" runat="server">Show the property!</div></UvvpWebTemplate> </UppvWebPart:UppvWebPart>
/* what to do with psp */
above in part could be! psp.UserOverridePrivacy && psp.DefaultPrivacy == Privacy.Public || psp.UserOverridePrivacy && /* ??? */
, but