Skip Ribbon Commands
Skip to main content

Dynamic Page Layouts in SharePoint 2013 - Part 4

Rendering the Dynamic Page

James Strugnell

02/11/2016

​This is the 4th (and likely final) part of our series on how we built a dynamic page template in SharePoint 2013, enabl​ing content managers to configure a custom grid layout within the page. You can find the previous articles in the series here:

In this article we're going to cover the code which consumes the current page level configuration and renders out the user-configured rows, including the web part zones, which comprise the page.​

​​Custom Page Layout and Web Control

Firstly, we created a custom Page Layout called "Dynamic Page Template". We then added to that page a custom ASP.Net web control, DynamicWebPartZoneRenderer.cs. The entire page template can be viewed ​here:

https://github.com/jstrugnell/SharePointDynamicPageLayouts/blob/master/DynamicPageLayouts/Common/Branding/PageLayouts/dynamic.aspx

The custom web control can be viewed here:

https://github.com/jstrugnell/SharePointDynamicPageLayouts/blob/master/DynamicPageLayouts/Common/WebControls/DynamicWebPartZoneRenderer.cs

Loading the Current Page Configuration

As established in the previous articles, the page configuration data is stored as a JSON string in a SharePoint field for the current page. For a quick recap, the first action performed by the DynamicWebPartZoneRenderer control is to pull the configuration JSON data out of the page field, and de-serialize into a C# object, of type "PageWebPartZoneConfiguration". This class is essentially a collection of "Row Configurations" each of which represents s single row as configured by the page editor. Each Row has the following available settings (we actually implemented more in the real solution):

  • Id – This is a unique ID (GUID)
  • Order – This number enforces the sequence in which the rows are added to the page
  • BackgroundColour – This is used as the background colour of the entire row
  • MarginTop – This is the gap that should be injected above the current row
  • MarginBottom – This is the gap that should be injected below the current row
  • PaddingTop – This is the gap that should be injected between the content and the top of the row
  • PaddingBottom – This is the gap that should be injected between the content and the bottom of the row
  • Layout – Perhaps the most important setting, this field represents the layout for the row. We have defined a number of pre-defined layouts that content editors can choose from. Each layout is implemented as a "User Control", made up of one or more Web Part Zones, and is loaded dynamically on to the page.

So once we have the configuration data de-serialized, we can then loop over each row and dynamically render the User Control associated with the "Layout" property. Some of the configuration for each Layout is hard coded in the HTML, for example the number of columns that exist. Other aspects, such as the margin, padding and background colour, need to be set at run-time. This is done in the Base Control from which each Layout User Control inherits - BaseWebPartZoneLayout.

BaseWebPartZoneLayout

Each Row Layout User Control inherits from a single base user control called "BaseWebPartZoneLayout". Each User Control is implemented consistently with known conventions. This allows the Base Control to manipulate the layout elements according to the configuration set for the current Row. For example, every Row User Control has an ASP.Net Panel with an ID of pnlRow. We then reference that Panel from the Base Control and configure its background colour, margin and padding according to the applied configuration.

We also implement the "Edit" button from this Base Control. We detect if the page is currently in Edit mode and then render out a link for each Row which enables the logged-in user to open up and manage the settings for each row.

Configuring the Web Part Zones

If you recall from the first article in this series (all those months ago...), the first hurdle we had to overcome was whether we could dynamically add Web Part Zones to a page. We now know that this is indeed possible but we never fully explained quite how. The big issue was that every Web Part Zone needs to have a unique ID. It also needs to maintain that ID through page postbacks and reloads. Each Web Part Zone also needed to maintain the same ID even if its parent Row was repositioned within the page. So how did we ensure all of this?

Each Web Part Zone is statically added in to its parent User Control. They are purposely not assigned an ID at this stage as they need to be unique to the whole page. So how do we assign these IDs uniquely and consistently?

After the Layout Control has been loaded and initialised (e.g. Colour, Padding, Margins assigned) we then pass an instance of the User Control (via a handle on the Base Control) into a method called "ConfigureChildWebPartZones". This method also receives the "ID" (GUID) for the current Row.

