以编程方式将List作为webpart插入WSS 3.0中的webpart页面

K-M*_*K-M 2 sharepoint moss web-parts

我尝试在网上搜索以编程方式在Webpart页面中插入一个List作为webpart但是不够幸运.

任何想法或想法如何我可以编程方式在Webpart页面中插入一个列表作为webpart

非常感谢!

Wou*_*out 7

首先添加这些使用语句.

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中

// 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); 
Run Code Online (Sandbox Code Playgroud)

如果您想使用自定义视图,请尝试使用此方法:

SPView view = list.GetUncustomizedViewByBaseViewId(0);
wp.ListViewXml = view.HtmlSchemaXml;
Run Code Online (Sandbox Code Playgroud)

希望它有所帮助,W0ut