SharePoint - Check If MySite Exists
SharePoint - Check If MySite Exists SharePoint MySites can be a good place to store data unique to individual users, but since they are usually provisioned only when a user first visits their MySite, we often need to check ahead of time whether the MySite exists. One way to solve this problem is to create a simple user control that checks whether the current user's MySite exists, and provides the URL if it is found.
MySite.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MySite.ascx.cs" Inherits="MyProject.Portal.WebControls.MySite, MyProject.Portal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2791977307baf46c" %> <%@ Assembly Name="MyProject.Portal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2791977307baf46c" %> <span id="MySiteUrl"><asp:HiddenField ID="MySiteUrl" runat="server" /></span>
MySite.ascx.cs
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);
UserProfile profile = profileManager.GetUserProfile(HttpContext.Current.User.Identity.Name);
if (profile.PersonalSite != null) {
MySiteUrl.Value = profile.PersonalSite.Url.ToString();
}
}
Using the MySite Control
We included this control on the masterpage, and simply searched for the span tag with id "MySiteUrl" when it was needed.
