3

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.

3 Answers 3

5

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

6
  • Thanks! I think using ASP.NET custom web control could achive my needs. Two more questions regarding to ASP.NET custom web control. 1. Could I use SharePoint Designer 2007 with ASP.NET custom web control -- i.e. drag and drop ASP.NET custom web control into master page? 2. Could you provide a guide about how to develop and deploy ASP.NET custom web control for SharePoint Server 2007? I have never did this before.
    – George2
    Dec 6, 2009 at 12:23
  • 1
    You really should both create the web control in Visual Studio, and deploy it using features and solutions (eg the feature that provisions your custom master and page layouts). Using SharePoint designer will unghost all your master pages and page layouts and should be avoided at all costs! If you choose to insert control using SPD you can do that (bot dont say i didnt warn ya;-). Its the same way you insert everything else: a web control is just a tagprefix and a control. Im no guru on SPD but take a look here blogs.msdn.com/spdsupport Dec 6, 2009 at 13:28
  • I have tried the following approach, not sure whether it is correct. 1. Register WebPart as safe control and enable it from Modify All Sites Content --> Web Parts, 2. in SharePoint Designer 2007, select Insert --> SharePoint Controls --> Web Part? Is that correct step?
    – George2
    Dec 6, 2009 at 13:31
  • BTW: which document in the blog do you refer to? It contains too many documents. :-)
    – George2
    Dec 6, 2009 at 13:31
  • 1
    I was talking in general terms. I hardly ever use SPD so i wouldnt be able to tell you how to do it other than using the source view. Heres a link that explains how to insert a custom control in a page msdn.microsoft.com/en-us/library/bb964680.aspx#sectionSection1 Dec 6, 2009 at 16:19
5

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();
    	}
    }
}
5
  • Hi Steve, how to a web part to master page using SP Designer?
    – George2
    Dec 6, 2009 at 13:24
  • I have tried the following approach, not sure whether it is correct. 1. Register WebPart as safe control and loads it from All Sites Content --> Web Parts, 2. in SharePoint Designer 2007, select Insert --> SharePoint Controls --> Web Part? Is that correct step?
    – George2
    Dec 6, 2009 at 13:30
  • 1
    In SP Designer that is the correct step. Did you make your dwp or webpart file? If so, you can import it using sp designer. In SP Designer (or though the UI), when you see the toolpane on the right where you can pick a webpart. Click where it says browse and select import. Then you can import the webpart from its dwp or webpart file. Dec 6, 2009 at 15:43
  • 1
    Its true you can use use web parts the same way as custom controls inserting it in a master outside a zone. I just cant see alot of good reasons to use a web part instead of a control in this case. Dec 6, 2009 at 16:21
  • 1
    I guess in my experience I like to be flexible. Maybe day 1 the business wants it one way and then later on they want it another way. It's nice to not have to recompile or make any code changes. Dec 6, 2009 at 18:52
1

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

1
  • I need to add WebPart to an existing site. I read the code of the first post. It mentions how to add a document library web part, but how to add a custom developed web part?
    – George2
    Dec 6, 2009 at 12:14

Your Answer

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

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