render sitecore mvc renderings programmatically - sitecore

I want to get all the renderings of a content item and render each of them to html string inside an MVC action using C# code. Below is the code I am using to get all the renderings of a content item.
Item item = Sitecore.Context.Database.GetItem(someItem);
RenderingReference[] myRenderings = item.Visualization.GetRenderings(Sitecore.Context.Device, true);
foreach (RenderingReference rendering in myRenderings)
{
RenderingItem renderingItem = rendering.RenderingItem;
}
I am able to get this list and the rendering item's Id. But how can i render them to html string here?
Note: the renderings could be of any rendering type like view renderings, xsl renderings , controller renderings etc. I dont want to use the approach of WebClient or HtmlAgility pack.

Not sure what you really try to do, but this code allows you to render a rendering in a MVC action and returns the Html for this:
public ActionResult GetFirstRenderingHtml()
{
var rendering = PageContext.Current.PageDefinition.Renderings.First();
return this.View(new RenderingView(rendering));
}
This actually only returns the code of the first rendering of the current item. If you want to call this action directly, you need to add the sc_itemid parameter in the to specify the context item:
/api/yourcontroller/GetFirstRenderingHtml?sc_itemid=<item id>
If you want the Html for a complete item with all it's renderings, I suggest you create a web request to get the output.

Related

How to make a NameValueCollection list editable in GlassMapper

I am trying to make a NameValueList collection editable with GlassMapper and I don't seem to be able to get to the bottom of this.
We have a list of validations that can be attached to a field and I would like to have the validation message editable in ExperienceEditor.
The collection is pre-processed when GlassMapper is retrieving the item:
Validations = glassItem.GetValidations();
#foreach(Validation validation in Model.Validations)
{
<div id="#validation.Identifier" ng-message="#validation.AngularKey" ng-cloak class="mtg-validation-msg">
#Html.Glass().Editable(validation, e => e.ErrorMessage)
</div>
}
Error that I am getting:
Failed item resolve - You cannot save a class that does not contain a property that represents the item ID. Ensure that at least one property has been marked to contain the Sitecore ID. Type: MyAssembly.Models.Validation
It is not possible to directly edit certain types of complex fields in the Experience Editor, such as Treelist, Multilist or Name Value Collection.
Instead, you should set up and use an Edit Frame. This will pop up a modal dialog allowing you to edit the field, it is not inline but means you do not need to leave the Experience Editor. This is the recommended approach to this problem.
Since you are using Glass Mapper, since version 4 you can declare Edit Frames all directly from code and now have to declare/set them up in the Core database first.
#if (Sitecore.Context.PageMode.IsExperienceEditor)
{
using (Html.Glass().BeginEditFrame(Model, "Edit", x => x.Validations))
{
<div>Edit Validations</div>
}
}
You might be interested in this blog post I wrote about adding a wrapper around the Edit Frame to make the UX more friendly.

Sitecore Rich Text links not user friendly when rendered through Glass

I have a component that includes a single Rich Text field. In the sublayout, the field is rendered as an Html.Editable with Glass so that it can be edited on the page in PageEditor. It looks like this:
public override void Initialize()
{
litBodyContent.Text = Html.Editable(GlassItem, item => item.Body);
}
<div style="min-height: 38px">
<asp:Literal runat="server" ID="litBodyContent" />
</div>
However, when I insert links using the Rich Text editor, when the page is rendered (in normal view after being published, not in page editor), the links are rendered with the item ID rather than the user friendly path, like this:
Go to another page
I am pretty sure that this is an issue with Glass. How can I keep the field editable in the Page Editor but make it render the link correctly?
You can check if you have proper attribute in the model.
If you have SitecoreFieldSettings.RichTextRaw attribute, it will NOT pass through render pipeline and returns the RAW HTML. Instead if you use Default (SitecoreFieldSettings.Default), rich text content will go through render pipeline and url will be in friendly format.
http://docs.glass.lu/html/d44739b2-5f0e-d862-f62c-86c50f0b262f.htm
Can you try to change it from using literal to use Editable() method directly, Something like:
<%= Editable(GlassItem, x => x.Body) %>
I think Initialize() is too soon in the page life cycle. Try moving it further along, like to Page_Load() or so.

