SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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


share|improve this question
you can use powershell for that – Vishal_Sharepoint Apr 13 '12 at 9:42
I am making a Form Application that is doing many operations including this one, so that isn't the option here am afraid :S, thanks anyway – Muhammad Raja Apr 13 '12 at 9:46
up vote 3 down vote accepted

Have you tried SPLimitedWebPartManager.MoveWebPart

And remember to call wpManager.Web.Dispose(); even though you didn't allocate it

share|improve this answer
I just figured out that myself and was about to post the code but you already answered it , cheers :) – Muhammad Raja Apr 13 '12 at 10:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.