We then loop over each "Control" inside the User Control recursively hunting for any Web Part Zones. When we find one, we then set its ID to a concatenated string comprising of the literal string "dynamicZone_" plus the row ID, plus the index of the Web Part Zone (i.e. if it's the first Web Part Zone we hit, this will be number 1).

Once this process has finished every Web Part Zone is assigned a unique ID, and one that persists even when the Row is moved around the page. If the Content Author changes the designated Layout configuration then any Web Parts within will potentially move. This is not a big problem however, and can be resolved by the editing user.

Please review the code on Github for a complete picture of the process outlined above.

Conclusion

That brings us to the end of this series. Hopefully it has served to present a number of useful techniques that have been brought together to build a much-used feature on our custom web platform. The customisable row settings detailed in this series are really just a starting point. Any number of other possible settings could be applied further enhancing the options and opportunities to page editors.

We have placed a cut down working version of this solution on Github which can be found here: https://github.com/jstrugnell/SharePointDynamicPageLayouts

If you have any questions or suggestions for other enhancements or improvements please get in touch.

Thanks.​





 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

 

 

Better Mobile 4G and the Portable Office3089<p class="lead">​​<img class="img-responsive float-right" src="/ourblog/Blog%20Site%20Images/OneDrive-for-Business-App.png" alt="OneDrive for Business App" style="max-width&#58;300px;margin&#58;5px;" />Transport for London’s (TfL) plans to roll out 4G across the London Undergound network are continuing apace, with the <a href="http&#58;//www.cityam.com/287267/tfl-moves-step-closer-offering-4g-mobile-coverage-tube-2019" target="_blank">recent news</a> that this will likely include the new Elizabeth Line (aka CrossRail) and the planned C​rossRail 2 and Northern Line extension projects.</p><p>Suppliers and interested parties have had until July 6th to submit a selection questionnaire, which will then be evaluated before a more thorough tender goes out in early August.</p><p>City AM reports that companies will be competing to construct a commercial fibre optic network and the provision of a public WiFi service in specified TfL stations. The plan Is for the first stations to get connected during 2019, taking the capital one step closer to being a fully mobile working environment.​</p><p>WiFi already exists in some Tube stations, as it does on large parts of the train network across the country – even if the actual internet speeds vary widely depending on whereabouts you are!</p><p>Both TfL and the train companies will be heavily promoting the virtues of this type of connectivity for both leisure and work use, and it is popular here in the East Midlands, where we have a mixture of commuters heading into either London or Birmingham and companies who have to travel to see their clients.</p><p>But in the same way as nothing can beat the quality of a film that is already downloaded to your device, you need to make sure you have the processes and protocols in place for your mobile working to make sure you do not lose​ work unnecessarily. After all, there is nothing more frustrating than concentrating hard for an hour, only to see your work disappear because the saving process has not worked properly!</p><div class="well well-lg bg-color4 lead"><p>Here are Fuse’s top tips for your mobile working&#58;</p><ol><li>Make sure you have OneDrive syncing in place, so that when you save your document to your device it will automatically update onto your online OneDrive folder as soon as you are back online, thereby ensuring that your company work remains up-to-date<br></li><li>Do not get too reliant on having internet connectivity when you are on the move. Access to online resources may not be available for the duration of your whole journey, and even if your device is connected to the on-train WiFi the actual internet speeds will vary depending on whereabouts you are, or if you are in a tunnel<br></li><li>Have processes and checklists in place – even if they are just your own individual way of working, not official company policy – to ensure that you always download what you need onto your machine before you travel<br></li></ol></div>​ <p class="lead">Here at Fuse we believe that having the wherewithal to work effectively on the move is an integral part of the modern office. This includes OneDrive syncing being set up on all your machines, working with you to develop robust working practices for all your staff, and proactively making sure that your IT provision works to meet your needs, not the other way around.</p><div class="well well-lg"><p class="lead">Get in touch to find out how we can make sure that you have the great IT provision your organisation deserves! Call 01604 797979 or <a href="/_layouts/15/FIXUPREDIRECT.ASPX?WebId=4fc45909-2b6d-48b9-bcf9-a446e9d472d6&amp;TermSetId=c98895cd-d37f-4406-9cff-5480b4f829b6&amp;TermId=218eb0be-10f6-490a-82a7-a7fd47c8de90">send us an enquiry​​​</a></p>​ </div> | Chris Wearmouth | 693A30232E777C6675736563735C632E776561726D6F757468 i:0#.w|fusecs\c.wearmouth18/07/2018 23:00:002018-07-18T23:00:00ZAs better 4G rolls out across the nation how are you going to make your office truly mobile?As better 4G rolls out across the nation how are you going to make your office truly mobile?18/10/2018 00:15:466342htmlFalseaspx

 Contact us

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