none
In Sp2013, after creating a page, how dynamically add all 5 custom webparts to the newly created page in Edit mode. RRS feed

  • Question

  • In Sp2013, after creating a page, how dynamically add all  5 custom webparts to the newly created page in Edit mode.

    As per the scenario,

    I have custom SharePoint server code in visual studio. Implemented the code in such away that after add the SharePoint page with custom layout,  page added opened in Edit mode.

    After either Refresh or save the page then only Custom web parts are coming in the created page. 

    As per the client scenario, it should add in edit mode with out refresh or save the button in webpart page.

    I wrote the code in Edit mode of the web part page and called it in onload()function.

    Let me know how my custom web parts add to my page (in edit mode itself) immediately after click on create page.

    Please guide me if any script or code to add the webparts dynamically after immediate page creation.

    Wednesday, August 1, 2018 3:36 PM

All replies

  • Hi,

    Did you add the custom web parts in the custom layout? If not, you can add the custom web parts in web part zone in page layout.

    http://www.benprins.net/2018/01/19/sharepoint-add-a-web-part-to-a-page-layout/

    If you want to add the web parts into a page using JavaScript code, we can use JSOM code to achieve it, the following article for your reference.

    https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/hh185010(v%3Doffice.14)

    Or you can provide more information about your requirement and some code for further research.

    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.

    Thursday, August 2, 2018 8:25 AM
    Moderator
  • Hi, Here I am sharing my code.

                                                            

    I called the below method in On load method. 

     protected override void OnLoad(EventArgs e)
    {

    //Lot of code

    //end calling the below method

    AddWebParts();

    }

    private static void AddWebParts()
            {

                SPContext.Current.Web.AllowUnsafeUpdates = true;
                var layout = SPContext.Current.ListItem != null ? (PublishingPage.IsPublishingPage(SPContext.Current.ListItem) ? PublishingPage.GetPublishingPage(SPContext.Current.ListItem) : null) : null;

                PublishingPage currentPage = PublishingPage.GetPublishingPage(SPContext.Current.ListItem);
                SPWeb oWeb = SPContext.Current.Web;

                if (OOCContext.Current.PortletID == Portlets.Portlet20)
                {
                    if (layout.Layout.Name.IndexOf("LayoutLetNav.aspx") != -1 || layout.Layout.Name.IndexOf("LayoutSinglePageLayout.aspx") != -1)//.Equals(Constants.PageLayouts.VisualLongPageLayout.Equals("LayoutLongPageLayout.aspx")))
                    {
                        Dictionary<Microsoft.SharePoint.WebPartPages.WebPart, string> wps = new Dictionary<Microsoft.SharePoint.WebPartPages.WebPart, string>();

                        ABCWP saWP = new ABCWP();
                        saWP.Title = "ABCBar";
                        saWP.ToolTip = "ABC Bar";

                        BannerWP bannerWP = new BannerWP();
                        bannerWP.Title = "Banner";
                        bannerWP.ToolTip = "Common Banner";

                       DEFWP lpcWP = new DEFWP();
                        lpcWP.Title = "DEFContent";
                        lpcWP.ToolTip = "DEF Content";

                        ShowPageMetadataWP spmWP = new ShowPageMetadataWP();
                        spmWP.Title = "ShowPageMetadata";
                        spmWP.ToolTip = "Show Page Metadata";

                        wps.Add(saWP, "TopZone");
                        wps.Add(bannerWP, "fullzone");
                        wps.Add(lpcWP, "BusinessContentZone");
                        wps.Add(spmWP, "BottomZone");
                        int i = 0;

                        foreach (KeyValuePair<Microsoft.SharePoint.WebPartPages.WebPart, string> kvPair in wps)
                        {
                            //TopZone,BusinessContentZone,BottomZone
                            var webpartToBeAdded = new ProvisionWebPart(kvPair.Key, kvPair.Value, i);
                            if (webpartToBeAdded != null)
                            {

                                if (webpartToBeAdded.WebPart != null)
                                {
                                    using (var limitedWebPartManager = oWeb.GetLimitedWebPartManager(currentPage.Url, PersonalizationScope.Shared))
                                    {
                                        string _webPartToolTip = webpartToBeAdded.WebPart.ToolTip;

                                        ////Retrive the webpart with some title
                                        IList<Microsoft.SharePoint.WebPartPages.WebPart> _listFormWebParts = (from _wp in limitedWebPartManager.WebParts.Cast<Microsoft.SharePoint.WebPartPages.WebPart>()
                                                                                                              where (string.Compare(_wp.ToolTip, _webPartToolTip, true) == 0)
                                                                                                              select _wp).ToList();
                                        ////Check if there are any web parts found
                                        if (_listFormWebParts.Count == 0)
                                        {
                                            //limitedWebPartManager.AddWebPart(webpartToBeAdded.WebPart, webpartToBeAdded.ZoneID, webpartToBeAdded.ZoneIndex);
                                            limitedWebPartManager.AddWebPart(webpartToBeAdded.WebPart, kvPair.Value, i);
                                            limitedWebPartManager.SaveChanges(webpartToBeAdded.WebPart);
                                            currentPage.Update();
                                        }
                                    }
                                }
                            }
                            i++;
                        }
                    }

                  }
                SPContext.Current.Web.AllowUnsafeUpdates = false;

                }

    Pls Help.


    Thursday, August 2, 2018 9:05 AM
  • Hi,

    Where the code did you add? An application page code behind in SharePoint solution in visual studio?

    Please provide more details about how to reproduce this issue, others would get easier to research on it.

    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.

    Thursday, August 2, 2018 9:21 AM
    Moderator
  • I have a Visual Studio where We have many User controls.

    I wrote this code in one of the User Control code behind file.If any page is added in share point automatically we called this code on onLoad method to add dynamically adding 4 web parts under Addebparts().

     public partial class CommonFieldsEditor : UserControl
        {
            private string _currentFlag = string.Empty;
            private DateTime _currentFlagExpireDate = DateTime.MinValue;

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

                               if (SPContext.Current.FormContext.FormMode == SPControlMode.Edit)

                    {

    // lot of code for page validation

    Addwebparts();

                   }

    Only able to see the webparts in zones after if we refresh or Save the page. As per requirement SHow it immediately after adding new share point page that to in Edit Mode of the current page.


    Or Please provide simple code, how to add the custom webpart to our Layout zone in edit mode of page creation?

    Thursday, August 2, 2018 10:18 AM
  • 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