I Want to add all the webparts already exists on a Page with different zoneIDs into "Top" zone using c#.
I know i can use this code of line
SPLimitedWebPartManager wpManger = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared);
webpartdefault webpart = new webpartdefault();
wpManger.AddWebPart(webpart, "Top", 1);
but thats when I add a webpart, But in my case all webparts are already there.
I tried this code, but it doesn't work as this property is read only
SPLimitedWebPartManager wpManger = web.GetLimitedWebPartManager("default.aspx",
foreach (System.Web.UI.WebControls.WebParts.WebPart eachwebpart in wpManger.WebParts)
{
eachwebpart.AllowZoneChange = true;
eachwebpart.Zone = "Top";
wpManger.SaveChanges(eachwebpart);
}
Ok guys, I made this code that actually works, but not for CreateModuleContent, and a list :/
protected void setCorrectView(SPWeb web)
{
web.AllowUnsafeUpdates = true;
SPWebPartCollection wpcWebParts = web.GetWebPartCollection(PageUrl, Storage.Shared);
foreach (Microsoft.SharePoint.WebPartPages.WebPart wptWebPart in wpcWebParts)
{
string titl = wptWebPart.Title;
if (wptWebPart.ZoneID != "Top")
{
wptWebPart.AllowZoneChange = true;
wptWebPart.ZoneID = "Top";
wpcWebParts.SaveChanges(wptWebPart.StorageKey);
}
}
web.AllowUnsafeUpdates = false;
}
Cheers
