How to embed SharePoint 2013 webparts directly in aspx layout page as default webparts for that layout

redfox05 picture redfox05 · Jul 5, 2015 · Viewed 14.6k times · Source

I am using SharePoint 2013, and have a custom master page and several aspx layout pages all of which to be deployed get copied into the "Master pages and page layouts" section within Site Settings.

These aspx pages contain web part zones, but how can I add a web part in the markup of the aspx page, within the web part zone?

What I'm essentially trying to do is embedd "Default" webparts into the layout of the page, so that when this layout is chosen for particular page, it already has webparts added to it.

I'm talking about out of the box webparts, such as Content Editor webparts and Document Library Webparts. In 2013 these are all now known as "Apps".

Any suggestions greatly appreciated.

Answer

Ebbz picture Ebbz · Jul 6, 2015

The easiest way is to make use of designer to create the code for you...

  • Create a page and open it advanced mode in SharePoint Designer.
  • Add the required webpart into the webpart zone. This will create the required html.
  • Copy it and remove the g_ from the ID value.
  • Use this code in your page layout.

Ps: Id the WebPart button is disabled in the SharePoint designer ribbon, just save the page. This will enable it.

The code that is generated by designer when I added Content Editor Webpart and Script Editor webpart is given below.

<WebPartPages:ContentEditorWebPart webpart="true" runat="server" __WebPartId="{BA190D08-907A-4F94-B8F0-C3966A61E601}">
    <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
        <Title>$Resources:core,ContentEditorWebPartTitle;</Title>
        <Description>$Resources:core,ContentEditorWebPartDescription;</Description>
        <PartImageLarge>/_layouts/15/images/mscontl.gif</PartImageLarge>
        <PartOrder>4</PartOrder>
        <ID>ba190d08_907a_4f94_b8f0_c3966a61e601</ID>
    </WebPart>
</WebPartPages:ContentEditorWebPart>

<WebPartPages:ScriptEditorWebPart runat="server" ChromeType="None" Description="$Resources:core,ScriptEditorWebPartDescription;" 
    ImportErrorMessage="$Resources:core,ImportErrorMessage;" Title="$Resources:core,ScriptEditorWebPartTitle;" 
    __MarkupType="vsattributemarkup" __WebPartId="{1861cebe-9134-4645-8500-13ce8817d416}" WebPart="true" 
    __designer:IsClosed="false" partorder="6" id="1861cebe_9134_4645_8500_13ce8817d416">
</WebPartPages:ScriptEditorWebPart>