Today I’m bringing an useful code snippet to get current page layout with JavaScript in SharePoint. It helped me on finding out which Page Layout I was running my JavaScript code. Here you can find a pure vanilla JavaScript example that builds the URL for the SharePoint REST API. First it requests only the PublishingPageLayout field of the current page. Then it logs the information in the console, but I suggest you to just do that for testing purposes.

Page layouts are used in SharePoint to customize and define where the content will be displayed in your Web Page. Essentially, it’s a template for your Web Page that helps you on building a better experience. It benefits those who access your content and to help the content creators when they need to create a new page for you Site or Intranet.

Page layouts must be attached to a Content Type, so you can reference site columns and fields inside the template to let SharePoint save and present the data in the page library just like any document library you had been working with. Also it let’s you define different ways to present the fields, for example the Title or the Page Content, when in the Display Mode and Edit Mode.

This script can be particularly useful when you need to accomplish some specific business logic that requires to run a JavaScript function in an specific layout and you can’t or don’t want to make changes to the page layouts directly. I say that because, due the fact that Page Layouts are just like templates, you can add Script tags directly to the Page Layouts and call only the JavaScript files you /want when a user selects that Page Layout during the content creation process.

Image result for sharepoint page layout
A bunch of Page Layouts – dotnetcurry

The JavaScript to get current page layout

Unfortunatelly I didn’t find this example anywhere in the internet to accelerate the development of what I need, so I hope it can help you someday on saving time when needing to do some “hacks” inside a page.

Furthermore, you can combine and prepare some A/B testing getting user information in the current page and check how specific users behave in a page layout with the change experiences you want to the test.

Anyway, I hope this blog post can help you when you need to get current page layout with JavaScript in SharePoint.

References:
Microsoft Docs