Including MVC in existing Sitecore Project - sitecore

we have a Sitecore Project up and running, that is based on the regular aspx/ascx approach.
Over time we would like to transform our existing sublayouts to MVC.
For test purposes I am trying to add a very simple MVC text component to the project, still I am stuck somehow.
What I have done so far:
Installed MVC 5.2
Installed WebPages
Added references and bindings
Added the MVC Scaffold
Right now, the Site does compile and run.
I have this Controller:
public class TextComponentController : Controller
{
public ActionResult Index()
{
return View();
}
}
And my view:
<h2>Index</h2>
<p>Hello from my View Template</p>
So absolutely nothing special here ;)
How can I create a sublayout (without a datasource) that just displays this simple MVC component?

In the very simplest way, you need to have the following:
Item for your page, the one that has URL; that is as normal in Sitecore
That page Item should have Layout assigned. From Presentation --> Details menu select at least a layout on that stage. If you do not have layout yet, you need to create a layout definition item under /Layout/Layouts folder and associate it with certain *.cshml file. Also mention that layout should have a placeholder where you will "inject"your rendering.
#Html.Sitecore().Placeholder("Main")
You need to create a Controller Rendering under /Layout/Renderings folder in Sitecore. Make sure you set Controller and Controller Action fields to your controller name and action method name.
Finally, go again to Presentation --> Details --> Edit --> Controls and add your newly created rendering into a placeholder that you have on your layout *.cshtml file.
That's all done.
Hope this helps!

Related

how to add view template in asp.net core

i want to add some view template for asp.net core mvc 3.1.1, i copied Templates folder from this path:"C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.visualstudio.web.codegenerators.mvc\2.1.7\Templates"
and added a new view with name "_Entiti.cshtml" to "ViewGenerator" folder, but when trying to add a view it not appears in dialog box of vs 2019 inside of templates drop down, originally i want to use controller with context to generate some views with custom controller, but couldn't change view name that been created like "Create,List,...".
can any body help me?
Try using the solution explorer. Right click on the Shared folder (under Views) and click "Add" --> "View".
Then on top of the pages that you would like to be using that template write:
**substitute the word "View" with the name of your template:
#{ Layout = "~/Views/Shared/View.cshtml"; }

Sitecore 7 adding a sublayout dynamically based on tree structure

based on a single page view I am looking to have my content structure dynamically build the page.
So as an example: with a child item of GALLERY template, this would itself have sub-items using the GALLERY ITEM template which defines each of the images to be shown in a carousel; so an Editor can simply add multiple items and the carousel will scale accordingly rather than having fixed fields (banner, banner 2 etc)
Currently I am having an issue in understanding how my sublayout which defines the galleries HTML can automatically be included in this main page if that template is present.
Rather than an Editor having to manually add the Sublayout and define a datasouce to the gallery node I'd want the main page to iterate down the tree and observe there is a template for GALLERY therefore knowing it needs to use the sublayout and add it to the main page placeholder
On this principle the page will self build depending on what items an Editor adds to the page.
Any advice or sample code (using webforms) is appreciated; currently I am having to manually add sublayouts which seems to not be intuitive for Content Editors to assemble pages.
Thanks
If you are properly using and configuring the Page/Experience Editor, adding the sublayouts and data sources is a very intuitive process, and allows you to take full advantage of all the features and capabilities of Sitecore, in particular personalization and A/B testing. This is the recommended practice that you should follow.
That said, if you have extenuating circumstances that really prevent you from doing this, there is an older technique called "Presentation Inversion of Control" which pulls renderings from items associated via the content tree. It was useful prior to the "unified page editor" and datasource wizards which appeared in Sitecore 6.4. There are various approaches to this, but the simplest is a sublayout/web control that you place on the page.
The following code hasn't been tested by be on Sitecore 7:
public partial class Gallery: System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
PageItem page = Sitecore.Context.Item;
//custom item contains logic for finding items to include
foreach (Item galleryItem in page.GalleryItems)
{
string strDataSource = galleryItem.ID.ToString();
RenderingReference[] renderings =
galleryItem.Visualization.GetRenderings(Sitecore.Context.Device, false);
foreach (RenderingReference rendering in renderings)
{
rendering.Settings.DataSource = strDataSource;
this.Controls.Add(
rendering.RenderingItem.GetControl(rendering.Settings));
}
}
}
}
But really, you should just properly implement the Page Editor.

