【问题标题】:Programmatically insert a List as a webpart in a webpart page in WSS 3.0以编程方式在 WSS 3.0 的 Web 部件页面中插入列表作为 Web 部件
【发布时间】:2010-12-07 23:08:25
【问题描述】:

我尝试在网上搜索以编程方式将列表作为 web 部件插入到 web 部件页面中,但不够幸运。

我如何以编程方式将列表作为 web 部件插入到 web 部件页面中的任何想法或想法

非常感谢!

【问题讨论】:

    标签: sharepoint moss web-parts


    【解决方案1】:

    首先添加这些 using 语句。

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

    然后在你的代码中

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

    如果你想使用自定义视图,试试这个:

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

    希望对你有帮助, W0ut

    【讨论】:

    【解决方案2】:

    您需要执行两个步骤才能将 Web 部件添加到页面。首先,您必须创建要在页面上显示的列表。因此,您可以使用网站列表集合 (SPListCollection) 的 Add() 方法。

    要在 Web 部件页面上显示列表,您必须使用页面的 SPLimitedWebPartManagerListViewWebPart 添加到 Web 部件页面。

    【讨论】:

    • 谢谢弗洛!请问你有任何例子或样品吗?
    【解决方案3】:

    为了使其作为特征接收器的一部分更可重用,您可以将 splist 和 spview 作为方法的一部分传递:

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

    然后在功能激活时调用它:

    AddEventsListViewWebPart(welcomePage, "Right", 1, "Events", PartChromeType.TitleOnly, "Events", "Calendar");
    

    【讨论】:

      猜你喜欢
      • 2012-01-15
      • 2010-11-02
      • 1970-01-01
      • 2021-07-01
      • 2011-11-12
      • 1970-01-01
      • 2011-03-12
      • 2011-06-27
      • 2011-09-20
      相关资源
      最近更新 更多