Dynamic Menu Navigation In Umbraco Using .Net User Control - menu

I am .Net developer and am looking for example related to creation of dynamic navigation menu in umbraco 7 using .Net user control without using MVC technique and XSLT. I have searched it on google but got not much response on it, examples which i got were using either XSLT or Razor. I have not any experience in MVC and XSLT and looking for technique related to using classic ASP.Net, i.e. without using XSLT and MVC. It will a great help if any one could please provide some useful videos or links with examples which i could refer to.
Thanks
Tarunjit Singh

You should really look into the Razor thing. It just TOO simple not to try!
It looks like c# mixed with some HTML. If you really want to stick to .Net UserControls you might miss out on future versions.
Creating navigation in a usercontrol is very difficult because you have 2 options
you need to write out HTML yourself
create some custom model to some 3th party control
Both of these options will never return the exact html as you wanted. Razor (and xslt) on the other hand will.
If you still want try something in a .Net User control anyway, get an instance of the Umbraco Nodes you want. Let's give an example:
using System;
using System.Linq;
using Umbraco.Web;
public partial class UserControls_TestUserControl : UmbracoUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var cache = this.UmbracoContext.ContentCache;
var rootNodes = cache.GetAtRoot().First().Children;
foreach (var node in rootNodes)
{
Response.Write("<li>" + node.Name + "</li>");
}
}
}
If you want to call this code, don't forget to add a macro, and insert the macro in your template.
Just in case you would like to explore razor, here is an equivalent:
#inherits UmbracoTemplatePage
#{
var homePage = CurrentPage.AncestorOrSelf(1);
var menuItems = homePage.Children;
}
<nav>
<ul>
<li>Home</li>
#foreach (var item in menuItems)
{
<li>#item.Name</li>
}
</ul>
</nav>
This example can be used as a partial view. And can be put in your (Razor) template using: #Html.Partial("NameOfThePartial")
You have to admit, it's not too hard.

Related

Exception trying to load tab from ajax request using Ext.NET

I'm using asp.net mvc with ext.net and I'm trying to create a set of tabs that load information only when they are selected by the user.
I can manage to load a partial view into a tab/panel using the ContentFromAction functions:
But I can't figure out how to populate a tab/panel only when a tab is selected.
I've based my project on the Ext.NET MVC Examples Explorer version 2.5 code base and this code on the TabPanel > Basic > Ajax Load example found here
I've cut down the example as far as I can to reproduce the problem:
In my view I create the tab and configure the loader (exactly the same as the example project):
Index.cshtml
X.Panel()
.ID("Tab3")
.Title("Ajax Tab")
.BodyPadding(6)
.AutoScroll(true)
.Loader(X.ComponentLoader()
.Url(Url.Action("Ajax"))
.LoadMask(m => m.ShowMask = true)
.Params(new Parameter("containerId", "Tab3"))
.Mode(LoadMode.Html)
)
It correctly calls into my controller (exactly the same as the example project):
Axax_LoadController.cs
using System.Web.Mvc;
namespace Ext.Net.MVC.Examples.Areas.TabPanel_Basic.Controllers
{
public class Ajax_LoadController : Controller
{
public ActionResult Ajax(string containerId)
{
return View("Ajax");
}
}
}
Which in turn displays the appropriate view in the tab after it's been clicked on:
Ajax.cshtml (this works)
#using Ext.Net.MVC
<div>
<p>I am content loaded via Ajax when the tab is selected</p>
</div>
The problem begins if I try to add controls in my view, as follows:
Ajax.cshtml
#using Ext.Net.MVC
#{ var X = Html.X(); }
<div>
<p>I am content loaded via Ajax when the tab is selected</p>
#X.TextField().Text("I am a text field")
</div>
This fails with the exception:
ItemTag validation (_tkn_1): Reference token (init_script) was not found.
If I modify the file Ext call to return Html as follows:
Ajax.cshtml
#using Ext.Net.MVC
#{ var X = Html.X(); }
<div>
<p>I am content loaded via Ajax when the tab is selected</p>
#X.TextField().Text("I am a text field").ToHtmlString()
</div>
It correctly renders the following text in my selected tab:
I am content loaded via Ajax when the tab is selected
<#:item ref="init_script" index="0">Ext.create("Ext.form.field.Text",{renderTo:"App.id534c5fe0f159f3fb_Container",value:"I am a text field"});</#:item><div id="App.id534c5fe0f159f3fb_Container"></div>
I believe that the ext.net code is written by #geoffrey.mcgill on stack overflow so I'm hoping he can help rescue me.
You need to use a PartialViewResult. Please look at these examples.
Partial Content
Partial Items
Personally, I would recommend to follow the Partial Items example. You always can wrap any non-Ext.NET content in an Ext.NET Container. The benefit of this approach is the fact that you don't need to worry about destroying Ext.NET components if you reload the content. Though, anyway, I would recommend to set up explicit IDs for Ext.NET components in a partial view. At least, for top level components.

