簡體   English   中英

sharepoint 使用 C# 將 webpart 添加到頁面

[英]sharepoint add webpart to page using C#

我在將 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);
                }
            }
        }

有誰知道為什么它不起作用?

提前致謝。

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

此博客中有一個示例。: 使用 SPLimitedWebPartManager 以編程方式添加 Web 部件

以下網站對演示如何將 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 "";

        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM