Skip Ribbon Commands
Skip to main content

Dynamic Page Layouts in SharePoint 2013 - Part 1

Giving content managers ultimate flexibility in creating pages on SharePoint 2013

James Strugnell

21/07/2016

​​​​​​​​​​​​​Introduction

This is going to be a series of articles (not sure how many yet) describing a solution we recently built for a client. The solution was built for SharePoint 2013 and is a full farm solution, i.e. no Office 365 here I'm afraid.

A couple of years ago we started a project for a long-term client which had the goals of building an all-new web platform on a hosted SharePoint 2013 environment. Their existing website was built on SharePoint 2007 and was starting to show its age, not least in the fact that support for that version was running out.

SharePoint 2013, at the time, was fairly new to the market and had shipped with a number of new features targeting the public website market. Our client was looking for a self-managing platform on which they could build any number of "microsites" in addition to hosting their main site. These sites needed to be highly customisable, themeable and include a large suite of web parts that could be configured to work across browsers and devices.

I'm happy to report that we delivered on the above and the client has been very pleased with the capabilities they now have. 

Nee​​d

There was one aspect of our solution that I was never completely satisfied with, and that was the static nature of the supposed "highly configurable" Page Layouts we had provided. Specifically, we had provided a "Landing Page" layout which was intended to be used for home pages and provide fairly unlimited options in terms of layout configuration.

The client wanted to be able to use the Landing Page in any number of ways, and basically not be restricted. We had used Bootstrap 3 and thus were able to use that grid system to set up a number of rows with differing numbers of columns. While this met the needs initially we soon started to hit issues with this approach.

Limitations of SharePoint Page Layouts

While web parts can be added any number of times to web part zones, content managers don't have the ability to easily manage the web part zones themselves (unless they crack open SharePoint Designer, and that's not going to happen on my watch). So we found that we were continuously adding new rows of new or repeated column configurations so that designers and authors could achieve their visual requirements.

This unfortunately meant that, when in "edit mode", the page was full of empty web part zones with only relatively few of them actually being used.

Eventually we hit a limit in the number of web part zones (or it may have been web controls of any kind) allowed on a page. I forget the number but I think by that point we'd topped one hundred.

A short-term workaround for that was to move the web part zones into a "User Control" and then just include a reference to that User Control in the page layout. Not nice, but it worked, and theoretically shouldn't cause any performance issues.

What if?

From the outset of this project I had tried to think of a good way to solve this problem. Giving the content authors complete control of how the page and its components were laid out, with no waste, and only the exact structure they needed. How could this be done?

What if we could code up an interface that would enable content managers to add and edit rows of Web Part Z​ones. What if they could choose the number and layout of those web part zones for each row? What if they could specify the width of that row, so that it could fill the width of the window or just the content area? What if they could specify background colours, images, padding and margin for each row?

Could that be done? Spoiler warning... Yes!

DWPZ_Grid.png

Proof of Concept

In order to achieve all of the above (and potentially more) I went about proving some concepts, to determine if it would be possible.

I've been developing with ASP.Net and SharePoint for a long time now and am familiar with the page model in that anything that you can add to a page in ASP.Net markup at design-time, i.e. web controls, can generally also be added dynamically, using C#, at run-time. But would this be the case with Web Part Zones? After all, they need to be in place and on the page early enough in the page lifecycle for SharePoint to hook into and inject any web parts they contain. They also need to have unique, consistent identifiers so that SharePoint knows which zones the web parts belong to.

Proof of Concept 1 - Can Web Part Zones be added dynamically to a page

So the first challenge was determining if Web Part Zones can be added dynamically to a page. By dynamically I mean using C# code and adding an indefinite number of web part zones.

To establish this, I created an empty Page Layout, and then created a custom web control called DynamicWebPartZoneRenderer.cs. I added the control to the Page Layout and deployed to a local dev SharePoint site. Not exactly step-by-step instructions but this isn't a beginner's guide I'm afraid…

Within that control I then added some code that looped from 1 to 10 and added a new Web Part Zone with no properties configured.​

Here's the code:

 

​​ ...and here's the resulting error:

DWPZ1.png
 

OK, so it looks like Web Part Zones require an ID to be set. That was a simple fix:​

 

​Progress. The page rendered out 10 sequential Web Part Zones. First concept proved, you can add Web Part Zones dynamically to a page.

Proof of Concept 2 – Can Web Parts be added consistently to dynamically added Web Part Zones?

Next up I needed to get Web Parts added in to these dynamic zones At this stage if I tried to add a web part to one of these zones nothing really happened. I suspected the problem was because the zones did not have persistent IDs. The way I'd written the code they were being assigned new random GUIDs on each load.

So I modified that code slightly again: ​

 

This worked. It rendered 10 Web Part Zones and I could add web parts in to those zones, and they would persist through post backs and page saves.

Concept 2 proved!

Proof of Concept 3 – How we can add "Rows" of Web Part zones to the page?

So at this point I knew I could add Web Part Zones to a page, and that they can be used. There's still a long way to go but I hoped that was the major unknown dealt with.