Orchard query content items in my custom controller

The title pretty much sums it up. I created my own module in Orchard. I can access its actions via http requests like an ordinary controller in any MVC application.
I generated the controller using the command line interface and it came with IOrchardServices property that is populated in the constructor.
On my Orchard site I have a blog which I filled up with about 40 blog posts.
How can I query these blog posts from within my controller?
First I would like to start by saying: "read the source luke". You may find Orchard lacking in documentation and examples, but because it is open source, pretty much everything you want to know can be found there.
You should use the BlogPostService, inject that into your controller to get the blog posts you want.
https://orchard.codeplex.com/SourceControl/latest#src/Orchard.Web/Modules/Orchard.Blogs/Services/BlogPostService.cs
You can see it being used in several controllers within Orchard.Blogs:
https://orchard.codeplex.com/SourceControl/latest#src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogPostController.cs
https://orchard.codeplex.com/SourceControl/latest#src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogAdminController.cs
Check out the code in the BlogPostService to see how it works, it is a little confusing because blogs are content items with blog posts underneath them. If you want to learn about simpler querying of content items I would check out how the BlogService works, it is a little easier to get to grips with:
https://orchard.codeplex.com/SourceControl/latest#src/Orchard.Web/Modules/Orchard.Blogs/Services/BlogService.cs
Here is an example of what I was looking for:
var query = Services.ContentManager.Query();
var list = query.ForType(new []{"BlogPost"}).List();
var result = new List<dynamic>();
foreach (var contentItem in list) {
result.Add(new
{
title = contentItem.As<TitlePart>().Title, // dynamically typed: ((dynamic)contentItem).TitlePart.Title
text = contentItem.As<BodyPart>().Text
});
}

Formatting options for Razor (cshtml) and C# (cs) files

