Monday, March 31, 2008

Custom Webpart Personalization Provider

If don't have SQL Server Express installed or want to modify its behaviour, you can create your own personalization provider. To create a custom personalization provider, the following steps are required:

1) Update web.config for custom personalization provider:

<webparts>
<personalization defaultprovider="AspNetTextFilePersonalizationProvider">
<providers>
<add name="AspNetTextFilePersonalizationProvider" type="TextFilePersonalizationProvider">
</providers>
</personalization>
</webparts>

2) Create the personalization provider which should be derived from
PersonalizationProvider. The following is a Example given in the MSDN at http://msdn2.microsoft.com/en-us/library/aa479037.aspx


using System;
using System.Configuration.Provider;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Text;
using System.IO;
public class TextFilePersonalizationProvider : PersonalizationProvider
{
}

Add this code in the App_Code folder of your application. It also can be compiled as a library and placed in the bin folder of the application. The following changes are required for that:

1) Update the web.config to include assembly info

<webparts>
<personalization defaultprovider="AspNetTextFilePersonalizationProvider">
<providers>
<add name="AspNetTextFilePersonalizationProvider"
type="TextFilePersonalizationProvider, <strong>CustomProviders</strong>"/>
</providers>
</personalization>
</webparts>

2) compile the assembly. You may need to reference additional assemblies than given below
csc /t:library /out:Samples.AspNet.CS.Controls.dll /r:System.dll /r:System.Web.dll *.cs

No comments: