In a SharePoint 2007 web part, I want to delete an existing property and replace it with a property using a different name. I want to get the value from the existing property and assign it to the new property.
How should I do this?
In a SharePoint 2007 web part, I want to delete an existing property and replace it with a property using a different name. I want to get the value from the existing property and assign it to the new property.
How should I do this?
In summary:
In code:
using (SPSite site = new SPSite("http://sharepoint"))
using (SPWeb web = site.OpenWeb("Web Title"))
using (SPLimitedWebPartManager webPartManager =
web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared))
{
try
{
foreach (WebPart webPart in webPartManager.WebParts)
{
if ((webPart.Title == "Web Part Title") && (!webPart.IsClosed))
{
YourWebPart wp = (YourWebPart)webPart;
wp.NewProperty = wp.OldProperty;
webPartManager.SaveChanges(wp);
web.Update();
break;
}
}
}
finally
{
webPartManager.Web.Dispose();
}
}
Replace the following in this code example: