3

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
  • 3,354
  • 1
  • 34
  • 39

1 Answers1

8

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>
Ebbz
  • 193
  • 4
  • 1
    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 Jul 06 '15 at 18:27
  • 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 Jul 06 '15 at 21:18
  • 1
    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 Jul 06 '15 at 21:19
  • Ah it does mess up my margins for some reason. all text now flush with left side :( – redfox05 Jul 06 '15 at 21:46
  • 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 Jul 07 '15 at 01:42
  • I am not sure about the Margins... might need to look at the CSS to make sure... Never seen that issue before... – Ebbz Jul 07 '15 at 01:43
  • 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. – redfox05 Jul 15 '15 at 10:25