简体   繁体   中英

sharepoint add webpart to page using C#

I have a problem with adding sharepoint listviewwebparts to a webpart page. i want to do it programmatically with C#. i have made the page but adding the listviewwebpart isnt working. code is below.

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

anyone knows why it isnt working?

thanks in advance.

You will need to use the SPLimitedWebPartManager and its AddWebPart Method.

There is an example in this blog.: Add Web Part programmatically using SPLimitedWebPartManager .

The following web site was very helpful in demonstrating how to add a web part to a page. It includes samples of how to add both out of the box web parts and custom web parts. Source: https://www.sharepointdiary.com/2012/08/how-to-add-web-parts-to-the-page-programmatically.html

Sample of how to add a ListViewWebpart to a page:

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 "";

        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM