Moving a WebPart to a different zone on a page

SitWalkStand
  • Moving a WebPart to a different zone on a page SitWalkStand

    How do I programmatically move a WebPart from one zone to another on a page?

    I have multiple sites that I want to loop through each site and update the page.

  • Use the SPLimitedWebPartManager.MoveWebPart method:

    string zoneId = "TopZone";           // ID of the WebPartZone control
    string zoneIndex = 0;                // Location of web part within zone
    string webPartTitle = "Sales Tips";  // Title of web part
    string filename = "default.aspx";    // Filename relative to SPWeb object
    
    using (SPLimitedWebPartManager webPartManager = 
        site.GetLimitedWebPartManager(filename, PersonalizationScope.Shared))
    {
        try
        {
         foreach (WebPart webPart in webPartManager.WebParts)
         {
          if (webPart.Title == webPartTitle)
          {
           webPartManager.MoveWebPart(webPart, zoneId, zoneIndex);
           webPartManager.SaveChanges(webPart);
           break;
          }
         }
        }
        finally
        {
         webPartManager.Web.Dispose();
        }
    }
    

  • In addition to the answer from Alex, be careful when you iterate your site collections and sites. If you do this from for example a console application where you have to establish your own context, you will run out of memory very fast if you forget to dispose your SPSite and SPWeb objects while iterating (eg. sites.AllWebs).

    read and understand the disposing guidelines on MSDN:Best Practices: Using Disposable Windows SharePoint Services Objects

Tags
web-part development
Related questions and answers
  • How can I set a web part so it will be automatically appended to a web part zone when a specific page layout is selected? The solutions I have tried so far is: Using <ZoneTemplate> under <WebPartZone> and putting my webpart in there Adding .webpart-code directly into <AllUsersWebPart> where I define the page layout, in elements.xml Put the web part on the page layouts... after it's created. would have to put .webpart-code in both a .webpart-file as well as in elements.xml (I want it to be stand alone as well as set by default). And making a mess in my elements.xml. Web

  • Does anyone else have this problem and know of a work around? On a WebPart page, add a Search Box WebPart. Edit the properties of the WebPart and change the Query text box width (in pixels) option. I tried setting mine to 50, 300 and 500 and none of these values seem to change the way the text box looks on the page. It seems the Search Box WebPart doesn't honor the value set in the TextBoxWidth attribute. Sometimes I feel like I'm a beta tester on a released product...

  • I have a web part which I am trying to target to a specific audience (the 'Staff' audience.) I have the web part in the home page of a site that has anonymous access enabled. I have set the Target Audience of the webpart to 'Staff'. The idea is that anonymous viewers can't see the web part, but logged-in members of staff can see the webpart. This works fine, as long as I am browsing the site from the server on which SharePoint is running, using the internal domain name (e.g. http://localhost:5050/site). If I view the site home page while not logged in, I can't see the webpart

  • I've created a Custom List ('Tooltips') in SharePoint. After this I used SPSource to reverse engineer the list into a feature. After installing and activating this feature, I want to add this list in a WebPart zone. When doing this, I get an error message : Tooltips: List View Web Part could not be added, list may be hidden. What's wrong with the list? feature.xml <?xml version="1.0" encoding="utf-8"?> <Feature Id="70d49f3d-233a-482d-97e3-dcec2c236171" Title="MyApp - Tooltips" Description="Creates a Tooltips list which can be used for mouse-over texts

  • When using a Data View WebPart to retrieve items from a list, The WebPart shows the results as 1-10, then next page is 11-20 and so on. It does not show whether is it 1-10 of 10 or of 10000. Is there anyway that I can customize the Data View WebPart to show paging as 1-10 of X for example?

  • I have a webpart that is part of a larger solution, deployed as a WSP using WSPBuilder. I can see the SafeControl entry in the web.config and can see that the assembly has deployed to the GAC. The solution reports as installed OK in Central Admin and the feature is activated at the site collection. When I attempt to add the webpart to a page, I'm getting an error stating that the "Type could not be found or is not registered as safe." I have removed this solution from MOSS and completely re-created the webpart, in case I've messed up GUIDs or something. I'm not sure what to check next. Any

  • I am using SharePoint Server 2007 Enterprise with Windows Server 2008 Enterprise. I am using publishing portal template. I am developing using VSTS 2008 + C# + .Net 3.5. I want to develop a WebPart and make some settings configurable (just like we can configure parameter for a console application in app.config) -- i.e. authorized people could set parameters of WebPart in Edit Page (from browser based UI interface), then select -> Modify WebPart properties, or edit parameter in SharePoint Designer. Such parameter is application specific custom parameters. Any solutions and referenced

  • SharePoint 2007 Enterprise, SP2. I am getting a strange error (found in the ULS logs, Google reports no hits on the message itself) when trying to upload a page with a webpart preloaded in a zone...;/WebPartPages:WebPartZone> The crazy part is if I clear this web part out from the zone, upload the page, and then add the web part back in thru the web UI, it works, no problem. What am I missing..., but the given markup is in XML Format., DesignText: <VPCQWP:ComponentLandingCQWP runat="server" id="CL_CQWP" /> This webpart derives from the ContentByQueryWebPart class, and sets a number of web

  • Does anyone know the JavaScript for setting display:none on an empty web part zone? I remember seeing it once on a publishing site but now can't find it. This is to avoid the padding and spacing a web part zone displays when there is no web part added to it.

Data information