0

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

  • 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
3

Have you tried SPLimitedWebPartManager.MoveWebPart

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

  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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