Tuesday 17 June 2014

SharePoint 2013/365: Get current page properties/metadata with REST

Scenario

Retrieve the current page layout name via the page metadata on Office 365, this approach must be maintainable, flexible and not have any extra costs


Investigation

There are a number of ways to get the current page metadata with full trust code, simply using the SPContext.Current.ListItem allows you to get the collection, of course that wont work going forward with O365.

The most flexible way of working with any form of SharePoint object in O365 is to use JavaScript, thankfully there are a number of ways that can be done:

  • Use the JSOM - there are a number of blog posts currently on how to return the current items with JavaScript
  • Use Search - mapping properties and matching the search result by url is also a possibility, this is fast and allows easy optimisation
  • Use REST - there isn't much on this approach out there, it looks like a pretty obvious choice so i decided to try this approach.

Solution

The architectural approach is as such:
On Page Load

  1. Using the current context object data form a rest query
  2. Query the ListItem REST endpoint
  3. Get the page fields out of the returned data

Here is the finished codeblock:

No comments:

Post a Comment