Monday, April 9, 2012

Store personalized variables through custom interface in SharePoint 2010 visual web part.

In SharePoint 2010 visual web part, you can create custom properties. These variables will be displayed in the web part’s properties window.

If the custom property variable contains the attribute ‘Personalizable’, these values will be stored in a database. This will happen through the properties window. If you set a value to these variables through code, it will not store in the database. This section describes how to store the values to the database through a custom interface.

1. Create a visual web part; name it as ‘Personalizable’.

2. Open Personalizable.cs file. Add the following property.

 [WebBrowsable(false)]
[Personalizable(PersonalizationScope.User)]
public string Notes { get; set; }

The WebBrowsable attribute is set to false, because we don’t want this value to be displayed in the web part properties window. We are going to set this value through the custom interface.

3. Open ‘PersonalizableUserControl.ascx’ file. Add a text box and a button. Text box is to enter the value; the button is to store the value in the personolizable database. Name the text box as txtNotes and button as btnSave.

4. Open PersonalizableUserControl.ascx.cs file. Create a variable type Personalizable under

 public partial class PersonalizableUserControl : UserControl
 public Personalizable webPart { get; set; }

5. Double click on btnSave to create the button click event. Add the following code block under button click event. This will get the value from the text box and store in the personolizable database.

 protected void btnSave_Click(object sender, EventArgs e)
{
this.webPart.Notes = txtNotes.Text;
SPWeb web = SPContext.Current.Web;
SPFile file = web.GetFile(HttpContext.Current.Request.Url.ToString());
SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(PersonalizationScope.User);
System.Web.UI.WebControls.WebParts.WebPart webPart = manager.WebParts[this.webPart.ID];
((Personalizable)webPart).Notes = this.webPart.Notes;
try
{
web.AllowUnsafeUpdates = true;
manager.SaveChanges(webPart);
}
finally
{
web.AllowUnsafeUpdates = false;
}
}

7. In the page load event, add the following code. This will display text in the text box, when the web part loads.

 protected void Page_Load(object sender, EventArgs e)
{
if (this.webPart.Notes!=null)
{
txtNotes.Text = this.webPart.Notes;
}
}
8. Add the following code in CreateChildControls() of Personalizable.cs file.
 protected override void CreateChildControls()
{
PersonalizableUserControl control = Page.LoadControl(_ascxPath) as PersonalizableUserControl;
if (control!=null)
{
control.webPart = this;
}
Controls.Add(control);
}

The coding part is done now deploy the solution and add the web part to a web part page.

Add some text to the text box and save it. When you go to this web part page next time you can see the entered value is displaying in the text box. This data taken from the personolizable database.

Cool!!!