none
How to add a Custom webpart to the newly created webpart page immediately while in Edit mode? RRS feed

  • Question

  • How to add a Custom webpart to the newly created webpart page immediately while in page Edit mode?

    I have created page based on my custom layout having 3  zones.  while click on new page create button, it shouldn't add my web parts to zones. I wrote the code in 

     protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);

                if (OOCContext.Current.PortletID == Portlets.Portlet20)
                {
                    if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
                    {

                            // lot of code 

                        }

                   }

                Addwebpart(); // calling Webparts to add to my page.

    }// End of my Onload method

    Could you please help me wheat i need to do to add these webparts to newly created page with out refresh or save click.

    its working only when Refresh or save click. Please provide to call any script or code to show webparts after click on Page creation?

    Thursday, August 2, 2018 7:12 AM

All replies

  • Hi,

    As a workaround, we can add web part zones and add web parts into a custom page layout using SharePoint designer 2013.

    The code below for your reference.

    <%@ Page language="C#"   Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=15.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>
    <%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="PublishingNavigation" Namespace="Microsoft.SharePoint.Publishing.Navigation" Assembly="Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register tagprefix="VisualWebPart1" namespace="SharePointProject96.VisualWebPart1" assembly="SharePointProject96, Version=1.0.0.0, Culture=neutral, PublicKeyToken=eed3bc0f1643672d" %>
    <asp:Content ContentPlaceholderID="PlaceHolderPageTitle" runat="server">
    	<SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
    </asp:Content>
    <asp:Content ContentPlaceholderID="PlaceHolderMain" runat="server">
    	<WebPartPages:SPProxyWebPartManager runat="server" id="spproxywebpartmanager"></WebPartPages:SPProxyWebPartManager>
    	<WebPartPages:WebPartZone id="g_648B520CD3054AFEADA98FE36366831E" runat="server" title="Zone 1">
    		<ZoneTemplate>
    			<VisualWebPart1:VisualWebPart1 runat="server" Description="My Visual Web Part" Title="SharePointProject - VisualWebPart1" ID="g_f47e0a62_d575_40e4_bf2b_32d656fc6ffd" __MarkupType="vsattributemarkup" __WebPartId="{F47E0A62-D575-40E4-BF2B-32D646FC6FFD}" WebPart="true" __designer:IsClosed="false" partorder="2">
    			</VisualWebPart1:VisualWebPart1>
    		</ZoneTemplate>
    	</WebPartPages:WebPartZone>
    </asp:Content>
    

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, August 3, 2018 9:37 AM
    Moderator