Accessing current post information from Widget

Topics: Business Logic Layer, Controls
Sep 5, 2009 at 12:41 AM

Is there any possible way to access the currently served post from the Widget?

Coordinator
Sep 5, 2009 at 12:57 AM

Somebody just asked me this the other day.  Here's the code I sent him.  This will tell you the current Post or Page being served.

public override void LoadWidget()
{
    string currentPage = System.IO.Path.GetFileName(HttpContext.Current.Request.PhysicalPath);
    string id = Request.QueryString["id"];
    if (!string.IsNullOrEmpty(id) && id.Length == 36)
    {
        Guid itemId = new Guid(id);
        if (currentPage.Equals("page.aspx", StringComparison.OrdinalIgnoreCase))
        {
            BlogEngine.Core.Page page = BlogEngine.Core.Page.GetPage(itemId);
        }
        else if (currentPage.Equals("post.aspx", StringComparison.OrdinalIgnoreCase))
        {
            BlogEngine.Core.Post post = BlogEngine.Core.Post.GetPost(itemId);
        }
    }
}

Sep 5, 2009 at 1:03 AM

But this won't work when URL rewriting is used, right? Seems like, this code is only useful when PermaLink is used. But still, thanks for the heads-up. Atleast I know the direction in which I should be working. I was hoping to have some method by which I can directly access the Post object. Also, I was hoping to use Session state + Extension to store the Post's GUID in Session and then use it for my widget. Do tell me your comments on it.

 

Coordinator
Sep 5, 2009 at 1:08 AM

This WILL work when URL rewriting is done.  For example, if the RawUrl is:

http://allben.net/post/2009/08/06/Performance-Compiled-vs-Interpreted-Regular-Expressions.aspx

That will be rewritten to something like:

http://allben.net/post.aspx?id=SOME GUID

The rewritten URL is not visible to the visitor.  The code I posted will look at Request.Url which is the rewritten URL containing the Guid.  Request.RawUrl contains the original URL visible to the visitor in their address bar.

Sep 5, 2009 at 1:14 AM

Ah OK....Thanks for that......

Sep 5, 2009 at 9:16 AM

Following on from that then Ben, is it possible to have a widget that only displays is you are on a "page" - or conversely a "post" - so for sake of argument, a "post information widget" that is hidden when the displayed content is not of type "post" - equally how could you extend this so that you had a widget that is only displayed on a given page - say "default.aspx" will only show a hacked (with this functionality) "newsletter widget"

Coordinator
Sep 5, 2009 at 10:06 AM

It can be done, but I think the only way would be to make a small modification to WidgetZone.cs and WidgetBase.cs.  Here's what I tried with success.

In WidgetBase.cs, add this new property:

private bool _DisplayWidget = true;
public bool DisplayWidget
{
    get { return _DisplayWidget; }
    set { _DisplayWidget = value; }
}

    
In WidgetZone.cs, in the OnLoad() method, add the IF statement after LoadWidget(), and before the widget is added to the widget zone.

control.LoadWidget();
if (control.DisplayWidget)
{
    this.Controls.Add(control);
}

Then in your widget's LoadWidget() method, you can have logic to decide if the widget should be displayed.  If it shouldn't then it can set DisplayWidget to false.

public override void LoadWidget()
{
    // some code to determine if this widget should be displayed.
    this.DisplayWidget = false;
}

Sep 5, 2009 at 1:04 PM

I just added user verification so that if the user is an Administrator, the Widget will be shown irrespective of it's DisplayWidget Value.

 

if (!control.DisplayWidget)
{
    if (Page.User.Identity.IsAuthenticated && Roles.IsUserInRole(BlogEngine.Core.BlogSettings.Instance.AdministratorRole))
        {
            this.Controls.Add(control);
        }
}
else this.Controls.Add(control);
Helps in managing buggy widgets(common thing for me :D)

 

Sep 13, 2009 at 3:44 AM

Hey Ben,

    Thanks for all the support. I'd finally managed to finish the widget I was working on here is the demo of it. I would really appreciate feedback on it.

Thanks again,

Mayank

Coordinator
Sep 13, 2009 at 8:06 AM

Mayank: I took a look -- nice job.  Now I know what all the commotion generated in this thread was about! <g>

I added your widget to the Widget Downloads page in the BE documentation wiki.