Sitecore 8.1 Header and Footer Composite Component

In Sitecore I have created base Template & Renderings for "Header, Top Menu, Logo, Search ,Login Button…etc that comes in Header across all Pages of the Site."
Also the same template & Rendering is using in other website so we can't set value in Standard field.
Now the problem is “suppose we have 100 pages in a Site then each page we have to go and add/update rendering & data source.
Is there any way that a “Header rendering” contains child rendering and by placing “Header rendering” render all its child renderings.
Header Composite Component ===>
Logo Component
Top Navigation Component
Search Component
There is a composite component module on the Sitecore Marketplace :https://marketplace.sitecore.net/Modules/C/Composite_components.aspx?sc_lang=en
This fill's your requirements exactly.
It works by creating a "Composite Component" item, then you add your renderings for your header to that component. E.g. Logo, Navigation etc...
Then in the standard values of your page templates, you add a new Composite Component rendering and set the datasource to the component item you created.
The module will then inject the renderings from the component item presentation into your current item. So you only have to update a single place to update all pages etc...
I have this running in a few production sites and it works well. It even supports the Experience Editor.
I don't think there's a solution out of the box. SxA solves this with Partial and Page Designs at site level. Just wrote a blog about it:
http://reinoudvandalen.nl/blog/sitecore-experience-accelerator-partial-designs-and-page-designs/
I also made something that might help you: Partial Layout Presets
http://reinoudvandalen.nl/blog/sitecore-partial-layout-presets/
It even allows you to store presets per site, but keep in mind that it injects composed layouts. If you modify the preset then it does not get applied to the pages where it was injected (though you might be able to pull something of with a shared datasource per site).
Lastly the obvious solution would be to create a page template per site so you'll be able to have standard values per site.
You may want to take a look at using Placeholder Fallback.
This will allow you to add components and renderings to a top level page and then the child pages would "fallback" to using the renderings inserted into the placeholder of the top level page in the item hierarchy. The editors can also insert page specific renderings on each child page if required.
If you do not want to custom code and only out of the box features then you could use Devices. You can find more information in the documentation about Set up a device layout. Once the device is created you can specify a different default device per site:
Now when you open the Presentation Details on the __Standard Values of your templates you can specify a different Layout or Renderings per device, i.e. different ones for AltSiteDevice

How to make a statically binded rendering editable via Experience Editor (Sitecore MVC)

So I have a layout view within Sitecore Mvc, this view contained a Controller Rendering that pulls in a header and footer navigation. Example:
#Html.Sitecore().Rendering("/sitecore/layout/renderings/some_rendering")
This specifies a Controller Rendering I've defined in Sitecore. This works great, except when I'm in the experience editor. It doesn't give me the ability to select this rendering. If I create a Placeholder and then define these navigation elements to this placeholder dynamically via Sitecore, then I can, but these navigational elements exist on every page of this layout, so I would like them statically placed instead of using a Placeholder, but I would still like the user to be able to select the navigation element in the experience editor (so I can create custom command to interact with this navigation, such as creating new links, etc).
Does anyone have an idea that will help me achieve this?
Use Edit Frame for that and create Custom Edit Frame Button for operations like adding new element to the navigation.
And remember to pass Datarsource ID or Path as a second parameter to the Html.Sitecore().Rendering() method:
#Html.Sitecore().Rendering("/sitecore/layout/renderings/some_rendering", new { DataSource = "{some-id-or-path}" })
Here is set of blog posts which can help you to understand how Edit Frames work and how to add them in Sitecore MVC solution:
https://visionsincode.wordpress.com/2015/01/08/how-to-use-editframe-in-sitecore-mvc/
https://www.cmsbestpractices.com/how-to-properly-use-sitecore-edit-frames/
https://briancaos.wordpress.com/2011/11/28/using-sitecore-editframe-in-pageedit/
You won't be able to remove the component or move it around the page (yeah, it's statically bound to one place on your layout), but you will be able to edit it's properties and datasource.
You can try to use GlassMapper views and statically inherit the view from the GlassView.
Then you'll be able to use Editable method to render the field.
But the consideration you need to take is that you'll not be able to set a datasource to the component from the page editor or content editor.
Instead of injecting the rendering through the Rendering method you should be able to use standart MVC RenderPartial.
I've used this approach on one of the projects I've been on and it worked.

