Hot answers tagged

23

Controller: public class RotatorController : SitecoreController { public ActionResult Index() { var dataSourceId = RenderingContext.CurrentOrNull.Rendering.DataSource; var dataSource = Sitecore.Context.Database.GetItem(dataSourceId); var viewModel = new RotatorViewModel { RotatorItems = dataSource.Children }; return View(...


21

There's a web.config that has Sitecore references that should exist in the /Views that is probably not right. This can be a common occurrence if using a new .NET MVC Project in Visual Studio and it accidently overwrites the Sitecore version. You can grab the web.config from the Sitecore Vanilla Zip file to compare. <system.web.webPages.razor> ...


20

Because Sitecore supports server-side responsive rendering, it's very easy to use the Device feature of the CMS to support AMP. AMP Layout (using Razor) This example is adapted from Google's example markup and Razorified: <!doctype html> <html amp lang="en"> <head> <meta charset="utf-8"> <script async src="https://cdn....


16

You should. Here are few things that you should know. Without this div element your rendering will not be SXA compliant. If you decide to skip it you will lose following features/abilities: your rendering will not be able to use "SXA styles". This div is the main wrapper and inside class attribute other classes which can be specified in Rendering ...


13

Its because you are not using the Sitecore field render to render the field content. This code: var mainContent = Sitecore.Context.Item.Fields["Main Content"].Value; just outputs the raw value of the field. It bypasses all of the Sitecore field rendering pipeline. This includes all the markup, and because razor automatically encodes html in variables you ...


12

To do page redirection, simply do return Redirect(url); For a permanent redirect (e.g. for search engines), use return RedirectPermanent(url); Both of these return a RedirectResult which inherit from ActionResult. To redirect to a Sitecore Item, do it the same way you always have. var url = LinkManager.GetItemUrl(yourItem); return Redirect(url);


11

Conditional renderings work fine with MVC. For version 8, you must have Analytics.Enabled set to true so you can get to the personalization section of the presentation. "Global" conditional renderings do not work with MVC, so you need to stick to personalization of the rendering on the items presentation rather than a global rendering rule. Here is how you ...


10

So it's took me all of yesterday and most of today to figure this out. In the end it was nothing to do with my code or configuration at all. The log files showing some errors were a Red-Herring and leading me in the wrong direction. It is a Sitecore bug with 8.1 Update-2 (public reference: 108023). This blog post was really useful for identifying it: http:...


10

You could create a new template to create datasource items that bundle your "multiple items" (in separate link type fields, or with a multi-select type field - as you want). This way you are able to set one item as datasource and find all your items on that single datasource. The benefit of keeping the datasource approach is that datasources can be ...


10

So for Sitecore - a global error handler in the global.asax will not work properly. The better way to do it is to override the code that calls each rendering and handle the errors there. ExecuteRenderer With a simple override of ExecuteRenderer we can easily catch all exceptions: public class ExecuteRenderer : Sitecore.Mvc.Pipelines.Response....


10

The Pipeline that you want to focus in on is the mvc.getPageItem pipeline. <mvc.getPageItem patch:source="Sitecore.Mvc.config"> <processor type="Sitecore.Mvc.Pipelines.Response.GetPageItem.SetLanguage, Sitecore.Mvc"/> <processor type="Sitecore.Mvc.Pipelines.Response.GetPageItem.GetFromRouteValue, Sitecore.Mvc"/> <processor type="...


10

@Html.Sitecore().Field("Property Name") only works when your model is Sitecore.Mvc.Presentation.RenderingModel. You are using a custom model. To use it in your case, you will need to pass in the item like this. @Html.Sitecore().Field("Property Name", Item) In your case, you would need to pass in the datasource item. Your model would look like this, notice ...


10

Yes - Sitecore JSS The officially supported Sitecore JSS has full support for ReactJS components on MVC pages in the form of Javascript Renderings or Client Side Embedding. JSS Javascript Renderings JSS Javascript Renderings offer support for both server-side and client-side rendering of ReactJS components on an existing Sitecore MVC page. Client Side ...


9

With MVC 5, you should have the option to add an MVC 5 View Page, like so, Using this option gives you a simple popup to type in a name, then it generates a cshtml file with no scaffolding. @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title></title> ...


9

Background When the ItemResolver processor executes, it determines the context item for the request based on the current URL. Additionally, it also performs a security check to see if the current user can access that item. If they can't, the context item is cleared and a PermissionDenied property on HttpRequestArgs is set to true. When the ExecuteRequest ...


9

Yes, you can do it by the following way. Configuration: <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <pipelines> <mvc.renderRendering> <processor type="YourAssembly.LogExecuteRenderer, YourAssembly" patch:instead="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering....


8

Yes - you are using the Raw Value when you do @Html.Raw(Model.SampleItem.Text.Raw) - this bypasses the field renderer and so will not be editable in the Experience Editor. Instead of .Raw use .Rendered - this will render the field using Sitecore's Field Renderer and add the page editable parts. @Html.Raw(Model.SampleItem.Text.Rendered)


8

The reason your model isn't being populated is because @Html.Sitecore().ControllerRendering("Module", "IssueSection", new { area = "Editorial", issueSectionViewModel = issueSection }) puts the properties of your anonymous object into the properties dictionary for the controller rendering you're calling, not route data. The built-in MVC model binders only ...


8

Methods like @Html.Action(), @Html.RenderAction etc. apparently requires a route with the {controller}/{action} pattern (even though it isn't actually relevant from what I can see). The config file Sitecore.Speak.Mvc.config contains such a route. Although the route is Sitecore specific it does contain the {controller}/{route} pattern. However, the ...


8

It sounds like you have added the component to a placeholder key called section - with dynamic placeholders there will be an extra bit of data appended to that. Depending on the version of dynamic placeholders you have installed, that data changes. If you have installed the Fortis Dynamic Placeholders - this appends the guid of the unique rendering Id to ...


8

You're going about it wrong. Or - I should say - not in accordance with Helix principles. First and foremost, your idea of a common base template to be used on all websites is not recommended practice. What Helix Documentation States The architecture does not have the concept of a single common base template across all templates – which is a practice ...


8

What I ended up doing is registering a single route on pipelines/initialize: <pipelines> <initialize> <processor type="MyFramework.Routing.RegisterVirtualFoldersRoutes, MyFramework" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" /> </initialize> </pipelines> and ...


8

If it's an ajax call you just did from javascript to a normal MVC controller, there is no Sitecore.Context.Item there. What you can do is in your original Sitecore rendering write the id of your item to the generated html (e.g. as a data-itemid attribute) and then pass it with your ajax call back to the server. Thanks to that you will know which item you ...


7

As has been mentioned. You should do this in your Constructor action and view model. This is an example of how you could do this without and ORM. But really if you are not using an ORM you are making life more difficult for yourself. Have a look at Fortis (my preferred choice) or Glass Mapper (another very good choice). Create your view model properly: ...


7

I'm not sure what code you have currently but in your controller you could do something like this to get the image urls for the sub items and return them in a view model: List<string> imageUrls; foreach(var item in parent.GetItems()) { string hashedUrl = HashingUtils.ProtectAssetUrl(Sitecore.StringUtil.EnsurePrefix('/', Sitecore.Resources....


7

Like Mark Cassidy mentioned above, <mvc.requestBegin> would be a close "equivalent". I ran into a similar situation wanting non-authenticated users to be redirected to the <site loginPage="/login">. The only caveat I had was that I needed some custom logic to create virtual users based on their IP address (site to site VPN), so I decided to ...


7

There are a couple ways you can do this. In the Custom Item Generator, each field has a .Rendered property you can reference which just runs the field renderer on that field. Use this if you want to run the Sitecore rendering pipeline for that field. https://github.com/Velir/Custom-Item-Generator/blob/master/Fields/BaseCustomField.cs So for your example, @...


7

I've done this before using a wildcard item for the parameter and a custom resolver processor that runs just after the ItemResolver - The ItemResolver should resolve to /sitecore/content/mysite/products/* - so in my custom resolver I know if I'm on that item, parse the Url, take the last segment and store the value somewhere - like Sitecore.Context.Items["...


7

You need to define your form properly for file uploads to work. The most important part is to set enctype to multipart/form-data. Normally, you should use the standard Html.BeginForm helper: @using (Html.BeginForm("VacancyDetail", "VacanciesController", FormMethod.Post, new { enctype = "multipart/form-data" })) { <input type="file" name="Resume" /> ...


7

The problem here is that a controller rendering in the Sitecore world is not the same as a Controller action result in standard MVC. So you can't pass data through in the same way without custom code. From your example, it looks like all you really need to do is use an @Html.Partial to render your other razor view and pass the data through like that. Your ...


Only top voted, non community-wiki answers of a minimum length are eligible