Pregunta

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.

¿Fue útil?

Solución 2

Please have a look at this for workaround.

Removing Web Parts tables in SharePoint 2010

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
scroll top