Programmatically insert a List as a webpart in a webpart page in WSS 3.0

与世无争的帅哥 提交于 2019-12-03 10:22:37

问题


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!


回答1:


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




回答2:


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.




回答3:


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");


来源:https://stackoverflow.com/questions/1588019/programmatically-insert-a-list-as-a-webpart-in-a-webpart-page-in-wss-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!