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) { } }
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
February 18, 2009 at 9:21 pm
Hi, Fantastic idea, unfortunately I havent been able to get this to work. The wsp was added/deployed fine, I even checked to make sure the dll was deployed to the GAC etc. I confirmed the feature was activated but the same userdisp.aspx page is displayed. Is there another step or should this just work once deployed?
Cheers
February 18, 2009 at 10:35 pm
Hey Nick ~
In theory it should “just work” once the feature is activated, assuming the other necessary pieces are in place (SSP with user profiles, My Site for the user, etc). Any evidence of foul play in the ULS Logs? Beyond that, you might try changing the Sequence value in Elements.xml to something lower. In a development environment you can always attach to the process and see if a breakpoint at OnFormInit gets hit. Or build a version that logs exceptions instead of just ignoring them. Let me know if you figure it out!
February 22, 2009 at 9:43 pm
Hi Keith, Took a little bit of digging and some quick trial and error. In the end I created a new control with your logic and attached the debugger as suggested.
Looks like it was this line
RedirectIfNecessary(user.Title);
. When the final redirect string was created it would pass the users display name, and appear to do nothing as I’m guessing it must redirect back to originating page if the profile lookup fails. For it to work for me it wanted the account name so I changed that line toRedirectIfNecessary(user["Account"].ToString());
to pass the account name instead. Starting working fine after that…?!February 25, 2009 at 8:58 pm
Thanks for reporting back! I’ve confirmed the behavior you report and will be releasing a new version shortly. That’s what I get for superficial testing. :)
I hate hard-coding field names and am paranoid about null reference exceptions, so this is the code I’m using:
RedirectIfNecessary(user[SPBuiltInFieldId.Name] as string);
Thanks ~
Keith