sharepoint使用C#将webpart添加到页面

时间:2012-12-05 08:33:00

标签: sharepoint listview web-parts

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

有谁知道它为什么不起作用?

提前感谢。

2 个答案:

答案 0 :(得分:0)

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

此博客中有一个示例:Add Web Part programmatically using SPLimitedWebPartManager

答案 1 :(得分:0)

以下网站在演示如何向页面添加Web部件方面非常有用。它包括有关如何添加现成的Web部件和自定义Web部件的示例。 来源:https://www.sharepointdiary.com/2012/08/how-to-add-web-parts-to-the-page-programmatically.html

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

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls.WebParts;

namespace YourProjectName.Helpers
{
    public class WebPartHelper
    {

        //Add ListView Web Part to Page
        //Source Ref: https://www.sharepointdiary.com/2012/08/how-to-add-web-parts-to-the-page-programmatically.html

        public static string AddListViewWebPartToPage(SPWeb web, 
                                                     string pageUrl, //full or relative
                                                     string ListName,
                                                     string WPTitle, 
                                                     string wpZone, // eg.  "Left", "Middle", "Right"
                                                     PartChromeType chromeType=PartChromeType.TitleAndBorder)
        {

            try
            {
                //Create the SPLimitedWebPart Manager to Add web parts
                SPLimitedWebPartManager WebPartMgr = web.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared);

                //Check for existing web part with same title first
                //Added this to ensure that I don't add the same web part multiple times, especially if this is used in a feature activate function.
                foreach (Microsoft.SharePoint.WebPartPages.WebPart wp in WebPartMgr.WebParts)
                {
                    if (wp.Title == WPTitle)
                    {
                        return "WebPart with title of [" + WPTitle + "] already exists.";
                    }
                }

                //Get the object of the list of which we are creatin the list viewer webpart
                SPList list = web.Lists[ListName];
                ListViewWebPart oListViewWP = new ListViewWebPart();
                //Set the properties of the webpart
                oListViewWP.ChromeType = chromeType;
                oListViewWP.Title = WPTitle;
                oListViewWP.ListName = list.ID.ToString("B").ToUpper();
                oListViewWP.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();

                //Define the zone in which webparts need to be added
                WebPartMgr.AddWebPart(oListViewWP, wpZone, 3);

            }
            catch (Exception ex)
            {

                return ex.ToString();

            }
            return "";

        }

    }
}