How to include/refer a dynamic component template in the page. I had created a dynamic CT and published it, but want to render its presentation in the page.
Please suggest.
Thanks in advance,
There are many ways to add dynamic presentation on the page.
Direct Approach - For this your component presentation should be allowed to on page. Check Allow on Page Using Dynamic Assembly. Add the presentation on page same as all others.
Code Approach - You may use API to get your component presentation direct from the broker storage.
Here is sample code for the same.
*<%# Import Namespace="Tridion.ContentDelivery.DynamicContent"%>
<%
ComponentPresentationFactory factory = new ComponentPresentationFactory();
ComponentPresentation ps = factory.getComponentPresentation("CompID","TEMPLATEID");
Response.Write(ps.Content);
%>
JSP example:
<%# page import="com.tridion.dynamiccontent" %>
<%
ComponentPresentationFactory cpf = new ComponentPresentationFactory("tcm:0-1-1"); // Publication URI
// Component URI and Component Template URI
ComponentPresentation componentPresentation = cpf.getComponentPresentation("CompID", "TEMPLATEID");
out.println(componentPresentation.getContent());
%>
c#
ComponentPresentationFactory cp_factory = new ComponentPresentationFactory(publicationid);
ComponentPresentation cp = cp_factory.GetComponentPresentation(CompID, TEMPLATEID);
if (cp != null)
{
Output = cp.Content;
}
There is multiple ways to show a Dynamic Presentation on a page:
the most simple one is marking your Dynamic CT as "Allow on Page Using Dynamic Assembly", then you will be able to embed it on your page as a Non-Dynamic CT
For more advanced alternatives, you can check the Live Content Documentation:
Implementing Content Delivery > Developing Web Pages
Related
Overview
We have an in house CMS that we've recently added multilingual support to. The CMS allows dragging/dropping of various panels (.net controls) and some panels show dynamic content entered via a rich text editor. Also, some fields are multilingual so some panel content will change according to the current language.
Ideally we want to add the language to the URL. So /contact-us becomes /en/contact-us.
Our main handler will then set the language and the all panels will show relevant copy.
Goal
So, ideally we'd like to be able to:
Process the page server side after it's been built by our main page assembler (eg in PreRender)
Parse the built page or recurse the control tree to update ALL internal links
Prepend a lanauge code to all internal links on the page. (easy enough once we know where they all are)
NB: Some links will by in .net HyperLink controls but others will be <a> tags entered via a Rich Text Editor.
Stuff I've looked at
I've skimmed google but haven't found anything that seems to match our needs:
Html Agility Pack - can be used to take a URL and parse for links. But I'm guessing this can't be used say in Pre_Render of our main page builder. Ideal for scraping I suppose.
Various JS solutions - locate links and update. Very easy but I'm wary of using JS for updating URLs client side.
All suggestions welcome :)
So, There will be dynamic content and static content. And the CMS users should be able to edit both of them. You should have a Language DB table, and for instance; For "about us" page, There should be about-us EN, about-us DE, about-us FR rows in other table.
And you should have another table for static content. For instance for contacy us form. There are static texts on contact forms. Name, e-mail,message etc.
This can be done by overriding Page.Render() as follows:
protected override void Render(HtmlTextWriter htmlWriter)
{
StringBuilder ThisSB = new StringBuilder();
StringWriter ThisSW = new StringWriter(ThisSB);
HtmlTextWriter RenderedPage = new HtmlTextWriter(ThisSW);
// pass our writer to base.Render to generate page output
base.Render(RenderedPage);
// get rendered page as a string
string PageResult = ThisSB.ToString();
// modify the page
string ModifiedPage = UpdatePage(PageResult);
// write modified page to client
htmlWriter.Write(ModifiedPage);
}
The method UpdatePage can manipulate the page as a string in any way you wish - in our case we use find and update all links and local file paths.
I'm converting an MVC aspx content place holder -> master page to an MVC razor section -> layout.
In the past when my aspx view came to something like this:
<asp:Content ID="HelpContent" ContentPlaceHolderID="HelpLink" runat="server">
Help
</asp:Content>
And the master page didn't have a corresponding HelpContent place holder (perhaps because a user was not authenticated) everything rendered fine (with no HelpContent section).
Now when I have a razor section defined that does not have a corresponding #RenderSection in the layout, I get this error:
The following sections have been defined but have not been rendered
for the layout page "~/Views/Shared/New.cshtml": "HelpLink".
Do I need to redesign this?
Is there a way I can have the view's HelpLink section render optionally if the layout gives it the green light?
EDIT:
I think there's some confusion, so let me re-summarize:
The layout logic looks like this:
if (User.IsLoggedIn) {
#RenderSection( "HelpLinks", false);
}
But then the user isn't logged in, it doesn't render, and then .NET throws an exception because the layout doesn't know what to do with the section.
You can indicate that the section is optional by passing false as the second argument:
#RenderSection("HelpLink", false);
Edit: In the case of control flow logic for rendering, you can use .NET in the razor view (like this c# example):
#if(IsSectionDefined("HelpLink"))
{
#RenderSection("HelpLink", false);
}
Or, if you want to base rendering on whether the user is logged in, you could replace the if logic in the above sample with your security check.
Edit 2:
Make sure you have defined the section:
#section HelpLink {
//This needs to be defined in any view that uses the layout with the #RenderSection. It can be empty.
}
Alternatively, you can add the check to see if the section exists and only define the #section in the required view:
if (IsSectionDefined("HelpLink") && User.IsLoggedIn) {
#RenderSection( "HelpLinks", false);
}
If a section is declared in a razor view it has to be rendered in the layout.
I found this in Freeman's Pro ASP.NET MVC 5 book.
Seems like a bad design to me.
I am trying to include different scripts on different pages in Sitecore and can't seem to find a very good way of doing this. In a normal mvc project I could add the #Section{} helper and use that on different partial views to specify where I want those scripts in the layout view, but I haven't been able to find an equivalent for the way that Razor helper is implemented with Sitecore. I'd like to do this without using a place holder, I don't want to add a view in Sitecore every time I need to add a script file.
Thanks in advance.
I'm afraid you're out of luck here.
#Section is not supported because Sitecore doesn't render the Razor views in the same way as MVC does.
A Sitecore MVC layout is basically just a regular view that is rendering several other partial views or controller actions.
So when the placeholders in the <body> of your layout view are being rendered, the <head> section of that layout has already been rendered.
There is no such thing as deferred rendering in Sitecore MVC like you can do with #Section.
Everything in the view is executed from top to bottom, so if you can put your scripts at the end of your layout (like before the </body>), you can still manipulate data in the views or actions that are executed earlier.
The way I have it setup in my current Sitecore MVC solution is my layout has an extension method call to RenderScripts() at the bottom before the closing body tag.
#Html.RenderScripts()
That extension method looks like this:
public static IHtmlString RenderScripts(this HtmlHelper htmlHelper)
{
var templates = (from object key in htmlHelper.ViewContext.HttpContext.Items.Keys
where key.ToString().StartsWith("_script_")
select htmlHelper.ViewContext.HttpContext.Items[key]).OfType<Func<object, HelperResult>>()
.Select(template => template(null)).ToList();
foreach (var template in templates)
{
htmlHelper.ViewContext.Writer.Write(template);
}
return MvcHtmlString.Empty;
}
Then on each MVC Razor View when I want to include a .js file that is specific to that rendering I call something like below at the bottom of the file:
#Html.Script(
#<script src="#Url.Content("~/js/custom/orderdetail.js?t=11172015")" type="text/javascript"></script>
)
Below is the Script extension method:
public static MvcHtmlString Script(this HtmlHelper htmlHelper, Func<object, HelperResult> template)
{
htmlHelper.ViewContext.HttpContext.Items["_script_" + Guid.NewGuid()] = template;
return MvcHtmlString.Empty;
}
This has worked out well for us and I think it is what you are trying to do.
Indeed, the Section-helper isn't supported in Sitecore. If you're using MVC4 you can maybe use Bundles to solve your problem. For more information see: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
You can also create multiple bundles for specific views. In a single Bundle you can add multiple script and output it in your view by adding #Scripts.Render()
I have performance issue on website, because of too long viewstate in source code. on some pages it's size is more than 15-20kb. Which is increasing the load time on browser.
Is there any way to disable viewstate partially or fully without any harm on other module of website. FYI there is one listview and one form on these example pages. where viewstate is very long.
More Example pages
http://www.pricingindia.in/coupons/ebay-in-coupon-codes-20
http://www.pricingindia.in/coupons/flipkart-coupon-codes-32
You probably want to disable for ViewState for your Page in general, and then only enable ViewState for the controls that need / use it.
See this MSDN page on the Control.ViewStateMode property, it describes how to set that up:
To disable view state for a page and to enable it for a specific
control on the page, set the EnableViewState property of the page and
the control to true, set the ViewStateMode property of the page to
Disabled, and set the ViewStateMode property of the control to
Enabled.
You will need to do some testing to see which controls need / use the ViewState in your specific app. But, basically,
anything that's static, you can disable the ViewState (Buttons, LinkButtons, etc).
Any controls whose state doesn't need to be restored between PostBacks, you can disable ViewState (such as a TextBox in a form that is submitted to the server, and then cleared).
Any controls that need to keep their state between PostBacks, you want to enable Viewstate (this would often be databound controls like GridViews / etc).
Doing this should definitely reduce the load that ViewState is putting on your pages.
Okay, I couldn't leave well enough alone:
Firebug said the following bit of code blocked the site from rendering for about 6 seconds. I thought it was the jsapi (based on info from chrome's tools) but the following returned a "502 Bad Gateway" message meaning that it sat there spinning it's wheels unable to process while preventing your web page from displaying.
I would move the <script .. call to the header, where it belongs. Then I'd move the google.load and google.setOnLoadCallback to the bottom of the web page so it runs last.
Finally I'd figure out exactly why it's failing to work right.
Homework for you: get Firebug loaded into firefox and learn how to use it's Net tools to see where site loading issues are.
<div class="sr_bx1 FL clearfix">
<div class="FL searchbg">
<div id='cse' style='width: 100%;'>Loading</div>
<script src='http://www.google.com/jsapi' type='text/javascript'></script>
<script type='text/javascript'>
google.load('search', '1', { language: 'en', style: google.loader.themes.V2_DEFAULT });
google.setOnLoadCallback(function () {
var customSearchOptions = {};
var orderByOptions = {};
orderByOptions['keys'] = [{ label: 'Relevance', key: '' }, { label: 'Date', key: 'date'}];
customSearchOptions['enableOrderBy'] = true;
customSearchOptions['orderByOptions'] = orderByOptions;
var imageSearchOptions = {};
imageSearchOptions['layout'] = 'google.search.ImageSearch.LAYOUT_POPUP';
customSearchOptions['enableImageSearch'] = true;
customSearchOptions['overlayResults'] = true;
var customSearchControl = new google.search.CustomSearchControl('010882286766777081969:xkox132izzk', customSearchOptions);
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
var options = new google.search.DrawOptions();
options.setAutoComplete(true);
customSearchControl.draw('cse', options);
}, true);
</script>
</div>
Using Page Adapter Class, you can solve this problem. I found following solution helpful for ASP.NET Web Application:
view state serialization is handled by an object of type PageStatePersister. (The PageStatePersister class is an abstract class that defines the base-level functionality for serializing view state to some persistent medium.)
Steps:
1) In Your Web Application, Add Folder: App_Browsers
2) In this folder, Add new BrowserFile: ViewStateAdapter.browser
3) Write following code in it:
<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.Page" adapterType="ServerSideViewStateAdapter" />
</controlAdapters>
</browser>
</browsers>
4) In Your WebApplication, Add Folder: App_Code
5) In this folder, Add new ClassFile: ServerSideViewStateAdapter.cs
6) Write following code in it:
using System;
using System.Web.UI;
using System.Web.UI.Adapters;
public class ServerSideViewStateAdapter : PageAdapter
{
public override System.Web.UI.PageStatePersister GetStatePersister()
{
return new SessionPageStatePersister(this.Page);
}
}
You can refer this video, it can help you:
https://www.youtube.com/watch?v=36pmFySbXZA
I'm newbie to silverlight , is it possible to submit a from from with in the silver light application . I want to use silverlight controls instaeda of asp.net , they look far better than asp.net controls .How to do this in silverlight 5.
I'm sure you have got your answer by now, but for later readers, I found this way the best way to do this in Silverlight. this way you don't need any aspx pages and you can create and submit html pages directly from silverlight.
it will use Silverlight browser interop to programmatically create an HTML form and set elements to it.
//Creates a blank html document
var htmldoc = System.Windows.Browser.HtmlPage.Document;
// Returns a Reference type to the body of html page
var body = htmldoc.Body;
// Create a <form> element and add it to the body
var newForm = htmldoc.CreateElement("form");
newForm.SetAttribute("action", targetUrl);
newForm.SetAttribute("method", "post");
body.AppendChild(newForm);
//Add your elements to your form
HtmlElement input1 = htmldoc.CreateElement("input");
input1.SetAttribute("type", "hidden");
input1.SetAttribute("name", "someName");
input1.SetAttribute("value", "someValue");
newForm.AppendChild(input1);
//submit your form
newForm.Invoke("submit");
That Simple!
original Answer: This Answer
You can call form.submit() via a javascript function called from a silverlight control
http://msdn.microsoft.com/en-us/library/cc221359%28v=VS.95%29.aspx
and you can also navigate: HtmlPage.Window.Navigate(url)