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;
web.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = false;

Leave a Reply

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

You are commenting using your 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