Sitecore MVC: Converting Web Forms sublayouts

15

Click here to load reader

description

As Sitecore moves away from web forms and into MVC, we provide you with the information you need to understand the change.

Transcript of Sitecore MVC: Converting Web Forms sublayouts

Page 1: Sitecore MVC: Converting Web Forms sublayouts

Sitecore MVC renderings

Converting Web Forms sublayouts

Prepared by: Jason St-Cyr and Nick Allen

November 2014

Page 2: Sitecore MVC: Converting Web Forms sublayouts

Converting Sitecore Web Forms to MVC

• Sublayouts to Controller Renderings

• The ViewModel

• The View

• Things to watch out for

Resources

Page 3: Sitecore MVC: Converting Web Forms sublayouts

Sublayouts to Controller Rendering

• Create Controller Rendering item (sitecore\Layout\Renderings)

- 1-to-1 Sublayout item to Controller Rendering item

- Many fields are the same (Cache settings, Display Name, Custom Experience buttons)

- Not necessarily 1-to-1 for .cshtml

• Create Controller method

- Examine presentation logic of ASCX and ASCX.cs

- Determine ‘state’ toggles (Page Editor vs. Normal, Empty Collections, No Datasource specified)

- Build business logic to load different views for each state

Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 3

Page 4: Sitecore MVC: Converting Web Forms sublayouts

Sublayouts to Controller Rendering: Controller example

Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 4

public ActionResult Accordion(){

var model = DataSourceItem != null ? new AccordionViewModel(this.DataSourceItem) : null;var viewResult = model != null ? this.View("Accordion/Accordion", model) : this.View("Accordion/Accordion");

if (Sitecore.Context.PageMode.IsPageEditor){

if (model == null){

viewResult = this.View("DatasourceMissing", new DatasourceMissingViewModel());}else if (!model.AccordionContainer.Panes.Any()){

viewResult = this.View("Accordion/AccordionNoPanels", model);}else{

viewResult = this.View("Accordion/AccordionPageEditor", model);}

}

return viewResult;}

Page 5: Sitecore MVC: Converting Web Forms sublayouts

Sublayouts to Controller Rendering: The ViewModel

Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 5

• Create .cs file to represent the object model

• Should contain all the properties needed for rendering

• Should wrap around data object

• Constructor must take an Item (datasource)

• Migrate ASCX and ASCX.cs logic to ViewModel as needed

• e.g. Code for loading configurations, methods to determine Boolean conditions, logic to determine ‘active’ classes

• Ensure Controller is instantiating ViewModel

Page 6: Sitecore MVC: Converting Web Forms sublayouts

Sublayouts to Controller Rendering: ViewModel Example

Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 6

using System.Globalization;using Keystone.SBL.Templates.Components;using Sitecore.Data.Items;

namespace Keystone.MVC.ViewModels.Components{

public class IFrameViewModel{

public IFrame Frame { get; set; }public string Height { get; set; }public string FrameBorder { get; set; }

public IFrameViewModel(Item dataSource){

Frame = new IFrame(dataSource);

Height = !Frame.Height.HasValue ? "500" :Frame.Height.Value.ToString(CultureInfo.InvariantCulture);

FrameBorder = Frame.ShowFrameBorder ? "1" : "0";}

}}

Page 7: Sitecore MVC: Converting Web Forms sublayouts

Sublayouts to Controller Rendering: The View

Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 7

• Create .cshtml file in the “Views” folder

• Website\Views\<path>

• May need multiple (e.g. Accordion shown earlier).

• Group multiple similar views in folders

• Build markup in View

• Use Sitecore HTML Helper methods for outputting fields

• Associate to ViewModel

• Minimize logic in the View, place in ViewModel class

Page 8: Sitecore MVC: Converting Web Forms sublayouts

Sublayouts to Controller Rendering: View Example

Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 8

@using Keystone.SBL.Templates.Components@using Sitecore.Mvc@model Keystone.MVC.ViewModels.Components.PanelViewModel

<div class="panel @Model.StyleCode @Model.Panel.BaseComponent["Additional Styles"]"><div class="panel-heading">

<h3 class="panel-title">@Html.Sitecore().Field(Panel.FieldIds.Title.ToString())</h3></div><div class="panel-body">@Html.Sitecore().Field(Panel.FieldIds.Content.ToString())</div>

</div>

Page 9: Sitecore MVC: Converting Web Forms sublayouts

Things to watch out for

• Placeholder settings

- Need to update to make an MVC component available.

- If building both Web Forms and MVC components in the same build, may have same name in placeholder settings.

- May need separate placeholder settings for MVC vs. Web Forms.

• Insert Options

- Need to have custom insert rules to specify which templates are available to author (MVC or Web Forms).

- This may be driven via path naming convention in the tree, or via a configuration-driven rule.

• Page Templates

- Different Page Templates are required in order to specify an MVC layout and default MVC components.

- Recommendation is to have ‘base’ templates which define all fields, then have presentation templates that specify only the layout portion.

Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 9

Page 10: Sitecore MVC: Converting Web Forms sublayouts

More things to watch out for

• Post-backs. As in: there are none.

- Use forms and actions to send data to the controller.

- Similar to building an HTTP POST integration

- No more server-control events, use Javascript events and AJAX

• Shared renderings

- If you use RenderPartial, needs to be in the Shared folder.

• Visual Studio syntax highlighting in Views

- Need reference to System.Web.MVC to get rid of the red squigglies

Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 10

Page 11: Sitecore MVC: Converting Web Forms sublayouts

Resources

Page 12: Sitecore MVC: Converting Web Forms sublayouts

Sitecore Community Docs

• http://sitecore-community.github.io/docs/documentation/Sitecore%20MVC/index.html

• Contains links to online content submitted by the Sitecore community.

• Key links:

- Anything by Martina Welander

- Creating a Visual Studio Project for Sitecore MVC

- Sample Sitecore MVC project (used in Youtube tutorial videos)

Toronto | Ottawa | New York | Calgary | São Paulo | Florianópolis 12

Page 13: Sitecore MVC: Converting Web Forms sublayouts

MSDN and others

• ASP.NET MVC Overview:

- http://msdn.microsoft.com/en-us/library/dd381412(v=vs.100).aspx

• Getting Started with ASP.NET MVC 5

- http://www.asp.net/mvc/overview/getting-started/introduction/getting-started

• PluralSight ASP.NET MVC 5 Fundamentals

- http://www.pluralsight.com/courses/aspdotnet-mvc5-fundamentals

Page 14: Sitecore MVC: Converting Web Forms sublayouts

Lessons LearnedJason St-Cyr

Solution Architect and Sitecore MVP, nonlinear digital

Background in .NET software development and Application Lifecycle

Management

Contact me: [email protected]

Around the web:

Technical Blog:

http://theagilecoder.wordpress.com

LinkedIn Profile:

http://www.linkedin.com/pub/jason-st-cyr/26/a73/645

Nonlinear Thinking:

http://www.nonlinearcreations.com/Digital/how-we-think

Twitter: @AgileStCyr

Page 15: Sitecore MVC: Converting Web Forms sublayouts

Lessons LearnedNick Allen

Solution Architect and Sitecore MVP, nonlinear digital

Background in .NET software development

Contact me: [email protected]

Around the web:

Technical Blog:

http://sitecorecreative.wordpress.com

LinkedIn Profile:

https://www.linkedin.com/in/nickallen80

Nonlinear Thinking:

http://www.nonlinearcreations.com/Digital/how-we-think

Twitter: @sitecoretweet