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);
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