How to create Web Part instances on pages using Features - FeatureReceiver - SPLimitedWebPartManager


Table of Contents

To add a instance of a web part using SPLimitedWebPartManager you'll need to be using the Object Model. The best way to use the Object Model to add a instance to a page in a site (SPWeb) is to activate a Feature scoped at 'Web' level with a Feature Receiver that triggers on activation. Note that you are going to be using a Feature to add the Web Part to the Site Collection Web Part Gallery, but this will be scoped at 'Site' level.

Once you have your base Feature created (see Creating Features) with a Feature Receiver simply add some extra code to the FeatureActivated method.

 public override void FeatureActivated(SPFeatureReceiverProperties properties) {
   using (SPWeb web = properties.Feature.Parent as SPWeb)
   {
    SPFile file = web.GetFile(web.Url + "/default.aspx");
    using (SPLimitedWebPartManager webpartsMng = file.GetLimitedWebPartManager(PersonalizationScope.User))
    {
     ContentEditorWebPart webPart = new ContentEditorWebPart();
     webPart.Title = "Test Web Part";
     webpartsMng.AddWebPart(webPart, "Right", 0);
    webpartsMng.Web.Dispose();
   }
  }
}

So to walk through each line.

Line 02: this instantiates the site (SPWeb object) in based on the context that the Feature has been activated at 'Web' level.

Line 04: this instantiates the page (SPFile object) from the site (SPWeb). If you are using a WCM site you need to be aware that the page will be in a Pages Library so the url will be slightly different e.g. "/Pages/default.aspx".

Line 05: this instantiates the SPLimitedWebPartManager that will manage adding web parts to the page. Remember there are two types of [Personalization scopes]: PersonalizationScope.User and PersonalizationScope.Shared.

Line 07: this instantiates the ContentEditorWebPart (but this can be any type of web part that implements System.Web.UI.WebControls.WebParts.WebPart).

Line 08: this sets the Title of the web part. Other properties can be set here and extended properties of particular more complexed web parts (e.g. Content Query web part).

Line 9: this tells the manager to add the web part to the page in the [web part zone] named "Right" and at index 0.

Other examples

Content by Query Web Part (CQWP)

 ContentByQueryWebPart contentByQueryWebPart = new ContentByQueryWebPart();
 contentByQueryWebPart.Title = "Schedule";
 contentByQueryWebPart.WebUrl = web.Url; 
 SPListTemplateCollection listTemplates = web.Site.RootWeb.ListTemplates;
 SPListTemplate template = listTemplates["Schedule"];
 contentByQueryWebPart.BaseType = string.Empty;
 contentByQueryWebPart.ServerTemplate = Convert.ToString((int)template.Type, CultureInfo.InvariantCulture);
 contentByQueryWebPart.CommonViewFields = "Air_x0020_Date,AirDate;Episode_x0020_Number,Episode;Season_x0020_Number,Season";
 contentByQueryWebPart.ItemStyle = "ScheduleStyle";
 contentByQueryWebPart.ItemXslLink = site.Url + "/Style%20Library/jeremythake/schedule.xsl";
 webpartsMng.AddWebPart(contentByQueryWebPart, "Right", 0); 

Line 03: this will set the scope of the site that it will take as the root and therefore its children
Line 04: this will filter the CQWP by a particular List Template called 'Schedule'
Line 08: this will set the site columns that will be available to the xslt for rendering
Line 09: this will select the style that is part of the xsl to define how each item is rendered
Line 10: this will point to the xsl that is published with the xslt styles mapped in Line 09

Labels

splimitedwebpartmanager splimitedwebpartmanager Delete
webpart webpart Delete
cqwp cqwp Delete
contenteditorwebpart contenteditorwebpart Delete
xslt xslt Delete
contentbyquerywebpart contentbyquerywebpart Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.



Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare