Some thoughts on programming stuff

Tag: rest api

Get current page layout with JavaScript in SharePoint

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
Continue reading

Get web URL from document URL in SharePoint

Maybe you need to get web URL from document URL in SharePoint, or even just grab the Site Collection URL and do some awesome logic and update some data in your site. But you are aware that it can be costy and require some array splits, substrings and those kind of tricks. So here is a code snippet to save your time to enjoy a good cup of coffee with your colleagues!

The document URL usually is composed by a site collection part, the specific web part, the Library name and then the final part containing the folders and the file name. The script basically will perform a request to a SharePoint API. Our parameter is the folder or library URL (cutting the filename) that will be made automatically. Finally, the result will come with the Web and Site Collection urls for that context.

Get web URL from document URL in SharePoint - A bunch of files in folders inside drawers (SharePoint)
A bunch of files in folders inside drawers (SharePoint) (Source: Howtogeek)
Continue reading

© 2024 wiliammbr's blog

Theme by Anders NorenUp ↑