Not able to make the navbar items dynamic in ember

I have navbar component which I have placed in an application.hbs so that it should be visible always.But I want to change the title of the navbar with each page I visit ( say I am on index page it should say "Home" , if I am on profile page it should say profile,etc).Right now what is happening is navbar title always remains "Home" for all the page.This is happening because the navbar gets rendered onlu during the time it loads the page in browser and after that it doesn't change according to page.
application.hbs
{{top-navbar dp_url=model.profile.dp_url first_name=model.profile.first_name title=title}}
{{outlet}}
Here the I am computing the value of title depending upon which page the user is.
application.js(controller)
if (currentPage === "" || currentPage === "#"){
currentState.set('title',"Home")
}
else if(currentPage === "Userprofile"){
console.log('myStudio');
currentState.set('title',"UserProfile");
}
In here the currentpage has the current url of app and I am comparing it and deciding the value of title for navbar.
But the top-navbar title value gets calculated only for the first time when user load the app in browser and not when I move ffrom one route to another.
I have also tried the Ember.Evented but not able to solve it.
I don't know what Ember.js and Ember-data version are you using, currentState is deprecated since 2.1, anyway looks like you're using a private method intended for Ember internals and not meant to be used in an application.
A possible (but maybe unnecessary complicated) way to accomplish what you want is:
Create a model with the information you want to mutate in the navbar (e.g. navbar-data).
In the route where the navbar is rendered, create and return a record for it using a fixed numeric ID (e.g. store.createRecord('navbar-data', { id: 1, title: "index" })).
Pass the created record to the component (instead of just a string).
Whenever you want to change, peek the record with store.peekRecord('navbar-data', 1) and change the value you want to change in the navbar.
Of course, the record you use for that must not be saved with record.save().

Pass default image to Sitecore MVC Field helper

Anyone know how I can specify the default editor image that shows when rendering a sitecore Image field?
#Html.Sitecore().Field("Image", new { #class = "full" })
In Page Editor, before the author as set the image, I get the ugly default sitecore image. I want to specify my own for different cases. Is there a way to pass the default image to use the Field helper?
The default image is defined on the following item in the core database, which you can change.
Alternatively, you can subclass Sitecore's default image rendered (Sitecore.Xml.Xsl.ImageRenderer), and override the GetDefaultImage() method.
Another less fragile solution is to place an Edit Frame around the image. This allows you to retain the Page Editor functionality that you get from #Html.Sitecore().Field() helper.
This example assumes the use of the Glass Mapper, however, you should be able to adapt it to use the default SitecoreHelper.
#using (Html.BeginEditFrame(Model.Id, new ID(Constants.ItemIDs.EditFrameButtons.Image)))
{
if (Model.Image != null && !String.IsNullOrEmpty(Model.Image.Src))
{
// Render image here
}
else
{
<img src="//placehold.it/400x225/2379cf/fff" alt="placeholder_image" />
}
}

Dynamically create view

I'm generating a list of views that the client should cycle through on the server. The server returns a list of something like 'App.AView', 'App.BView', 'App.CView", ..., etc. that refer to views and templates on the client.
I'd like to dynamically create these views, swap out the previous view, and include the new view. My first though was to compile a handlebars template with this dynamic view name, ala:
App.QuestionView = Em.View.extend({
template: function() {
return Ember.Handlebars.compile("{{view " + this.get("view_name') + "}}");
}
});
Which works, but seems ugly - is there a way to create a view with a string of the view name in Ember.JS and replace an existing view in a parent view with that view?
You want to look at Ember.ContainerView, which will let you programmatically manage a set of child views. Container View's documentation is excellent, check out: http://emberjs.com/api/classes/Ember.ContainerView.html
Since it sounds like you only ever want one view displayed at a time you can focus on the currentView property, which will automatically maintain your childViews array for displaying a single view.
Also here is an example: http://www.emberplay.com/#/workspace/2792969430