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
The <webparts> is from your visualwebpart.webpart file:
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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!
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
The <webparts> is from your visualwebpart.webpart file:
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.
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: