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:
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:
“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.
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:
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:
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:
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.