Update a web part programmatically

This is an example how we can update a web part programmatically in multiple sites.

LiteralControl lit = new LiteralControl();
lit.Text = "";
try
{
using (SPSite site = new SPSite(SPContext.Current.Web.Site.RootWeb.Url))
{
for (int i = 1; i < site.AllWebs.Count; i++)
{
lit.Text += "<BR/>" + site.AllWebs[i].Title;
SPFile file=site.AllWebs[i].GetFile(site.AllWebs[i].ServerRelativeUrl + "/default.aspx");
if ( file.Exists)
{
using (Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager wpm = site.AllWebs[i].GetLimitedWebPartManager(file.Url, PersonalizationScope.Shared))
{
foreach (WebPart wp in wpm.WebParts)
{
if (wp.Title == "Notes")
{
wp.Height = new Unit("200px");
wpm.SaveChanges(wp);
break;
}
}
}
}
}
}
}
catch (Exception ex)
{
lit.Text += "<BR/>" + ex.ToString();
}
this.Controls.Add(lit);

Comments

Popular Posts

SharePoint Interview Questions and Answers

Download Infopath Form Templates

How to get current logged user information using JavaScript ?

Steps to set Form based authentication (FBA) for SharePoint 2010

SharePoint Interview Questions and Answers II

Get List Items - JavaScript

Cross Site List Rollup Web Part for SharePoint 2010

Hide Recently Modified Items

Change Language for current user with JSOM in SharePoint Online

SharePoint 2010 CSS Chart