Generally in my company we use MS C# Coding Conventions for C# code (.cs files). So, typically, our code looks more or less like this:
public class FooController
{
public ActionResult Index()
{
for (int i = 0; i < 10; i++)
{
// Code here
}
if (true == false)
{
// More code here
}
return View();
}
}
Which looks nice and readable.
But in my team, the designers (who usually work only on Razor cshtml files) use a different formatting in their code. More or less like this:
#using (Html.BeginForm()) {
#if (true == false) {
<p>You'll never see this!</p>
}
}
They generally open curly braces in the same line and have other formatting options because they only use cshtml (Razor) files. (They do other tricks as well so the source code looks similar to generated HTML code.)
We have all agreed on that and it's cool.
The problem starts when I or some other developer edits part of the Razor view and Visual Studio re-formats the code using our (C#/cs file) settings. This is not desirable.
The question is:
Is there a way to have different formatting options for .cs and .cshtml files, or to stop Visual Studio from formatting Razor cshtml files specifically?
Any help will be kindly appreciated.
Visual Studio does not have such options.
I'm using Telerik's JustCode. It has different formatting settings for C# and Razor. It also has some other useful for developers stuff.

Liferay - Getting localized name in javascript

Consider the following code
function populateLayout(scopeGroupId){
Liferay.Service.Portal.Layout.getLayouts(
{
groupId: scopeGroupId,
privateLayout: false
},
function(layouts){
for(var i=0;i<layouts.length;i++){
var layout = layouts[i];
alert(layout.name);
}
}
);
}
As you can see from the above Liferay's JSON service API to get all the layouts. I particularly need layout name to populate in a select box. I know that the name is stored as xml string to support different locales. I was wondering if there is javascript api to get just the name of the layout using this xml string and language id. There is a java api for doing the same as below. I need equivalent javascript API if any.
layout.getName(locale)
or
LocalizationUtil.getLocalization(String xml, String languageId)
I believe there is no such utility on Liferay's JavaScript libs.
Most of the times people need to do it in Liferay, they just parse the content in the server side and send the parsed content to the client. Frequently, the client only needs the value in the default locale; if it needs more, the developer finds a way to write it in HTML itself. See e.g. the input-localized tag where the developer writes the code to create a JS object directly in the output.
This makes sense: parsing XML is resource-costly so one would rather avoid it on client-side JavaScript. It may be even that there is a utility to do that in Liferay but I'd propose an alternative approach: just ask what you want to use in your JavaScript and write it in the HTML. For example, instead of writing the following JavaScript code
var names = Liferay.Util.Localization.nonExistentMethodToGetLocaleMap(layout.name);
for (var locale : names) {
alert(names[locale]);
}
decide in server-side which layouts to display and write the following JSP code:
Map<Locale, String> names = layout.getNameMap();
<script>
<% for (String name : names.values()) { %>
alert('<%= name %>');
<% } %>
</script>
Not much clear, I agree, but rather efficient.

Can I Do This In Grails 2: Secure Individual Tabs with Spring Security

If I create tabs using one of the Grails GUI options (which one should I use), is it possible to turn tabs on and off, based on the current user? For example only users with a role of admin should see the Manage Users tab. And even anonymous users should see the Main Content tab.
Ideally, I'd like to use Spring Security ACL.
Just to add, sometimes you can add too much logic and coding in the view (GSP). You can push more of the code to the controller by using other options such as the navigation plugin along with the Spring-Security plugin. The nice thing is the view is just cleaner removing condition tags.
grails install-plugin navigation
Then in the controller just use the #Secured annotation. For example I created two tabs with two corresponding controllers.
#Secured(['ROLE_ADMIN'])
class SlidesController {
static navigation = [
group:'tabs', order:10, title:'Users', action:'index'
]
def index = {
.....
}
#Secured(['ROLE_ADMIN'])
class ProgramsController {
static navigation = [
group:'tabs', order:10, title:'Programs & Presentation', action:'index'
]
def index = {
.....
}
In the view:
<head>
... other head elems.
<nav:resources/>
</head>
<body>
<nav:render/>
... Your other stuff
</body>
Tabs automatically appears (would be also useful to make this view a layout GSP).
Yes, it's possible and pretty easy with the Acegi (Spring Security Plugin), see this section of the docs. You'll need to define some roles, and then describe how those roles apply to different URLS. Example (from link above):
/admin/=ROLE_USER
/book/test/=IS_AUTHENTICATED_FULLY
/book/**=ROLE_SUPERVISOR
There is also explanation about exactly how to do this in Grails In Action book.
Spring Security has a nice tag library for this. You can delimit rendering the tabs by using something similar to this:
<sec:authorize access="hasRole('supervisor')">
This content will only be visible to users who have
the "supervisor" authority in their list of <tt>GrantedAuthority</tt>s.
</sec:authorize>
You can just enable the jsp tag as normal:
<%# taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
Read more about it here if you like

Resources