Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Inna 4 posts 24 karma points
    Jan 28, 2012 @ 13:38
    Inna
    0

    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.

  • Tinminator 2 posts 22 karma points
    Feb 28, 2012 @ 00:32
  • Thomas Sandvær 17 posts 37 karma points
    May 10, 2012 @ 00:24
    Thomas Sandvær
    0

    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

  • Bryan Drenner 37 posts 59 karma points
    May 31, 2012 @ 01:02
    Bryan Drenner
    0

    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();
            }
        }
    }
  • Thomas Sandvær 17 posts 37 karma points
    Jun 16, 2012 @ 00:21
    Thomas Sandvær
    0

     

     

    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 { getset; }
        }
    }

     

     

Please Sign in or register to post replies

Write your reply to:

Draft