Programmatically insert a List as a webpart in a webpart page in WSS 3.0
Asked Answered
C

3

2

I tried searching on the net to programmatically insert a List as a webpart in a webpart page but was not lucky enough.

Any thoughts or ideas how i could Programmatically insert a List as a webpart in a webpart page

Many Thanks!

Casias answered 19/10, 2009 at 10:48 Comment(0)
W
7

First add these using statements.

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

Then in your code

// First get the list
SPSite site = new SPSite("http://myserver");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["MyCustomlist"];

// Create a webpart
ListViewWebPart wp = new ListViewWebPart();
wp.ZoneID = "Top";   // Replace this ith the correct zone on your page.
wp.ListName = list.ID.ToString("B").ToUpper();
wp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();

// Get the web part collection
SPWebPartCollection coll = 
    web.GetWebPartCollection("default.aspx",    // replace this with the correct page.
    Storage.Shared);

// Add the web part
coll.Add(wp); 

If you want to use a custom view, try playing with this:

SPView view = list.GetUncustomizedViewByBaseViewId(0);
wp.ListViewXml = view.HtmlSchemaXml;

Hope it helps, W0ut

Wahoo answered 19/10, 2009 at 20:58 Comment(1)
any idea about #1596415 please?Casias
C
1

You need to perform two steps to add a web part to a page. First you have to create the list you want to show on the page. Therefore you can use the Add() method of the web site's list collection (SPListCollection).

To show the list on the web part page you have to add a ListViewWebPart to the web part page using the SPLimitedWebPartManager of the page.

Capitulation answered 19/10, 2009 at 12:31 Comment(1)
Thanks Flo! have u got any example or sample please?Casias
S
1

To make this more re-usable as part of a feature receiver, you could pass in the splist and spview as part of a method:

static public void AddEventsListViewWebPart(PublishingPage page, string webPartZoneId, int zoneIndex, string webPartTitle, PartChromeType webPartChromeType, string listName, string viewname)
{
     using (SPLimitedWebPartManager wpManager = page.ListItem.File.GetLimitedWebPartManager(PersonalizationScope.Shared))
     {
         SPWeb web = page.PublishingWeb.Web;
         SPList myList = web.Lists.TryGetList(listName);
         using (XsltListViewWebPart lvwp = new XsltListViewWebPart())
         {
             lvwp.ListName = myList.ID.ToString("B").ToUpperInvariant();
             lvwp.Title = webPartTitle;
             // Specify the view
             SPView view = myList.Views[viewname];
             lvwp.ViewGuid = view.ID.ToString("B").ToUpperInvariant();
             lvwp.TitleUrl = view.Url;
             lvwp.Toolbar = "None";
             lvwp.ChromeType = webPartChromeType;
             wpManager.AddWebPart(lvwp, webPartZoneId, zoneIndex);
         }
     }
}

And then call it during feature activation:

AddEventsListViewWebPart(welcomePage, "Right", 1, "Events", PartChromeType.TitleOnly, "Events", "Calendar");
Shroyer answered 26/9, 2012 at 2:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.