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

14,976

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>
Share:
14,976
redfox05
Author by

redfox05

Updated on June 05, 2022

Comments

  • redfox05
    redfox05 almost 2 years

    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.

  • redfox05
    redfox05 almost 9 years
    Thanks for the answer. Tricky that it needs SharePoint Designer, as I am working from an Apple Mac (yes...) but I can use Virtual Box and go into my Windows VM. I'll try that tonight thanks.
  • redfox05
    redfox05 almost 9 years
    Yep, just tried it, took a little longer to find the ID field on the Document Library webpart, but success! It works; when I apply the Page Layout to a new page, the document library webpart is present, and shows the correct library. Now to double check each page can have different library "Views". Thanks. Setting as Accepted answer and +1.
  • redfox05
    redfox05 almost 9 years
    Strange that g_ is removed from the ID and then thats it. @Ebbz, do you know what that does? Just for background info/learning.
  • redfox05
    redfox05 almost 9 years
    Ah it does mess up my margins for some reason. all text now flush with left side :(
  • Ebbz
    Ebbz almost 9 years
    the id with g_ is usually generated by the SharePoint Designer... and at times this causes problems since SharePoint thinks this webpart is added by Designer (even though it is inside a page layout).
  • Ebbz
    Ebbz almost 9 years
    I am not sure about the Margins... might need to look at the CSS to make sure... Never seen that issue before...
  • redfox05
    redfox05 almost 9 years
    FYI, I screwed up my pages now I think. With the flicking back between uploading aspx files and editing them via SharePoint Designer. Just a note for those following my issue to be aware. I have chosen to move away from trying to embed the webparts now. It seems to not be working well for me. Thanks for the help all.