Fredrik Normén's Blog - NSQUARED²
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Microsoft Most Valuable Professional
     .Net Framework - ASP.Net - Architecture - Development
NOTE: This list of posts will only list the 15 latest posts, to see the rest, select from the Archives located in the menu to the left. The RSS will only list the 10 lastest posts.
Make the WebPart personalize a page on its raw Url.

Category:  ASP.Net 2.0

There have been some discussions on forums about how to make the WebPart’s Personalization feature to personalize WebParts for a raw Url (Url with querystrings and) not only the Url to the requested page (Url including only the name of the page that is requested).

 

Today when you navigate to a page with a querystring, the WebPart’s personalization feature will ignore the querystring and only load and save personalization data based on the requested page without including the querystrings. There is a way around this, which I’m going to write about in this post.

 

Note: I will extend my WebParts Components with this extension when Beta 2 is released. The reason why I don’t add this today is because the SqlPersonalizationProvider that I’m going to extend is sealed in the current CTP release of VS 2005, but will be unsealed in beta 2.

 

To make the WebPart’s Personalization feature to personalize content based on a raw Url, you have to create a personalization provider that will get the personalization data from the current requested page’s raw Url. The simplest why of doing this is to create a class that inherits the SqlPersonalizationProvider (Remember that this class I currently sealed, but will be unsealed for beta 2). By inheriting the SqlPerosnalizationProvider, you can use the ASP.Net databases created when you run the aspnet_regsql.exe for the Sql providers. There are four methods of the SqlPersonalizationProvider you need to override for making sure you will only load, save or reset the personalization for the current requested page’s raw Url. The four methods you need to override are the LoadPersonalizationBlobs, SavePersonalizationBlob, ResetPersonalizationBlob and GetCountOfState. Within those four methods you need to get the raw Url of the current requested page and pass it as a value to one of the base class LoadPersonalizationBlobs,SavePersonalizationBlob,ResetPersonalizationBlob and GetCountOfState method's path argument (The GetCountOfState uses a PersonalizationStateQuery as argument, which you change its PathToMatch property with the raw Url and pass the PersonalizationQuery to the base class method). The following code is an implementation of the custom SqlPersonalizationProvider:

 

public class Nsquared2SqlPersonalizationProvider : SqlPersonalizationProvider

{

  

    private string GetPath

    {

        get

        {

           HttpContext context = HttpContext.Current;

           if (context == null)

              throw new HttpException("Context can not be Null");

 

           HttpRequest request = context.Request;

           if (request == null)

              throw new HttpException("The current Context's Request property can not be Null");

 

           return request.RawUrl;

        }

    }

 

    protected override void LoadPersonalizationBlobs(WebPartManager webPartManager, string path, string userName, ref byte[] sharedDataBlob, ref byte[] userDataBlob)

    {

        base.LoadPersonalizationBlobs(webPartManager, this.GetPath, userName, ref sharedDataBlob, ref userDataBlob);

    }

 

    protected override void ResetPersonalizationBlob(WebPartManager webPartManager, string path, string userName)

    {

        base.ResetPersonalizationBlob(webPartManager, this.GetPath, userName);

    }

 

    protected override void SavePersonalizationBlob(WebPartManager webPartManager, string path, string userName, byte[] dataBlob)

    {

        base.SavePersonalizationBlob(webPartManager, this.GetPath, userName, dataBlob);

    }

 

    public override int GetCountOfState(PersonalizationScope scope, PersonalizationStateQuery query)
    {
       if ((HttpContext.Current != null) && (HttpContext.Current.Handler is Page))
       {
          Page page = (Page)HttpContext.Current.Handler;
          WebPartManager webPartManager = WebPartManager.GetCurrentWebPartManager(page);
          if (webPartManager != null)
          {
             query.PathToMatch = this.GetPath;
             return base.GetCountOfState(scope, query);
          }
       }

      

       return base.GetCountOfState(scope, query);
    }

}

 

To use the extensions created in this post, you need to specify in the configuration file that you should use the custom SqlPersonalizationProvider as the default provider.

 

Note: I can’t promise that this code will work without doing some changes when Beta 2 is released. The idea of this post is to give you the basic understanding of what you need to do to make sure the WebPart’s Personalization feature could also save and load personalization data for pages with querystring.

Posted: Monday, January 10, 2005 - 20:07 GMT+1    Print     E-mail    Comments (6)
Feedback
re: Make the WebPart personalize a page on its raw Url. - Andreas Håkansson

Fredrik,

I just wanted to drop a thought on the use of Exceptions in one of your properties

private string GetPath
{
get
{
HttpContext context = HttpContext.Current;
if (context == null)
throw new ArgumentException("Context can not be Null");

HttpRequest request = context.Request;
if (request == null)
throw new ArgumentException("The current Context's Request property can not be Null");

return request.RawUrl;
}
}

The part that caught my attention was the use of the ArgumentException. This could prove to be very confusing to developers since this is the kind of exception you would expect to be thrown by a property setter (set) och a method, where you pass values into the code, not from code returning data.
Posted: Wednesday, January 12, 2005 - 13:00 GMT+1  
re: Make the WebPart personalize a page on its raw Url. - Fredrik Normén

Andreas:

Do you know what, you have absolutely right, when I did a refactoring of my code, I removed two arguments, context and the request. But I did actually forget to change the exception. Thank you for letting me know.
Posted: Wednesday, January 12, 2005 - 14:51 GMT+1  
re: Make the WebPart personalize a page on its raw Url. - John Vowles

Fredrik,

This may be a stupid question, but is there anyway to extend the SQLPersonalizationProvider before BETA 2 is released? Have microsoft made the source available?

Many thanks
Posted: Tuesday, February 08, 2005 - 18:28 GMT+1  
re: Make the WebPart personalize a page on its raw Url. - Fredrik Normén

John Vowles:

The source code is not available. I think you can extend the SqlPersonlaizationProvider if you use a later build than the Beta 1. But I can't promise that you don't need to do any changes when beta 2 is released, but I think you only need to do very small changes if you need to do any.
Posted: Tuesday, February 08, 2005 - 19:01 GMT+1  
re: Make the WebPart personalize a page on its raw Url. - Cindy Mello

Don't know how to ask question. for wss services that search result gets a parameter from the querystring filter=1 to turn on display filter combo lists, how can I write to the Allitems.aspx to always have filter value = 1?? Can I use a web part to set filter=1 when it is not in the querystring so many months on this puzzle Can I make the page reload with post back data and append the ?filter=1 to return request.RawUrl;
Posted: Thursday, February 10, 2005 - 01:19 GMT+1  
Thanks - Lee

This is invaluable, it worked on the first run.
Posted: Thursday, January 14, 2010 - 16:21 GMT+1  

Add feedback
Subject:
Name:
URL:
Message:




Please enter the text from the image:   

 
   fredrik.nsquared2.com - 2007