I have a requirement where I have to loop through all the web parts in my page layout whether they are in WebPartZone or not and render only the webpart instead of rendering webpart zone.

I am able to loop through for the WebPartZone, but unfortunately I am not able to loop through the webparts inside the page.

I have written this code to find the webpart in a page:

SPLimitedWebPartManager splwManager = web.GetLimitedWebPartManager(HttpContext.Current.Request.Url.ToString(), System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

        foreach (WebPart wprt in splwManager.WebParts.OfType<WebPart>())

but here I am getting the problem that my wprt.Page object is null. So it's giving me error that "Parameter value cannot be null. Parameter Required: Page"

Here is the code I have written for WebPartZone:

WebPartZone webPartZone = Control as WebPartZone;

if (webPartZone != null)
    foreach (WebPart wp in webPartZone.WebParts)

Looking for someone's help.

Thanks in advance.

Here is what I found a workaround.

  • The Page property should be returned since you are inheriting from System.Web.UI.WebControls.WebParts.WebPart. Can you check whether all the web parts are deployed correctly in your page?
    – Deepu Nair
    Jan 4, 2012 at 9:52
  • @DeepuNair : I have checked and found that all the web parts are added properly. In a normal scenario - I have added a web part page layout and added a "Content Editor web part" into the page content part instead of 'web part zone'.
    – Ankit
    Jan 4, 2012 at 10:07
  • Why is it again that you want to go through the trouble of rendering the Web Parts youself? The framework does that for you! It would help to know you are trying to achieve here that the framework cannot do. Jan 4, 2012 at 17:48
  • @LarsFastrup : Coz I don't want the framework to do that. The framework is rendering the webpart zone and that makes so many tables which I don't want to render. I want to remove the webparts tables. that is why I am rendering the webpart myself.
    – Ankit
    Jan 5, 2012 at 10:54

2 Answers 2


Rather than using the web.GetLimitedWebPartManager, Can you try this way instead?

This method reads from the Pages Library first, and iterates each page in Pages Library to get all the webparts associated within each page.

SPList pagesList = null; 
 pagesList = site.Lists["Pages"];
 if (pagesList!=null)
  SPListItemCollection pages = pagesList.Items;
  foreach (SPListItem page in pages)
   SPFile file = page.File;
   using (SPLimitedWebPartManager mgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
     SPLimitedWebPartCollection webparts = mgr.WebParts;
     foreach (System.Web.UI.WebControls.WebParts.WebPart wp in webparts)                     
       \\insert your code

Answer is referred from this post.

  • I have already tried this and also able to get the webpart. but the problem here I am facing is that the wp.Page property is coming null. so that doesn't allow me to do any this. I want to render only the webpart instead of rendering the webpart zone. I found a small solution for the same what I am trying to do. blog.mastykarz.nl/removing-web-parts-tables-sharepoint-2010
    – Ankit
    Jan 5, 2012 at 10:57

Please have a look at this for workaround.

Removing Web Parts tables in SharePoint 2010

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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