【问题标题】:sharepoint add webpart to page using C#sharepoint 使用 C# 将 webpart 添加到页面
【发布时间】:2012-11-23 00:48:56
【问题描述】:

我在将 sharepoint listviewwebparts 添加到 Web 部件页面时遇到问题。 我想用 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


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的快速回复。但我有一个问题。 XmlUrlResolver _xmlResolver = new XmlUrlResolver();不工作,它不把它作为一个功能重新调整?难道我做错了什么 ?提前致谢
    • splimitedwebpartmanager 对我也不起作用......你知道这是为什么吗?提前致谢
    【解决方案2】:

    以下网站对演示如何将 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 "";
    
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-19
      • 2015-12-12
      • 2010-12-23
      • 2013-01-29
      • 2011-08-23
      • 2015-05-05
      • 2011-06-23
      • 2010-11-12
      相关资源
      最近更新 更多