Hi, I'm new in developing with Umbraco V5. I can't figure out how to get content node in my Surface Controller. For example i have product nodes and i want to get all names of products.
Hi Inna, ive been working on our company site a while, and I just accomplished what youre asking for yesterday, did you find an answer? if you reply i will gladly post my code for it :-)
Although I'd prefer to find a simpler way, I think I've found a workable solution. The following is a custom base class that exposes a single "CurrentContent" property, which can then be used by inheriting surface controllers.
using Umbraco.Cms.Web;
using Umbraco.Cms.Web.Surface;
using System.Web.Mvc;
using Umbraco.Cms.Web.Model;
namespace Whatever
{
public abstract class BaseSurfaceController : SurfaceController
{
private object m_currentContent = null;
public dynamic CurrentContent
{
get
{
if (m_currentContent == null)
{
if (Request.HttpMethod == "POST")
{
m_currentContent = GetContentForSubmitAction();
}
else
{
m_currentContent = GetContentForChildAction();
}
}
return m_currentContent;
}
}
// from Lee Gunn's response
// http://our.umbraco.org/forum/core/umbraco-5-general-discussion/29178-In-a-controller-how-do-I-get-the-current-pages-hiveId?p=2
private object GetContentForChildAction()
{
ViewContext vc = ControllerContext.RouteData.DataTokens[
"ParentActionViewContext"] as ViewContext;
var content = vc.ViewData.Model
as global::Umbraco.Cms.Web.Model.Content;
return content.AsDynamic();
}
// from Nicholas Ruiz
// http://our.umbraco.org/forum/core/umbraco-5-general-discussion/30928-Surface-Controller-and-Current-Node-Properties
private object GetContentForSubmitAction()
{
UmbracoRenderModel rm =
ControllerContext.RouteData.DataTokens["umbraco"] as UmbracoRenderModel;
if (rm == null)
{
return GetContentForChildAction();
}
return rm.CurrentNode.AsDynamic();
}
}
}
Im not sure all the using statements are needed but i have a rather large controller so bare with me : )
As you see i query the hive for a specific documentType. Fetch the field values from the content and add them to a wrapper object.
This collection is then passed to the view as the model.
In the view you iterate the collection with razor as you would in any case.
Hope this helps anybody : )
using System; using System.Collections.Generic; using System.Web; using System.Web.Mvc; using Umbraco.Cms.Web.Context; using Umbraco.Cms.Web.Surface; using Umbraco.Framework; using Umbraco.Cms.Web.Mvc; using Umbraco.Cms.Web; using Umbraco.Hive; using Autofac; using Umbraco.Framework.Persistence; using Umbraco.Cms.Web.Model; using Umbraco.Framework.Persistence.Model.Attribution; using Umbraco.Cms.Web.Configuration; using System.Configuration; using Microsoft.Web.Mvc;
publicvoid Query() { //Get all content with the alias "documentTypeAlias" var all = Hive.QueryContent().Where(x => x.Id != HiveId.Empty && x.ContentType.Alias == "documentTypeAlias");
foreach (Content content in all ) { WrapperObject wo = newWrapperObject(); wo.MyAttribute = (content.Field("fieldName") == null ? "" : content.Field("fieldName").ToString()); myCollection.Add(wo); } }
Get content in Umbraco v5 SurfaceController
Hi, I'm new in developing with Umbraco V5. I can't figure out how to get content node in my Surface Controller. For example i have product nodes and i want to get all names of products.
Hope this post helps you as it helped me:
http://our.umbraco.org/forum/core/umbraco-5-general-discussion/28459-How-to-move-this-code-into-a-Surface-Controller
Hi Inna, ive been working on our company site a while, and I just accomplished what youre asking for yesterday, did you find an answer? if you reply i will gladly post my code for it :-)
Thomas
Although I'd prefer to find a simpler way, I think I've found a workable solution. The following is a custom base class that exposes a single "CurrentContent" property, which can then be used by inheriting surface controllers.
IMO i found i simpler way to accomplish this.
Im not sure all the using statements are needed but i have a rather large controller so bare with me : )
As you see i query the hive for a specific documentType. Fetch the field values from the content and add them to a wrapper object.
This collection is then passed to the view as the model.
In the view you iterate the collection with razor as you would in any case.
Hope this helps anybody : )
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using Umbraco.Cms.Web.Context;
using Umbraco.Cms.Web.Surface;
using Umbraco.Framework;
using Umbraco.Cms.Web.Mvc;
using Umbraco.Cms.Web;
using Umbraco.Hive;
using Autofac;
using Umbraco.Framework.Persistence;
using Umbraco.Cms.Web.Model;
using Umbraco.Framework.Persistence.Model.Attribution;
using Umbraco.Cms.Web.Configuration;
using System.Configuration;
using Microsoft.Web.Mvc;
namespace Umbraco.Cms.Web.UI.Controllers
{
public class MySurfaceController : SurfaceController
{
private List<WrapperObject> myCollection = new List<WrapperObject>();
private IHiveManager Hive;
//leave default constructor its needed
public MySurfaceController()
{
}
public MySurfaceController(IRoutableRequestContext requestContext): base(requestContext)
{
var context = requestContext;
Hive = context.Application.Hive;
MyCollection.Clear();
Query();
}
[ChildActionOnly]
public PartialViewResult MyView()
{
return PartialView("Partials/MyView", myCollection);
}
public void Query()
{
//Get all content with the alias "documentTypeAlias"
var all = Hive.QueryContent().Where(x => x.Id != HiveId.Empty && x.ContentType.Alias == "documentTypeAlias");
foreach (Content content in all )
{
WrapperObject wo = new WrapperObject();
wo.MyAttribute = (content.Field("fieldName") == null ? "" : content.Field("fieldName").ToString());
myCollection.Add(wo);
}
}
public class WrapperObject
{
public string MyAttribute { get; set; }
}
}
is working on a reply...