The power of JSOM

I’ve been a SharePoint developer for several years now and over that time have dabbled with the REST API and JavaScript Object Model (JSOM) but a lot of the functionality I have produced has been created using server side code.

The power of JSOM

Recently whilst working on an Office 365 SharePoint solution we had a requirement to produce a central hub where users could create pages of various content types, populate fields, pick webparts, pick a location for the new page and view created pages using search​​​.

​Initially I thought this would have to be a new page in the provider hosted app we had created for provisioning the site using the Client Side Object Model (CSOM), but I knew JSOM was supposed to have the equivalent CSOM functionality so decided to try it out using this.​

Get all subsites

The first thing we want to do is give the user the ability to choose a site to create their page in. To do this we can use SP.Web.getSubwebsForCurrentUser​which gives us all webs the user has access to. Calling this in a recursive function will allow us to get a tree of sites the user can place their page in.​​

​​​​​​​​​​

Get the publishing web

Once the user has chosen a site, we can get the context of that publishing site using SP.Publishing.PublishingWeb.getPublishingWeb. The below is an example of how you would do this:

publishing-web

 Getting page layout and creating the page

With any context we can get the site and therefore the root web. Once we have the root web we know where to get our page layouts (rootWeb + “/_catalogs/masterpage/”) and web parts (rootWeb + “/_catalogs/wp/”) from. With the layout and the title of the page the user has selected, we can create the page using SP.Publishing.PublishingWeb.addPublishingPage

page-layout

 Updating the page informa​tion & adding web parts

Once our page is created we can get the associated list item using newPage.get_listItem() and then we can populate any available fields, including taxonomy fields, using set_item. If you need to allow users to pick taxonomy field values there are several blog posts available online that cover this topic.​

For adding the web parts to the page we can make use of ​SP.File.getLimitedWebPartManager which will give us the ability to access web parts associated with the page. With the web part manager and the XML of the web part we have access to fr​om the root web (as mentioned earlier) we can now add web parts to the page using addWebPart:

adding-web-parts

Putting this all together gives us the ability to create our page that meets the requirements mentioned earlier where users can create content, all powered by JSOM.

Once the page is created, fields updated and web parts added we can navigate the user to the page in edit mode for any final tweaks before check-in and publish. Once the page is published it can be crawled and we ​can extend our central page by also providing the users with any pages they’ve created in a rollup. How can we do this? Search using JSOM​​​ of course.

So there we have it. A custom central content creation & content viewing page in Office 365 developed using JSOM. Office 365 development is very easy when you have access to the same server side methods on the client side.​​​

For more information on our SharePoint Development services, take a look here. Have existing SharePoint development requirements? Get in touch with us today.

Talk To Us

Adam Wildash Senior Developer

Adam describes his work as “turning customer’s requirements into a functional reality” which sums it up pretty well. He could list out all the technologies he works with to create custom solutions, like SharePoint, .Net, Azure, & SQL, but that’s not the point. Adam leads development teams, liaises with his clients and runs demos of the solution and it’s all for one purpose – to deliver a ‘functional reality’ based on the requirements he’s been given. Says it all, really. Adam is from a consultancy background and got a first in his BSc in Computer Systems, Networking and Telecommunications, so when everything feels a little too easy, he loves the challenge of finding his way around a problem!

Adam devotes his spare time to spending as much time with his little ones as possible.

Twitter Feed

Where Next? Relevant Stories and Insights.

Part 2​ - Securely Publishing SharePoint Externally Using Web Application Proxy ​(WAP)
Part 2​ - Securely Publishing SharePoint Externally Using Web Application Proxy ​(WAP)
In this post we will take a look at the end-to-end configuration process of WAP using pre-authentication for Windows claims-based SharePoint web applications.
Part 1​ - Securely Publishing SharePoint Externally Using Web Application Proxy ​(WAP)
Part 1​ - Securely Publishing SharePoint Externally Using Web Application Proxy ​(WAP)
​Hopefully this post gives you an overview of the new capabilities of the Web Application Proxy (WAP) feature in Windows Server 2013 R2 and how it can be utilized by your SharePoint 2013 environments to securely publish your sites externally.
What's New for Developers in 2015?
What's New for Developers in 2015?
The beginning of a new year signals the start of a new release cycle for software vendors. So I thought I'd give my personal view of what I'm looking forward to and hoping to see this year. This is all public information, but I admit some is guess work and rumor.