Question

I am using SharePoint Server 2007 Enterprise with Windows Server 2008 Enterprise, and I am using publishing portal template. I am developing using VSTS 2008 + C# + .Net 3.5.

I want to know, How to add a WebPart to all pages of a SharePoint Site?

Any reference samples?

I want to use this WebPart to display some common information (but the information may change dynamically, and that's why I choose a WebPart) on all pages.

Was it helpful?

Solution

That information changes dynamically does not mean you have to use a WebParts.

Use WebParts when you

  • Need designer/contributor to change the default functionality by either adding WebParts or change properties on WebParts without having to involve a developer
  • Have a specific reason like using it as Filter WebParts, Connected WebParts, caching etc.

For what you are doing i would instead recommend using an ordinary ASP.NET custom web control.

Depending on requirements this can be placed in either master pages or layout pages.

You can also choose to develop a delegate control which gives you more flexibility to add new controls and have different behavior on different levels of the site structure. More on delegate controls

hth Anders Rask

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top