Quantcast
Channel: Solutionizing .NET » MySite
Viewing all articles
Browse latest Browse all 2

MySite Redirect Update

$
0
0

Per Ian’s suggestion, I’ve updated the MySite Redirect solution (CodePlex) to support the Force=True parameter passed by the “My Settings” link.

I was curious how the default control handled this case, so I went hunting. The delegate control is added in the MySite farm feature (which also adds the controls for “My Site” and “My Links” to delegates GlobalSiteLink1 and 2, respectively) via _controltemplates/mysiteredirection.ascx, which loads Microsoft.SharePoint.Portal.WebControls.MySiteRedirectionUserControl. The redirect logic of the control is obfuscated, but I still got something  for my effort:

public void OnFormInit(object objOfInterest)
{
    SPListItem user = objOfInterest as SPListItem;
    if (user != null)
    {
        this.RedirectIfNecessary(user);
    }
}

It had never occurred to me that objOfInterest might actually be of interest, but it turns out to be an item from the internal users list (_catalogs/users). As much fun as it was to go hunting for the account in the control tree, I prefer the performance of a simple cast:

public void OnFormInit(object objOfInterest)
{
    var user = objOfInterest as SPListItem;
    if(user != null)
        RedirectIfNecessary(user[SPBuiltInFieldId.Name] as string);
}

I also changed ServerContext.Default to ServerContext.Current, which is shared across an HttpContext. The revised redirect logic is pretty clean:

protected void RedirectIfNecessary(string user)
{
    try
    {
        if (string.IsNullOrEmpty(user))
            return;

        bool force = false;
        if (bool.TryParse(Request.QueryString["Force"], out force) && force)
            return;

        var sc = ServerContext.Current;
        if (sc == null)
            return;

        var mgr = new UserProfileManager(sc);
        if (mgr != null && mgr.UserExists(user))
            Response.Redirect(mgr.MySiteHostUrl.Trim('/') +
                              "/Person.aspx?accountname=" + user);
    }
    catch (SPException) { }
}

Updated Source v0.2

Update 3/27/2009: Updated Source v0.3

  • Get username from Account field (Name site column) instead of Title
  • Quit early if user is empty

Tagged: IFormDelegateControlSource, MySite, OnFormInit, User Profile

Viewing all articles
Browse latest Browse all 2

Trending Articles