Friday, April 21, 2006

 

Reading and modifying the configuration of web parts from an external tool

The Web Part Infrastructure in ASP.NET 2 offers a very neat system that allows users to configure and personalize parts of a web page, dragging the web parts to their preferred position, etc... When you are administering a web site that works with web parts, you'll soon want to know how many of your users actually personalized their page. And what settings did they use? Surely, you can find this out by inspecting the personalization database? Wrong.

The personalization of all web parts in a web part page is stored in a binary field in the database. This field holds the position and configuration of all web parts on the page and also which extra parts a user may have added to her page. All this information is not readily available.

Last year, I wrote a post detailing how the WebPartManager stores its information in the personalization system and I created a small library that could be used to read and change the configuration of a page from code and without the ASP.NET context available. This was working with beta 2 of ASP.NET and sadly, the format of the storage later changed in anumber of very subtle ways, leaving the library broken.

I have now updated the library to work with the final release (it turned out to be difficult to have it work with both versions). It comes with a little sample program that shows how to read the binary info from the database, make a few changes and save it back to the database. This is the code of the sample program:



As you can see, I use a typed dataset and the automatically generated TableAdapter to load the byte array of configuration from the SQL Express database, then I create a PageState object with this data. In the PageState, I make two changes: I delete all dynamically added web parts from the page and I set the web part with ID Weather to the RightZone. Lastly, the updates are stored in the dataset and persisted to the database.

The library and sample program can be downloaded here. No warranties.

Labels:


Comments:
Teun, your library is way cool!
Finally a way to read/set those WebPart settings from outside the WebPartManager.
 
This is great. Could you show a sample for adding a new webpart?
 
Teun,

I'm using your WebPartTools library to read existing personalization records from the database, altering the info in it and saving it back to the database. That goes well.

When I clear the DynamicParts, Locations and WebPartSettings properties in such an existing PageState object that I've read from the database, and after that add new DynamicPartInfo, LocationInfo and PersonalizationInfo objects, reusing the controlID's en wrapperID's from the existing PageState, when I save the blob to the database it still goes well. In other words, I can replace all public properties with my own newly created objects.

But when I create a new PageState object, and then add exactly the same new DynamicPartInfo, LocationInfo and PersonalizationInfo objects, and save that altered PageState to the database, it goes wrong. The webpart page that should show the altered webpart doesn't show anything, and the personalization record is even deleted from the database. I think the webpart manager doesn't like the format or something.

The public properties of the PageState object are exactly the same as in the working case. The only thing I can come up with is that the private _wpm property of your PageState class isn't populated as should be? After creation that one is empty, but when the Encode method is called, it is filled with info about the CustomProperties.

Do you know what could be wrong?

Thanks in advance for your help!
 
@Jeroen: You found a bug. When creating a page state from scratch, the type of the web part manager was not set. It has been fixed now and I have updated the download.
 
I downloaded the code for this, but the PerUser classes are missing! Is there somewhere to get these?
 
@Andy:
I included a slightly more complete version of this library in the code downloadfor my book on web parts. You can download it from http://www.teuntostring.net/webpartinfra/

The book includes a chapter on how to use the library, but you can use the code freely without buying the book (which is only €14).
 
This post has been removed by a blog administrator.
 
This post has been removed by a blog administrator.
 
Teun, you library was really helpful to me as well. I hope Microsoft is not going to change format of WebPart blob again.

Viktar Web Developer Blog
 
You've just helped me (and others) imposibly much!!! :)
Your library is great!
Thank you very very very much :)!!!!!
 
i've even just bougth your book from "lulu"!
You saved lost of my time and i think that it is the best way i can tell thanks again :)
 
Post a Comment

Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?