Next up was how to go about adding rows of differing numbers of Web Part Zones. I envisaged and end-goal UI that allowed content managers to specify exactly the configuration they wanted, using a drag and drop approach, or fancy wizard. But achieving that seemed a long way off at this stage. Instead, I took the approach of setting up several user controls which each had a row configuration of Web Part Zones. For example,

  • 1 * Full width
  • 2 * Half width
  • 3 * One-third
  • 4 * One-quarter
  • Etc.

Each user control would be a Bootstrap "row" with the required number of Web Part Zones. The idea being that we (or a content editor) can then add the User Control on to the page which contains the required column configuration. This could eventually also allow for more complex Row configurations, perhaps with nested content.

So we now need to be able to dynamically add User Controls to the page, that contain Web Part Zones. Will this work?

I created a folder in my solution and dropped in a few sample User Controls with Web Part Zones inside. I then wrote some code in the WebPartZoneRenderer that would render each of those User Controls out on to the page. I realise all of this code is hard coded at this stage. The end goal is that the code will read from some page-level configuration data and render the Zones previously configured by a content manager.

The code looked something like this:

​And that worked. Kind of. At this stage I had only used each User Control once. If I tried to add the same User Control to the page twice, it blew up.  We'll solve that problem in the next post...

Still to come…

That's enough for one article. As we complete this series we'll be looking cover the following:

  • How to load multiple User Controls containing the same row configurations, but ensuring each Web Part Zone is instantiated with a persistent, unique ID
  • How to persist the page level configuration data
  • How to read and write to the configuration data as part of the page save and load lifecycle.
  • How to enable an interface for content managers to add, edit, delete and manage rows in a page.
  • Adding additional features to each row (think custom padding, margins, background colours, borders, fonts etc.
  • Going beyond simple Web Part Zone configurations… ​

I'll try to continue this series next week. Thanks for reading.


 


 


 About us

Fuse Collaboration Services is a Cloud Solution Provider and Microsoft Gold Partner specialising in delivering SharePoint, Skype for Business, and Azure cloud-based solutions. Based in Northampton, UK.

Microsoft Gold Partner Logo showing 5 competencies

Read more

 Latest Tweets

 Latest Blog

 

 

Skype for Business and so much more…195<p class="lead"> Yes? Then this is an important read!</p><p>We all know (hopefully!) <a href="/_layouts/15/FIXUPREDIRECT.ASPX?WebId=4fc45909-2b6d-48b9-bcf9-a446e9d472d6&amp;TermSetId=c98895cd-d37f-4406-9cff-5480b4f829b6&amp;TermId=4cd8262c-9c67-4bcb-8f13-65e8ffa6299a">what Skype for Business is</a> but what is different about the version we can offer? </p><p>Fuse has been working ​with the UK's leading Skype for Business provider since they began operating four years ago. Working alongside our partners you will receive the usual unified communications inherent in Skype for Business but further to this you will have access to call centre solutions, call monitoring, call analytics and 24-hour support. The support is available globally and frees up IT staff allowing them to increase their productivity and availability. </p><p>Further to the above features you will have access to an online portal which will allow you to quickly commission and decommission users, configure/reconfigure call groups, manage users and telephone numbers, configure interactive voice response(IVR), call routing, on hold marketing and call reporting.</p><p>Working with the solution we offer, you will be given the benefit of not having a long-term contract tying you in to a lengthy subscription. You can down scale or upscale monthly, giving you the control of your budgets. The flexible scale up/down in line with organisational changes is an important bonus to many customers. </p><p>The monthly price works out at half the price of competitors with call bundles and next to no downtime. </p><p> <strong>The benefits are&#58;</strong></p><ul><li>Reduced annual costs as one monthly cost includes unlimited free outbound calls to UK and line telephone numbers. International call packages are available. Free calls to UK mobile numbers available as an add on</li><li>Flexible working - inbound and outbound business calls and online meetings from anywhere with an internet connection</li><li>Encrypted and secure communication<br></li><li>End to End Fully Managed Service</li><li>24 hr, 7 days a week dedicated support; online support and contact centre</li><li>Online Visual Management and Analytics Portal</li><li>Global solution, from reslient datacenters with multiple providers</li><li>Audio and video conference facility for up to 250 participants – even if they're not on Skype for Business. All they need is a phone or internet connection.</li><li>Fixed monthly fee per user that is payable on a monthly subscription</li><li>Integration with Outlook/SharePoint to show availability/presence (on premise or O365)</li><li>Increases productivity - Collaboration between teams is simplified due to the ability to share files etc.</li><li>Instant messaging with dancing emojis!</li></ul><hr />​​ <div class="well well-lg"><p class="lead">​To book a demo contact Fuse Collaboration on 01604 797979 or email fuse@fusecollaboration.com </p></div>​​ <p>Fuse Collaboration is a Microsoft accredited Gold Partner, License and Systems Specialist</p>​ ​l.ozier@fusecollaboration.com | Louise Ozier | 693A30232E777C6675736563735C6C2E6F7A696572 i:0#.w|fusecs\l.ozier26/06/2017 23:00:002017-06-26T23:00:00ZDoes your business make lots of phone calls?29/06/2017 15:18:4669https://www.fusecollaboration.com/ourblog/Pages/Forms/AllItems.aspxhtmlFalseaspx

 Contact us

Our address
12-14 Brookfield, Duncan Close
Moulton Park, Northampton
NN3 6WL
P: +44(0)1604 797979
Contact Us