You are on page 1of 38

Copy all content from one language to another in sitecore

Copy all content of the sitecore tree from one language to another

If you want to migrate all the content of the siteore from existing language to new language then below
solution is fit for you.

Problem statement: my client wanted to migrate the complete site from /fr-fr to /fr so I had to migrate all
language version of fr-fr into new language version called fr. We migrated more than 1 million items
from FR-FR to fr with below solution:

Solution:
There are three major tasks to migrate this language

1. Export the language


2. Change the language in file
3. Import the language

Export language

Step1: Create new language in the system called FR only.

Step2. Go to control panel > globalization>

Step3. Click on export language to a file


Step4. Select the language that needs to be migrated ex. Fr-fr

Step5. Select the content tree that needs to be migrated. In my case I selected the content folder as I
need to migrate the complete tree, but we can select any folder or item.
Step6. Enter the file name.

Step7 . Download the file


Step8: Open the file in notepad ++ or any editor and replace fr-fr with fr

Step 9 : Same as replace </fr-fr> with </fr> and save the file
Step 10: Import the saved file:

Step11: Browse the file


Step12: select the new language(import)
Step13 : Finished the import dialog
Step14 : Now new language version has been created with all field value of fr-fr > go to item > language
version>

If you have any comments or suggestions, please use the comments below, thanks.
Solr Index rebuild for single Item (Document) in Sitecore

Solr Index rebuild for single Item (Document) in Sitecore

There are many live scenarios in sitecore application where we need to update the index to

reflect the changes on website (UI). As sitecore recommended we use solr/lucence search

index to play with huge data or search.

Some time we need to update some items or particular one item to update the index file.

So here is the way to update the single item in index file:

1. Go to sitecore

2. Go to developer tab: by default its hidden to show a developer tab, right-click a tab, and

then select the tab to show > developer.

3. Or go to ribbons > my toolbar >Customize>Add Re-index Tree under indexing tools

4. Now select the item which you want to rebuild the index

5. Click on Re-index tree button > it will re build the index of selected item and its

descendants.
Appreciate to some comments/

Publish items only if version available in target database

Publish items only, if available in the target database.

One of my client requirement was to publish only those items which have version available
in the target database, if we found any item which doesnt exist in target database or dont
have a version in current language then dont publish that particular item.

So here is the solution:

Override the publish:itemProcessing

Add below config file in App_config>Include>ItemProcessedEvent.config

<?xml version="1.0"?>

<configuration>
<sitecore>

<events timingLevel="custom">

<event name="publish:itemProcessing">

<handler type="Namespace.className, " method="OnItemProcessing" />

</event>

</events>

</sitecore>

</configuration>

Create the class with desired name and add below method:

public void OnItemProcessing(object sender, EventArgs args)

try

var argsProcessing = (ItemProcessingEventArgs)args;

var context = argsProcessing.Context;

