Stefan:
There are only two things you forgot to mention ;) The SqlPeronslaizationProvider is sealed in the latest CTP build of ASP.Net. Hopefully this will not be sealed when beta 2 is released!?
A new custom WebPartPersonalization class must also be created where the HasPersonalizationState method must be override and also pass the Request.RawUrl to the providers GetCountOfState method.
Because there must be a new WebpartPersonalization class, there must also be a new custom WebPartManager class where the CreatePersonalization method is override and returns the new custom WebPartPersonalization class:
Custom WebPartManager:
public class MyWebPartManager : WebPartManager
{
public MyWebPartManager()
{
}
protected override WebPartPersonalization CreatePersonalization()
{
return new Nsquared2WebPartPersonalization(this);
}
}
Custom WebPartPersonalization:
public class Nsquared2WebPartPersonalization : WebPartPersonalization
{
private PersonalizationProvider _provider;
public Nsquared2WebPartPersonalization(WebPartManager owner) : base(owner)
{
}
public override bool HasPersonalizationState
{
get
{
if (this._provider == null)
throw new InvalidOperationException("Can't use the WebPartPersonalization's HasPersonalizationState before Page_Init");
if (this.WebPartManager.Page == null)
throw new InvalidOperationException("The WebPartManager.Page property can not be null");
HttpRequest request = this.WebPartManager.Page.Request;
if (request == null)
throw new InvalidOperationException("The WebPartManager.Page.Request property can not be null");
PersonalizationStateQuery query = new PersonalizationStateQuery();
query.PathToMatch = request.RawUrl;
if ((this.Scope == PersonalizationScope.User) && request.IsAuthenticated)
query.UsernameToMatch = this.WebPartManager.Page.User.Identity.Name;
return (this._provider.GetCountOfState(this.Scope, query) > 0);
}
}
private void DeterminePersonalizationProvider()
{
if (string.IsNullOrEmpty(this.ProviderName))
{
this._provider = PersonalizationAdministration.Provider;
}
else
{
PersonalizationProvider provider = PersonalizationAdministration.Providers[this.ProviderName];
if (provider == null)
{
throw new ProviderException(string.Format("Can't found provider '{0}'", this.ProviderName));
}
this._provider = provider;
}
}
protected override PersonalizationScope Load()
{
if (!this.Enabled)
throw new InvalidOperationException("Personalization not enabled");
this.DeterminePersonalizationProvider();
return base.Load();
}
....
}
Custom SqlPersonalizationProvider (When it's not sealed ;) )
public class Nsquared2SqlPersonalizationProvider : SqlPersonalizationProvider
{
private string GetPath(WebPartManager webPartManager)
{
if (webPartManager == null)
throw new ArgumentNullException("webPartManager");
if (webPartManager.Page == null)
throw new ArgumentException("The webPartManager's Page property can not be Null");
HttpRequest request = webPartManager.Page.Request;
if (request == null)
throw new ArgumentException("The webPartManager's Page.Request property can not be Null");
path = request.RawUrl;
}
protected override void LoadPersonalizationBlobs(WebPartManager webPartManager, string path, string userName, ref byte[] sharedDataBlob, ref byte[] userDataBlob)
{
base.LoadPersonalizationBlobs(webPartManager, this.GetPath(webPartManager), userName, sharedDataBlob, userDataBlob);
}
protected override void ResetPersonalizationBlob(WebPartManager webPartManager, string path, string userName)
{
base.ResetPersonalizationBlob(webPartManager, this.GetPath(webPartManager), userName, sharedDataBlob, userDataBlob);
}
protected override void SavePersonalizationBlob(WebPartManager webPartManager, string path, string userName, byte[] dataBlob)
{
base.ResetPersonalizationBlob(webPartManager, this.GetPath(webPartManager), userName, sharedDataBlob, userDataBlob);
}
}
/Fredrik Normén -
fredrikn @ twitterASPInsider
Microsoft MVP, MCSD, MCAD, MCT
ASPInsidersMy Blog