I saw a post on Community Server Forums yesterday from someone who wanted to know how to add a menubar link to the current user's blog. There were other items on my CS Latenight Geek Session Short List that I could have done last night, but this sounded like an interesting pursuit.
What we're after is a link that will take the registered user to his blog, not control panel, but to his actual blog. The url would typically be something like http://site/blogs/blogAppKey/default.aspx. We need a dynamic blogAppKey. A screenpic below shows the working update.
I spent too much time on investigating this only because I was being dumb. I was initially thinking I could change a menutab's url on the fly, on each page load or user login. Wasn't gonna happen, since the SiteUrlsData class of the SiteUrls Provider packages up the links and urls in the SiteUrls.config file on startup and that's that. This is a beautiful thing that I wasn't going to mess with to create a dynamic menubar url.
Then it dawned on me to use a simple redirect.
I added the navigation link to SiteUrls.config
<link name="yourblog" resourceUrl="yourblog" text="Your Blog" roles="Registered Users" />
The roles attribute as "Registered Users" was important here to not display the link for anonymous users or registered users not currently logged in.
Then my redirection page url element in the SiteUrls.config.
<url name = "yourblog" location = "weblogs" path="yourblog.aspx" pattern="yourblog.aspx" vanity="yourblog.aspx" />
The redirection page obtains the user's Weblog ApplicationKey and takes the user to their blog. It could be much simpler than the code below if, say, the user's applicationkey was the same as their username, for instance. The code doesn't handle the "admin" account and assumes each user is owner of one blog only.
public partial class yourblog : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string _user = CSContext.Current.UserName;
string _applicationKey = GetWeblogApplicationKey(_user);
if (_applicationKey != null)
Response.Redirect(BlogUrls.Instance().HomePage(_applicationKey));
}
}
private string GetWeblogApplicationKey(string username)
{
ArrayList ownedBlogs = new ArrayList();
ArrayList weblogs = Weblogs.GetWeblogs(true, false, false);
string _applicationKey = null;
foreach (Weblog weblog in weblogs)
{
foreach (string owner in weblog.OwnerArray)
{
if (owner.ToLower() == username.ToLower())
{
_applicationKey = weblog.ApplicationKey;
break;
}
}
}
return _applicationKey;
}
}
Now if I could only find that Forums post, which I somehow lost!