Assert.IsNotNull(context, "ItemProcessedIndexingEvent.OnItemProcessing: Cannot get


PublishItem context");

var sourceItem = context.PublishHelper.GetSourceItem(context.ItemId);

var targetItem = context.PublishHelper.GetTargetItem(context.ItemId);

if (targetItem == null || (sourceItem.Database.GetItem(sourceItem.ID,


Sitecore.Context.Language).Versions.Count > 0 && targetItem.Database.GetItem(sourceItem.ID,
Sitecore.Context.Language).Versions.Count <= 0))

{
argsProcessing.Cancel = true;

return;

catch (Exception ex)

//

Solution: if you publish site, folder with sub items or single item, the publishing will check if
item and language version is available in target database, then only it will publish it
otherwise that particular item will skip from publishing.

Please feel free to provide your valuable input/feedback

Country Condition (Geo IP) item path location changed in sitecore 8

Country Condition (Geo IP) item path changed in sitecore 8 for personalization

Recently I have implemented the personalization(country based layout rendering) on site core 8.1 and
wanted to overrides the existing country condition of sitecore with my custom logic.

So I noticed that the path of the country condition has been changed as below
/sitecore/system/Settings/Rules/Definitions/Obsolete/Marketing Workflows/Conditions/Geo IP/Country
Condition

Previously in sitecore 7 versions the country folder path as below:

/sitecore/system/Settings/Rules/Conditional Renderings/Conditions/Geo IP/Country Condition

Happy Sitecore :)
Think before GO-Live - Check Sitecore Security First

All development has been done and planning to GO-Live? Stop thinks about security. Security is the
major concern for any web application and it should be well implemented to avoid any vulnerability,
security itself is a very big topic and difficult to implement from all aspect but yes we can secure our site
as much as possible.

Is your Sitecore application secure? Ask this question again and again, what all are the check-list have
followed for better security of the system?

Even if your Sitecore solution does not require authentication for users of the managed websites,
youshould consider Sitecore security when designing your information architecture.

Here I am listing some checklist that should be implemented before GO-LIVE.

1. Protect your user password policy: enforce user to enter the strong password. Please refer the blog
for complete details: http://sitecoresolution.blogspot.in/2014/05/sitecore-security-password-expiration.html

2. Ensure you changed the default admin password: Changing the password prevents unauthorized users
from using the default password to access the admin account

Step

1. Login with admin user:


2. Go to security editor >

3. Go to user manager >

3. Restrict Anonymous Access to Sitecore Folders from IIS:

We should restrict following folders

/App_Config

/sitecore/admin

/sitecore/debug

/sitecore/shell/WebService
Below are the steps to change the permission level of this folder:

1. Open the IIS > run> inetmgr

2. Navigate to the Web Sites\your instance name\folder name.

3. Double-click Authentication under feature view.

4. Disable the anonymous user

4. Ensure your login page on https: you can use If you do need HTTPS on some (but not all) of your websites
pages you might also want to consider the SSL Redirector module on the Sitecore marketplace. It allows serving of
content items over HTTPS encryption by adding the template to the templates of the items you wish to be encrypted.

5. Ensure that Client RSS Feeds is disabled if there is sensitive information: just disable the client rss
feed setting from webconfig

6. Ensure that the only way to upload files is from the Media Library: by disabled the Upload Watcher the
files that are placed in the /upload folder are not automatically uploaded to the Media Library.
7. Ensure the correct license file on the production server: Install the correct license in each environment.
Most important, do not install a license that allows content management in a content delivery environment. An
improper license can increase the solutions vulnerability to attack.

8. Ensure to follow best practice if importing users from another system.

9. Ensure your custom error on: Remember to update your production web.config to <customErrors
mode="RemoteOnly" />. This will allow to you have a friendly error message to your site visitors should
an error occur.

10. Ensure your custom administrative pages are fully protected never leave these pages
unprotected.

11. Prevent Cross Site Scripting (XSS) Attacks: Cross Site Scripting (XSS) attacks are when a user submits
HTML, script or SQL code to your site via form fields. Client-side validation should prevent malicious data being
entered, but remember that this relies on JavaScript, which is trivial to disable in the browser. Add the following
attribute to the <httpRuntime> element in your web.config file to enable request validation:

12. Ensure that security rights is assigned to roles and not to users.

13. Ensure that home item permission is Heavily restricted of each managed site, and grant access
rights to its children and descendants instead.

14. Use UserSwitcher wherever required instead of SecurityDisabler when editing programmatically.

15. All non-implemented membership provider methods should throw non-supported exceptions

16. Create the roles in Sitecore Domain instead of specific domain

17. Use locally managed domains in the case of a multiple site implementations in single Sitecore
instance.

18. Turn off Auto Complete of Username in the Login Page

You can specify that Sitecore should not complete the username of users automatically when they log
in. This is useful, for example, if you do not want user names to be disclosed when content authors log
into Sitecore on a shared or public computer. In addition, you can disable the Remember
me checkbox.

To disable auto complete of user names, open the web.config file and set
the Login.DisableAutoCompletesetting to true. This disables autocomplete on the Sitecore login forms
on the /sitecore/login/default.aspx and /sitecore/admin/login.aspx pages.

To disable the Remember me checkbox on the login page, open the web.config file and set
theLogin.DisableRememberMe setting to true. This also ignores any existing Remember Me cookies,
and all users have to log in again

Hope this will help you.

Happy

How to Make Sitecore Architecture More Powerful !

How to make Sitecore architecture more powerful!

Architecture begins where engineering ends. - Walter Gropius

Architecture is as important in software development as it is in building construction. No one breaks


ground on a building without any plans and good architect.
With the non-functional requirements captured, the next step is to start thinking about how you're going to
solve the problems set out by the stakeholders and define the architecture.

The architect should be very careful about IA architecture and initial back grounding of the project. He/she
should know all the alternative and pros/cons of the technology and of course cost.

Architecture definition is about introducing structure, guidelines, principles and leadership to the technical
aspects of a software project. So here I am adding some point that can help the Sitecore architect to build
a robust system.

There are total 24 points that every Sitecore Architect Should Know:

1. Identify the scope of the project: scope of the project must decide at the very begging of the any
project so accordingly we can design/architect the application, below are some few sample questions that
needs to be asked for a customer for Sitecore applicable:

How will the website use the content?

Scope of data migration?

Scope of Web Forms?

Scope of any third party integration like CRM, Sales-force, Facebook

Infrastructure requirement would be on cloud or physical servers?

Any specific performance required?

Any rapid development requires like campaign etc.

Scope of multi-lingual and multi-site?

Scope of DMS, what type of reporting they want?

What kinds of content items exist?

What types of content elements will appear consistently on multiple pages (also known as reusable
content)?

Where does the aforementioned, reusable content come from?

Does the reusable content display different content on different pages?

Does the system display content from other systems?


How do you integrate the content from the other systems?

Is the solution multilingual? If so, does the content on the multilingual versions differ?

What kinds of users will have access to the CMS?

What website regions or content types will each user have access to?

2. Choose the Right Sitecore version:

Always choose the right version of Sitecore, before project getting started. Its not always good to
implement the latest version of Sitecore, it should be decided after considering the factors below

Team strength

Product version maturity and stability

Cost

Scope and timeline of the Project.

3. Selection of technology:

Web Forms and MVC are two different approaches for building Sitecore application

Both can be excellent choices, and both can be very bad choices,

Depending the requirements of the application and the knowledge and experience of the team on
technology.

4. Setup Sitecore project and code deployment methodology:

There are two ways to set up the visual studio solution with Sitecore. Your solution is fully coupled with
the Sitecore web site. Your solution is outside of Sitecore's website and you use some post build process
to deploy. For more information please visit at http://sitecoresolution.blogspot.in/2015/02/setup-sitecore-
development-environment.html

And its always recommended to keep your project outside the root directory

5. Identify the build deployment process:

Architect need to be decided which methodology should be used for deployment purpose, obviously the
cost can be the most important factor like TDS has license cost over serialization or package creation
methodologies.

6. Use of Sitecore Rocks: its need to be decided that Sitecore rock would be used in the project or not.
Sitecore itself recommends using this tool for rapid development.

7. Responsive design framework: RWD is very common nows a day, it need to be decided which
framework would use for responsive design like bootstrap, htm5 etc. So, accordingly resource and design
would be finalized.
8. Third party integration: It should be proper planning for any third party integration with Sitecore
application like CRM, Sales force, etc., so, accordingly IA architecture and solution can be designed.

9. Defining IA: Information architecture design can be as significant as the coding of a CMS solution.
Below are some important aspect that should be considered

10. Scope of DMS: Sitecore layout, template, DB design (Mongo-DB in Sitecore 7.5 or later) must be
designed based on the DMS scope like personalization, email campaigning etc.

11. Search selection: search is the backbone of any ECM system, so need to decide very carefully which
search pattern or framework should be implemented like Lucence, Solr, Coveo, custom search pattern.
You can find some information on Coveo search
herehttp://sitecoresolution.blogspot.in/2014/09/coveoindustrys-most-advanced-sitecore.html

12. Setting multiple site solution: It is possible to have multiple sites in the same solution in Sitecore, but
there are a few pitfalls and issues which need to be taken care while developing the solution.

13. Identify the multi-lingual solution: Architect also needs to consider the factor of multi-lingual solution
while creating an IA including template, layouts, presentation and standard value.

14. Publishing architecture: publishing is very time consuming process in Sitecore application, sometime
we required the dedicated Sitecore instance for publishing task to avoid long queue.

So here we need to decide and set up the accordingly

15. Setup multiple CM and CD: By default, each Sitecore instance provides both content management (CM,
for CMS users maintains the managed sites) and content delivery (CD, for visitors to those managed
sites). But we can separate the CM and CD facilities into separate environments, typically involving
separate servers. Its always recommended to keep separate the CM and CD environment. Below are
some advantage of separating CM and CD:

Scalability

Performance

Security

Administration

16. Handling media library: we can manage a media library within Sitecore by two ways either in

Database or file system. Both have pros and cons so, at the time of designing the project we need to
decide the best approach. Also, we need to consider some cloud based approaches like DAM with
Sitecore etc.

17. Caching implementation: Cache plays very important role in website performance. So, an
understanding of all Sitecore caches is really important before implemented. If we understand all of them,
then it would be easy to do performance tuning using cache settings.
18. Web form implementation: architect also needs to identify the web form marketer scope and find out
how it compatible with MVC.

19. Logging mechanism: Sitecore uses log4net for its logging purposes, but in some situations we need to
build a custom logging mechanism. One of the most common scenarios would be a multi-site solution in
which the standard log will become huge and finding the exact trace information becomes painful. so we
need to consider this factor at the time of designing the architecture of the application .

20. Error handling in Sitecore: we need to consider various levels of exception/error handling like Custom
Errors, Try... Catch... Finally Blocks, XSL Exception Management, Web Forms Exception Management,
MVC Exception Management, Application (Global) Exception Management, Error Pages, Managing
Exceptions. So after taking all error handling, need to decide the base architecture where it should be
forcefully implemented.

21. Sitecore customization: architect needs to identify the scope of customization in Sitecore like pipeline
and some UI customization so accordingly template and project should be modified from the very
beginning of the project.

22. Consider Sitecore performance: Sitecore IA plays a very important role in performance point of view,
below are some points that need to be considered for optimization purpose of the application:

Avoid Item which has more than 100 items.

Caching tuning

Enable CSS, JS Caching, Compression

IA architecture (avoid creating items with hundreds of children

Optimization in publishing wizard

Prevent use of Get Descendants

IIS level optimization

23. Sitecore security: Sitecore provides a comprehensive security infrastructure that you can use to secure
any item in any Sitecore database. An architect should know all the aspect of Sitecore security and should
implement at the time of designing the application. Sitecore also provides additional functionality that
enables you to use Microsoft Dynamics Customer Relationship Management (CRM) and Active Directory
(AD) for authentication.

Below are the some security topics that need to be considered at the time of designing the site.

Access rights

Users

User profile

Roles (groups)

Domains
Security inheritance

For more information: http://sitecoresolution.blogspot.in/2015/05/go-live-security-checklist-sitecore.html

24. Scope of data migration: scope of data migration requirement should be handled from beginning of the
project here you can find the complete details on this topic.

http://sitecoresolution.blogspot.in/2015/01/sitecore-tips-techniques-on-data.html

I Hope this article will help you.

Happy

VIEW RENDERINGS VS. CONTROLLER RENDERINGS IN


THE SITECORE ASP.NET CMS
By John West, January 19, 2015 | Rating: | Comments (5)

This blog post contains my perspective on contrasts between view and controller
renderings in MVC solutions that use the Sitecore ASP.NET web Content Management
System (CMS) and Experience Platform (XP). In many cases, you can use view
renderings and controller renderings to achieve the same objectives, and the choice is a
matter of preference. In other cases, one rendering technology may be more
appropriate based on criteria including those discussed here. This blog post does not
consider these constructs in the context of processing entire requests at the
page/request level, but only these two types of renderings, and only at the presentation
component level.

Sitecore MVC solutions support view renderings, which allow runtime binding of nested
views to placeholders in existing views. Sitecore invokes the mvc.getModel pipeline to
determine and construct the model to pass to the view as specified in the view definition
item. It then passes that model to the class compiled from the .cshtml file specified in
the view definition item.
Controller renderings function similar to view renderings in that they define presentation
components for nesting within other components. With view renderings, the rendering
engine invokes a .cshtml file; with controller renderings, the rendering engine invokes
an action method in a controller class.

Logic in the action method of a controller renderings can invoke the View() method in
the System.Web.Mvc.Controller base class, which in turn invokes a view. With this
approach, the controller rendering functions much like a view rendering, but with at least
two differences:

The controller can contain logic, including creation of the model passed to the view.

The View() method called by a controller renderings can use ASP.NET logic to determine
the path to the .cshtml file rather than using a view definition item in which a field must specify
the literal path to the file.

Due to the order of operations and for performance or other reasons, some logic may
belong or function correctly only in the primary controller activated by the HTTP request
rather than an individual controller rendering or view rendering. For example, the
primary controller may be responsible for redirection. Again, this blog post does not
attempt to account for these considerations at the request level, but only presentation at
the component level.

View Renderings

Simple to use for relatively small and quick components that mostly contain markup with
little logic, and for which the Sitecore model resolution process suffices, such as simple
content presentation.

Without a solution such as the first blog post linked in the Resources section at the end
of this page, require the creation of a model definition item. This can be useful when passing
different models to a single view.

Relative to controller renderings, simplify form posts

Can contain nested placeholders

Can be useful for components that generate significant markup

Use the mvc.getModel pipeline to determine the view, typically from the view definition
item
Controller Renderings
Provide a superset of the features available with view renderings

Support separation of concerns by adding controller logic not in the model (state) or the
view (presentation)

Allow the developer to construct and pass a model to a view

Can return null, avoiding view invocation if there is no data to present

Can be appropriate for generating output other than markup, such as JSON

Do not require model definition items

Bypass the mvc.getModel pipeline

May be overkill for simple things

Provide standard MVC support for common patterns such as repository and dependency
injection

Controller renderings can be more testable than view renderings

Potentially fewer files to deploy, specifically .cshtml

Create MVC Controller and action method in .NET MVC Solution.

1. Assuming that you have already created and configured a .NET MVC project for sitecore,
create a new controller in that .NET MVC solution.

2. Name the controller as Cart and create a new action method List inside it as shown below.

CartController.cs

public class CartController : Controller


{
public ActionResult List()
{
CartList model = new CartList();
//model = ..; Perform database operations and retrieve
model details
return View(model); //return model details to the view
}
}

3. Create a corresponding view for this action method. i.e., create List.cshtml under Views/Cart/
path in the same .NET solution. Sample view file is shown below.

List.cshtml

@model Sitecore.MyMVCSolution.Website.Models.CartList
@{
if(Model!=null)
{
foreach(item in Model.CList)
{
<div class="testrow">
<div class="prod-name-css">@item.ProductName</div>
<div class="prod-qty-css">@item.ProductQty</div>
</div>
}
}
else
{
<div>
<h4 style="text-align:center;">
There are no items in this cart.
</h4>
</div>
}
}

Create Controller rendering in sitecore

1. Login to Sitecore Content editor. Go to Layouts->Renderings.

2. Right click on Renderings, click on Insert->Controller Rendering. Enter the data values as
shown below.

Controller: Cart, Controller Action: List

3. Save and publish the item.

Create sitecore Content Item


1. In Sitecore content editor, create a new item under Content. Name this as the controller
name i.e., Cart. For creating this, select any default template or create a new one based on
your need.

2. Create another item under the newly created item. Name it as the action method in the
Controller i.e., List.

3. Once these two items are created, the path http://Website/Cart/List will be available.
Whenever this link is called from a browser, the request will be hitting the List content item.
Now we need to make sure that it is rendered appropriately using a Controller rendering.

Configuring presentation details of sitecore content items.

1. Click on the List content item in sitecore.

2. Go to Presentation tab and click on Details.

3. Edit the default details by clicking on Edit link.

4. Under Layouts, select the layout you want to choose.

Sample Layout File with placeholders (SampleLayout.cshtml):

<!DOCTYPE html>
<html lang="en">
<head>
<title>Sample Page</title>
</head>
<body>
<div class="header">
@Html.Sitecore().Placeholder("phHeader")
</div>

<div class="Body">
@Html.Sitecore().Placeholder("phCartListController")

</div>

<div class="footer">
@Html.Sitecore().Placeholder("phFooter")
</div>
</body>
</html>
5. Under Controls, add the rendering items for this page. For each placeholder in the layout file
add a presentation/rendering item. Here the rendering item could be view rendering,
controller rendering, XSLT rendering or of any other type based on our need.

o For adding the controller rendering, click on add.

o Under Renderings in the left, select the controller rendering we have created i.e.,
CartListControllerRendering and select the placeholder as per the layout file. Say
phCartListController. Click on select and then ok.

o Save and publish item.

6. Now run the URL http://Website/Cart/List from the browser. This will hit the List content
item in the sitecore. While rendering the List item, the controller action is called. The
controller action will return a model to the view. The view is rendered and added to the
placeholder phCartListController in the layout file.

Building a Carousel in Sitecore MVC

Part 1: View Renderings


22 February 2015

Introduction
In this three part tutorial I describe how to build a carousel in Sitecore MVC
using view renderings (this part), controller renderings and finally item
renderings. It doesnt discuss the CSS or JavaScript aspects of the carousel,
just the basic HTML markup and the necessary Sitecore templates, items,
Razor and C# code.

HTML Markup
Our HTML markup is shown in the image below. Note that we may have as
many carousel-slide items as we want.
Sitecore Content for the Carousel
For this tutorial we will be focusing on the rendering and code aspects of the
carousel, but as a brief overview we will use the following content structure:

One item to define the datasource of our carousel. This is a sub-item of


the page.
One sub-item per carousel slide.

This carousel item sits beneath the page on which it is used, but there is no
reason you couldnt place it in a shared content folder outside of the main site
tree, if you want to share the carousel content between multiple pages.
Note that the carousel item has a "SelectedItems" field where the required
slides can be chosen. We could just make the assumption of using the child
items of the carousel item, but this gives us a bit more flexibility and allows us
to remove slides from display without deleting them.

Below is a screenshot showing the fields used on our carousel slide items.
Part 1.1: Building a carousel with a Sitecore
MVC View Rendering without C# code
This section describes how to build the carousel without writing any C#
controller, model or rendering model code. This is a great option if you are a
front-end developer, or are transitioning from XSLT renderings to MVC.

This is the approach I would also recommend for structural and container-
style renderings where the intention is to provide HTML scaffolding and
placeholders for other components. Note that this solution just selects the
carousel slides based on the children of the datasource.

View (cshtml)
The code for the view is actually very simple. You dont even need to include
the @model directive, but it can be useful to make it clear what model the view
uses. Sitecore automatically passes an instance of
the Sitecore.Mvc.Presentation.RenderingModelclass to the view, so you can
access two main properties of interest: Item and PageItem. Item refers to the
datasource item set for the view in the rendering component properties,
whereas PageItem refers to the context item of the page.

<div class="carousel">

@foreach (var carouselSlide in Model.Item.Children)

<div class="carousel-slide">

<div class="carousel-slide-content">

<h1>@Html.Sitecore().Field("Header",
carouselSlide)</h1>

<p>@Html.Sitecore().Field("SubHead",
carouselSlide)</p>

@Html.Sitecore().Field("CallToAction",
carouselSlide)

</div>

@Html.Sitecore().Field("Background", carouselSlide)

<div class="clearfix"></div>

</div>

</div>

View rendering definition item


The rendering definition item is configured as shown below with the Path
field set to our view - note that because our view rendering uses the default
Sitecore RenderingModel there is no need to set the Model field.

Using the View Rendering


1. Go to the "Home" item and select the presentation details and add the
new Carousel (Simple View Rendering) rendering.
2. Set the datasource to point to the Carousel item beneath the page.

Part 1.2: Building a carousel with a


Sitecore MVC View Rendering and a
RenderingModel
In this section we build the carousel using a Sitecore MVC View rendering
with a RenderingModel. As a minimum our model must implement
the Sitecore.Mvc.Presentation.IRenderingModel interface - you can also make it
inherit from Sitecore.Mvc.Presentation.RenderingModel. I refer to this type of
class as a "RenderingModel" to make the distinction from the various other
types of model you could encounter in ASP.NET MVC.

You may wonder how this model is initialsed if we dont create a controller?
This is handled by the standard Sitecore controller. You will also need to
create a model definition item in Sitecore which tells the Sitecore controller
what class to instantiate when invoking the view rendering.

RenderingModel class
This is a simple class inheriting
from Sitecore.Mvc.Presentation.RenderingModel (or implementing the
interface Sitecore.Mvc.Presentation.IRenderingModel) which overrides the
Initialize method.

public class CarouselRenderingModel :


Sitecore.Mvc.Presentation.RenderingModel

public override void Initialize(Rendering rendering)

base.Initialize(rendering);

CarouselSlides =

Sitecore.Data.ID.ParseArray(Item["SelectedItems"])

.Select(id =>
Item.Database.GetItem(id)).ToList();

public IList CarouselSlides { get; private set; }

View (cshtml)
The razor markup for our View Rendering is almost identical to that of the
previous example, but includes the @model directive and references our
CarouselSlides property instead of the child items.
@model SitecoreMvcCarousel.Models.CarouselRenderingModel

<div class="carousel">

@foreach (var carouselSlide in Model.CarouselSlides)

<div class="carousel-slide">

<div class="carousel-slide-content">

<h1>@Html.Sitecore().Field("Header",
carouselSlide)</h1>

<p>@Html.Sitecore().Field("SubHead",
carouselSlide)</p>

@Html.Sitecore().Field("CallToAction",
carouselSlide)

</div>

@Html.Sitecore().Field("Background", carouselSlide)

<div class="clearfix"></div>

</div>

</div>

Model definition item


Under /sitecore/layout/Models create a new Model. In the Model Type field enter
the fully qualified namespace and class name and assembly in the
format: MyNamespace.MyRenderingModel, MyAssembly
Creating the View Rendering definition item
1. Under your renderings folder create a new view rendering
2. In the Path field enter the path to the cshtml view (using forward
slashes)
3. In the Model field select the model definition item created previously
4. Go to the CarouselDemo item and select the presentation details and
add the new Carousel (View Rendering) rendering.
5. Set the datasource to point to the Carousel item beneath the page.
Using the View Rendering
1. Go to the "Home" item and select the presentation details and add the
new Carousel (View Rendering) rendering.
2. Set the datasource to point to the Carousel item beneath the page.

The finished carousel should now appear as shown below:


Summary
In this first post, I have covered two ways to build Sitecore MVC View
renderings:

1. Without C# code or rendering model - useful for front-end developers,


building simple or scaffolding-like components, banishing XSLT!
2. With a RenderingModel class and Model definition item - useful when
you want to apply some logic/control to your item selection

The source code for the tutorial series can be found on GitHub. When you're
ready please check out the next two posts on:

Controller Renderings and


Item Renderings

You might also like