The last few weeks I have worked on a WCMS Publishing Portal where I had to programmatically add and modify some WebParts. Although this is not a big deal I had to look up some thing which I want to summarize in this blog post. So here’s the scenario… 

In this case I have an out of the box WCMS Publishing Portal which looks like this:

WCMS Publishing Portal homepage WCMS Publishing Portal subsite

At the sub site I want to programmatically place a ListViewWebPart, a SummaryLinkWebPart and a ContentEditorWebPart. In order to do this I can utilize the following namespaces:

  • Microsoft.SharePoint.WebPartPages (ListViewWebPart and ContentEditorWebPart)
  • Microsoft.SharePoint.Publishing.WebControls (SummaryLinkWebPart)

which can be found at:

  • Microsoft.SharePoint.dll
  • Microsoft.SharePoint.Publishing.dll

SPLimitedWebPartManager, SPWebPartManager and the difference

First of all I wondered about the two classes: SPLimitedWebPartManager and SPWebPartManager because I didn’t know when to use them.

SPWebPartManager

The SPWebPartManager “Manages all the Web Part controls, functionality, and events that occur on a Web page.” It can be found e.g. in the blueband.master:

Using SPWebPartManager in a master page

“Any SharePoint page that includes Web Parts must have an instance of the SPWebPartManager on the page to manage Web Parts. This object tracks which zones each Web Part is in, connection information related to Web Parts, personalization, and page edit mode.”

SPLimitedWebPartManager

The SPLimitedWebPartManager “Provides a limited set of Web Part operations that can be performed in our object model scenarios when there is no HttpContext and no Page object is instantiated.”

So the difference is that the SPLimitedWebPartManager has a limited set of functionality which can be used without a HttpContext.

Usage

In order to get an instance of the SPLimitedWebPartManager I used the following (strong simplified) code:

SPWeb web = new SPSite("http://moss2007").AllWebs["PressReleases"];
PublishingWeb pWeb = null;
 
if (PublishingWeb.IsPublishingWeb(web))
    pWeb = PublishingWeb.GetPublishingWeb(web);
 
using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(
       pWeb.DefaultPage.Url, PersonalizationScope.Shared))
{
    if (pWeb.DefaultPage.CheckOutStatus == SPFile.SPCheckOutStatus.None)
        pWeb.DefaultPage.CheckOut();
 
    //Todo...
 
    if (pWeb.DefaultPage.CheckOutStatus != SPFile.SPCheckOutStatus.None)
        pWeb.DefaultPage.CheckIn("Information...");
}

Don’t forget to check out and check in the page.

Version history of a page item.

ListViewWebPart

The ListViewWebPart is a WebPart which displays the content of a list or library. The following simplified code adds a ListViewWebPart for the library Pages to the TopColumnZone:

//ListView WebPart
SPList list = web.Lists["Pages"];
 
ListViewWebPart wp = new ListViewWebPart();
 
wp.WebId = web.ID;
wp.Title = list.Title;
wp.ListName = list.ID.ToString("B").ToUpper();
 
manager.AddWebPart(wp, "TopColumnZone", 1);

The result looks like this:

Rendered page with a programmatically added WebPart

Note that there is no change of the view which is used.

SummaryLinkWebPart

The SummaryLinkWebPart is a WebPart which displays links. The following simplified code adds a SummaryLinkWebPart to the LeftColumnZone:

//SummaryLinkWebPart
SummaryLinkWebPart smWp = new SummaryLinkWebPart();
smWp.Title = "My links";
 
manager.AddWebPart(smWp, "LeftColumnZone", 1);

The result looks like this:

Rendered page with a programmatically added WebPart

ContentEditorWebPart

The ContentEditorWebPart is a WebPart which is used to create content with an Html editor. The following simplified code adds a ContentEditorWebPart to the RightColumnZone:

ContentEditorWebPart cewp = new ContentEditorWebPart();
 
cewp.Title = "Content Editor WebPart";
 
XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlElement = xmlDoc.CreateElement("Content");
xmlElement.InnerText = "<a href=''>Some uncool link here</a>";
cewp.Content = xmlElement;
 
manager.AddWebPart(cewp, "RightColumnZone", 1);

Using an XmlDocument you can place some content before the WebPart is added to the page. The result looks like this:

Rendered page with a programmatically added WebPart

Although I have used a ContentEditorWebPart you can also use FieldControls of the type RichHtml.

Conclusion

As usual you can do a lot of stuff with the SharePoint object model. If you want to use one of the described methods you shouldn’t forget to:

  • Include exception handling,
  • Check for objects that are null and to
  • Upgrade the verification if the page is checked out or not.

I hope this post gives a short overview about how to programmatically add WebParts.

Comments (2) -

2/25/2009 8:08:03 AM #

Trackback from namics SharePoint Weblog

WCMS mit SharePoint (MOSS) 2007 – Teil 5

namics SharePoint Weblog Reply

5/26/2009 2:49:27 PM #

Thank you for that. It helped a great deal! It's crazy though that the ListName in ListViewWebPart must be uppercase.

Boris Gomiunik Slovenia Reply

Pingbacks and trackbacks (1)+

Add comment




  Country flag
biuquote
Loading