Dave Burke : Freelance .NET Web Developer specializing in Online Communities

CS Nuglet: Adding dynamic menubar links

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!

Comments (6) | Post RSS RSS comment feed

Posted on 10/4/2006 6:30:09 AM by Dave Burke
Categories: Community Server
Tags: No tags for this post

Related posts

Comments

10/4/2006 8:39:55 AM Permalink

Very good tip Dave!

CarKnee |

10/4/2006 8:42:43 AM Permalink

Thank you, CarKnee.  That means a lot, and it also means that last night's geek session wasn't wasted. Smile

daveburke |

10/5/2006 3:12:09 AM Permalink

If the appKey was the username could you not use the ##userName## transformer thus negate the need for an extra page?

DavidR |

10/5/2006 5:59:13 AM Permalink

David,

I think you're absolutely right.  I also think that you just gave me the confirmation that I need to talk about Community Server Url Mapping at New England Code Camp coming up later this month.  Adding a new url like this would be a good example, and when I do that I'll blog the details (unless you do it first. Smile  Thanks very much for your suggestion, David.

daveburke |

11/21/2006 7:37:39 PM Permalink

Dave,

I made the changes to SiteUrls.config file and placed the files you gave me in the \blogs directory.

I now get the "Your Blog" link in the Tabbar for Registered Users, but when I click on it it it takes me to the dreaded error page.

The URL in the address field looks like yapland.com/.../yourblog.aspx

Sachin Misra |

11/21/2006 7:52:18 PM Permalink

Sachin, We won't know what's happening without customerrors disabled in the web.config.

daveburke |

Comments are closed

This site was built with the Sueetie .NET Open Source Community Framework. Learn more about Sueetie at Sueetie.com.