Sitecore 7 : Combine CSS from View Renderings into the Layout <head> area

this is probably a newbie Qs... i am using Sitecore 7 for my web application, and this is what i have so far
Data Template - this has only one field called "Title" to display page specific title
One Layout - this is pointing to my cshtml file under asp.net mvc project path. This has complete markup starting from Doctype. The title tag under head tag uses Sitecore's Html extension to render the field "Title" from template mentioned in #1. This also renders sitecore's View via placeholder called "page-body" under body tag of layout.
I have created a View Rendering pointing to Razor view in my asp.net mvc project. This view simply has h1 calling out hello world.
The Sitecore/Content/Home item (from sitecore's master tree) uses the template created in #1 and uses the layout created in #2. This item has one and only one Rendering created in #3
Now when i hit the root from my local sitecore website, everything looks good! I see Hello World in H1 tag under body tag with complete html makrup mentioned in layout...
This is where things starts getting complicated... Now i want my View Rendering (created in #3) to refer a CSS file which is specific to this Rendering only. This CSS will not be referred to all the pages. Of course I want to add the reference into the head as link href. So tried using asp.net mvs "Section" but i keep getting that sweet error "file cannot be requested directly because it calls the rendersection method..." So i realized that my view rendering doesn't have any #{Layout = "..."} and ofcourse which is controlled by sitecore engine!!
So i still went ahead and added Layout reference in my view Rendering's cshtml file and refer to the same layout file that Sitecore Engine would (i.e. #2 above). I still have same error.
Then i found a post Using sections in Editor/Display templates which is essentially for Scripts and tried implementing for CSS. But this didn't work either because View Rendering executes after the Helper in head tag gets executed so my CSS reference never go spit out in stream. BTW for Scripts this solution works perfect because scripts rendering helper is called after the Sitecore's view rendering.
At this point i am totally stuck for "How can i get my CSS ref for View Rendering in head tag". Any sitecore experts to help here?
So it's definitely a more advanced customization, but we do this using Cassette and a customization to the renderLayout pipeline. Use of this pipeline solves your issue of the helper in the head tag being called before your view renderings.
Extend the view rendering template (/sitecore/templates/System/Layout/Renderings/View rendering) and add a field that stores the name of a Cassette bundle you want to include on the page.
In your RenderLayoutProcessor, loop through all the page renderings (Sitecore.Context.Page.Renderings), inspect the rendering item for the bundle field (rendering.RenderingItem.InnerItem[YOUR_FIELD]), and call Cassette's Bundles.Reference.
In the header of your main layout, call Cassette's Bundles.RenderStylesheets().
You can also accomplish this without Cassette, but it makes the bundling, referencing, and inclusion of scripts and stylesheets much easier.
The other advantage to this approach is that it does not break Sitecore caching. Any approach which requires code to execute in your View rendering will render Sitecore HTML caching useless, because when the rendering is cached, your code will not execute and the page styling will be broken.