sharepoint使用C#将Webpart添加到页面中

问题描述 投票:1回答:2

我在将共享点listviewwebparts添加到Webpart页面时遇到问题。我想用C#以编程方式进行操作。我做了页面,但添加listviewwebpart无法正常工作。代码如下。

   private void addListViewWebPart(SPFeatureReceiverProperties properties, String _listName)
        {
            using (SPWeb _web = properties.Feature.Parent as SPWeb)
            {
                SPList _list = _web.Lists.TryGetList(_listName);

                if (_list != null)
                {
                    ListViewWebPart _webPart = new ListViewWebPart();

                    _webPart.ZoneID = "Left";
                    _webPart.ListName = _list.ID.ToString("B").ToUpper();
                    _webPart.ViewGuid = _list.Views[0].ID.ToString("B").ToUpper();

                    SPWebPartCollection _webPartColl = _web.GetWebPartCollection("Default.aspx", Storage.Shared);
                    System.Guid _guid = _webPartColl.Add(_webPart);
                }
            }
        }

任何人都知道为什么它不起作用?

预先感谢。

sharepoint listview web-parts
2个回答
0
投票

您将需要使用SPLimitedWebPartManager及其SPLimitedWebPartManager方法。

此博客中有一个示例。:AddWebPart


0
投票

以下网站在演示如何向页面添加Web部件方面非常有用。它包括有关如何添加即用Web部件和自定义Web部件的示例。来源:Add Web Part programmatically using SPLimitedWebPartManager

如何将ListViewWebpart添加到页面的示例:

https://www.sharepointdiary.com/2012/08/how-to-add-web-parts-to-the-page-programmatically.html
© www.soinside.com 2019 - 2024. All rights reserved.