manoj´ssharepointblog

SharePoint Blog

Modify property values of a web part in SharePoint website programmatically using C#

Leave a comment

Some times other than reading the values of the property of a webpart we need to update the value of a webpart. When you try to update the property of a webpart it does not get updated. This is due to the fact that the SaveProperties is not set to true.

There are two scenarios to update the properties of the webpart

Scenario 1: Updating the webpart properties in the webpart

this.WebPartProperties = “SampleText”;

this.SaveProperties = true;

Scenario 2: Updating the webpart properties externally

SPWeb web = SPContext.Current.Web;
SPFile file = web.GetFile(“URL Of the page”);
SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
System.Web.UI.WebControls.WebParts.WebPart webPart = manager.WebParts[this.ID];
((MyWebpart)webPart).Settings = this.Settings;
try
{
web.AllowUnsafeUpdates = true;
manager.SaveChanges(webPart);
}
finally
{
web.AllowUnsafeUpdates = false;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s