Pregunta

Estoy utilizando SharePoint Server 2007 Enterprise con Windows Server 2008 Enterprise, y estoy usando la publicación de plantilla de portal. Estoy desarrollando el uso de VSTS 2008 + + C # .NET 3.5.

Quiero saber, ¿Cómo añadir un WebPart a todas las páginas de un sitio de SharePoint?

Cualquier muestra de referencia?

Quiero usar esta WebPart para mostrar cierta información común (pero la información puede cambiar de forma dinámica, y es por eso que elegir un WebPart) en todas las páginas.

¿Fue útil?

Solución

Esta información cambia de forma dinámica no significa que usted tiene que utilizar un WebParts.

Use WebParts cuando

  • Necesidad diseñador / colaborador para cambiar la funcionalidad por defecto ya sea añadiendo WebParts o propiedades de cambio en WebParts sin tener que involucrar a un desarrollador
  • Tener una razón específica como usarlo como filtro WebParts, conectado WebParts, el almacenamiento en caché, etc.

Por lo que está haciendo en su lugar me gustaría recomendar el uso de un control personalizado ASP.NET Web convencional.

En función de los requisitos de esta se puede colocar en cualquiera de las páginas maestras o páginas de diseño.

También puede optar por desarrollar un control delegado que le da más flexibilidad para añadir nuevos controles y tienen un comportamiento diferente en diferentes niveles de la estructura del sitio. Más sobre controles de delegado

hth Anders Rask

Otros consejos

I think a webpart will work either way, because a webpart is just a more advanced webcontrol. I would do it one of two ways:

  1. Add it to the master page or page layout using SP Designer.
  2. Add it to every page programmically using a feature

1 is if you need the webpart to exist where there isn't a webpart zone. You'll need to make the change to every different instance of the master page or page layout that you will want to have the control on. Most people just have one master page so this could be an easy win.

2 limits you to just pages that have web part zones. Remember adding a webpart programmically requires it be added to a zone.

The code to add a webpart looks like this:

ContentEditorWebPart webpart = new ContentEditorWebPart();
webpart.ChromeType = PartChromeType.TitleOnly;
webpart.Title = "Content Editor";

XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlElement = xmlDoc.CreateElement("MyElement");
xmlElement.InnerText = "My Text";
webpart.Content = xmlElement;        

using (SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager(pageUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
{
    try
    {
    	mgr.AddWebPart(webPart, "MiddleLeftZone", 0);
    }
    finally
    {
    	if (mgr != null &&
    		mgr.Web != null)
    	{
    		mgr.Web.Dispose();
    	}
    }
}

There are two ways to do this depending on your situation.

If the sites exist already, you need to iterate over the sites, adding the web part:

http://blogs.msdn.com/tconte/archive/2007/01/18/programmatically-adding-web-parts-to-a-page.aspx

If the sites do not exist then you can add the web part to the site template:

https://stackoverflow.com/questions/234302/how-to-add-a-web-part-page-to-a-site-definition

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