Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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>())
        {                    
            wprt.RenderControl(writer);
        }

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)
    {
        wp.RenderControl(writer);
    }
}

Looking for someone's help.

Thanks in advance.


Here is what I found a workaround. blog.mastykarz.nl/removing-web-parts-tables-sharepoint-2010

share|improve this question
    
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 '12 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 '12 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. –  Lars Fastrup Jan 4 '12 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 '12 at 10:54
add comment

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.

share|improve this answer
    
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 '12 at 10:57
add comment
up vote 1 down vote accepted

Please have a look at this for workaround.

Removing Web Parts tables in SharePoint 2010

share|improve this answer
add comment

Your Answer

 
discard

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.