How to add SharePoint Visual WebPart to WebPart page

Vadym Gaievyi 21 Reputation points
2020-12-16T10:58:14.54+00:00

Hi!
I have a farm solution developed in VS2019, Visual Webpart and a WebPart page with WebPartZone:

 <WebPartPages:WebPartZone runat="server" Title="loc:FullPage" ID="HomeWebPartZone">
        <ZoneTemplate></ZoneTemplate>
</WebPartPages:WebPartZone>

Please, explaine me how can I put my WebPart in WebPartZone.
Thanks!

Microsoft 365 and Office SharePoint Server Development
0 comments No comments
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,126 Reputation points
    2020-12-18T08:09:05.197+00:00

    Hi anonymous user,

    Per my reasearch and test today, I find the way to add the custom web part in WebPartZone.

    In Elements.xml file of webpart page, update it as below: 49443-webpart.txt

    49279-image.png

    The <webparts> is from your visualwebpart.webpart file:

    49357-image.png


2 additional answers

Sort by: Most helpful
  1. MichaelHan-MSFT 18,126 Reputation points
    2020-12-17T08:45:54.977+00:00

    Hi anonymous user,

    To add the webpart to webpart page, the webpart needs to be in the Web Part Gallery of the site.

    If your Visual Webpart and WebPart page are in a project, you cann't put the visual webpart to webpart page. We can only add the webparts which are in the webpart gallery to a webpart page.

    As a workaround. you could add the webpart after deploying the solution.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. MichaelHan-MSFT 18,126 Reputation points
    2020-12-18T06:11:15.507+00:00

    Hi anonymous user,

    For sever-side approach, you could add the custom webpart like this:

    using (SPSite site = new SPSite("http://sp16"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        using (SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager("http://sp16/SitePages/CustomWebPartPage.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
                        {
                            Guid storageKey = Guid.NewGuid();
                            string wpId = String.Format("g_{0}", storageKey.ToString().Replace('-', '_'));
                            VisualWebPart1 webpart = new VisualWebPart1();
                            webpart.ID = wpId;
                            webpart.Title = "My Webpart";
                            wpManager.AddWebPart(webpart, "HomeWebPartZone", 0);
                        }
                    }
                }
    

    Reference:

    https://www.c-sharpcorner.com/blogs/sharepoint-programmatically-adding-our-custom-web-part-on-wiki-page1

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.