How to Get Datasource from sublayout using glassmapper - sitecore

I know in MVC you could do this.GetLayoutItem<Model>() to get the datasource of rendering on a page. How do we make use of this in sublayouts (.ascx)?

If you inherit your user control from the GlassUserControl the AbstractGlassUserControl has properties for LayoutItem which will give you the datasource if its defined or there is a DataSourceItem property which returns the datasource explicitly.
Using GlassUserControl

Related

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.

Including MVC in existing Sitecore Project

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!

Sitecore: Add a New Data Template to my Sublayout

I sublayout is about Events.
If I put it inside a page which contains an Event, it will display informations pertaining to it.
I want to add a Data Template to my Sublayout that will allow editors to specify one particular Event from which to draw these informations.
How do I go about creating a Data Template that allows an editor to choose a specific Event from a list?
How do I add this to a given Sublayout?
When building componentized architectures as you are doing, you will want to implement the datasource property for a sublayout to make your sublayout a datasource-driven sublayout.
This article by John West should help on how to set a datasource:
http://www.sitecore.net/Learn/Blogs/Technical-Blogs/John-West-Sitecore-Blog/Posts/2010/11/How-to-Apply-Data-Sources-to-Sitecore-ASPNET-Presentation-Components.aspx
The basics are:
Create a template for your Event to contain the data
On your sublayout item (/sitecore/Layouts/...) set the datasource template property to match to your new Event template.
Update your code to pull from the datasource item on the sublayout, not the Sitecore.Context.Item
The following post by Mark Ursino is helpful in seeing the code side as well:
http://firebreaksice.com/using-the-datasource-field-with-sitecore-sublayouts/

fetch list of sublayouts used by item in sitecore

I'm using Sitecore 7.1 with MVC.
I need to get the item ID of the sublayout or rendering used for a particular Sitecore items.
Is there a way i fetch list of sublayouts/rendering used by an item in sitecore using .net.
Could some post the necessary code for that.
Thanks
The traditional way to get an array of renderings prior to Sitecore MVC is:
Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, false)
I believe this will return a list of renderings in both a WebForms and MVC context.
Specific to MVC, you can also get a list of renderings by using the current RenderingContext or PageContext via:
RenderingContext.Current.PageContext.PageDefinition.Renderings
or
PageContext.Current.PageDefinition.Renderings
Please try below code
Sitecore.Context.Database.GetItem(((Sublayout)Parent).RenderingID);
Also see Using the DataSource Field with Sitecore Sublayouts.

Sitecore create drop-down button in RTE populated from master database

I'm trying to create a drop-down button for the rich text editor (RTE) in Sitecore but cannot figure out how to implement this. I would like something similar to the 'Insert Snippet' command shown below, but with the source of the dropdown driven by content from the master database instead of core items within the html editor profile.
The closest approach I've found is this article which describes how to add a button which opens a dialog in the RTE.
Another option could be to have a save handler which can create the snippet items in the core database based when items are created/edited in a certain area of the master database.
There's a fair amount of work involved to set up your own button, including all the the JS handlers as well. The easiest way to achieve what you want is (as Ben Holden states) is to inherit from Sitecore.Shell.Controls.RichTextEditor.EditorConfiguration and override the SetupSnippets() method:
public class EditorConfiguration : Sitecore.Shell.Controls.RichTextEditor.EditorConfiguration
{
public EditorConfiguration(Sitecore.Data.Items.Item profile)
: base(profile)
{
}
protected override void SetupSnippets()
{
// add in all the snippets from default
base.SetupSnippets();
// load your custom snippets
Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item obj1 = master.GetItem("/sitecore/content/shared/snippets");
if (obj1 == null)
return;
foreach (Sitecore.Data.Items.Item obj2 in obj1.Children)
this.Editor.Snippets.Add(string.IsNullOrEmpty(obj2["Header"]) ? obj2.Name : obj2["Header"], Sitecore.StringUtil.RemoveLineFeeds(obj2["Value"]));
}
}
You can then set the configuration type in web.config (use a patch include file)
<!-- HTML EDITOR DEFAULT CONFIGURATION TYPE
Specifies the type responsible for setting up the rich text editor. Can be overriden at profile level. Must inherit from Sitecore.Shell.Controls.RichTextEditor.EditorConfiguration,Sitecore.Client.
Default value: Sitecore.Shell.Controls.RichTextEditor.EditorConfiguration,Sitecore.Client
-->
<setting name="HtmlEditor.DefaultConfigurationType" value="myCustomDLL.Controls.RichTextEditor.EditorConfiguration, myCustomDLL"/>
Then create your snippets in the specified directory. You may have to refresh your browser after adding the snippets since there is some caching going on in the RTE.
EDIT
As Ben rightly points out, if you are using Rich Text Default profile then setting HtmlEditor.DefaultConfigurationType in config will have no effect. The following item in the core database under the profile determines which config type to use for the 'Rich Text Default' profile for example:
/sitecore/system/Settings/Html Editor Profiles/Rich Text Default/Configuration Type
If your profile contains a child item called Configuration Type then it will use that, otherwise it will use the default specified in config. The other profiles do not contain this setting item by default. If you want the other profiles (or a custom profile) to use a specific (or different) configuration then make sure your profile contains an Item called Configuration Type of template type Html Editor Configuration Type. This could be very useful in multi-site scenarios.
Inherit Sitecore.Shell.Controls.RichTextEditor.EditorConfiguration. If you just want to add snippets to the standard snippets list, just override the SetupSnippets method and add to the Editor.Snippets collection.
If you want to add your own dropdown, it will get more complicated, but you can probably override the SetupToolbars method and add an EditorToolGroup with an EditorDropDown. You might want to look at Telerik's documentation for the RadEditor if you run into any problems.
Once you have a draft of your class written, register it by going to the profile definition in the core database under /sitecore/system/Settings/Html Editor Profile. Each profile has a Configuration Type item where you can specify the type signature of your class.