Copied to clipboard

Flag this post as spam?

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


  • Lee Cook 28 posts 60 karma points
    Feb 25, 2012 @ 18:23
    Lee Cook
    1

    Getting data from umbraco inside a controller

    HI,

    I managed to get get data from umbraco using hive inside a razor view, but I couldn't work out how to do it in a controller. Is that the only way do it? If so, it seems counter intuitive and not very testable. I would expect to be able to constuct a Hive object via injecting stufff into the constructor of my controller.

     

    Anyone managed to do this?

     

    Thanks,


    Lee

  • Lee Cook 28 posts 60 karma points
    Feb 25, 2012 @ 18:51
    Lee Cook
    0

    I found this on the wiki: http://jupiter.umbraco.org/Data-Access-in-Umbraco-5.ashx?HL=hive

    var mappingGroup = context.Hive.GetProviderMapForRoute("/news/archive/my-news-article"); // First get the providers for the current route
    using (var unit = _mappingGroup.CreateReadOnlyUnitOfWork()) // Get a unit of work for the group
    {
        Assert.IsNotNull(unit.ReadRepository.GetEntityByRoute("/news/archive/my-news-article")); // Retrieve the entity by its route
    }
    How do I get the context object though?
  • Lee Cook 28 posts 60 karma points
    Feb 25, 2012 @ 19:01
    Lee Cook
    3

    Ah ha! Getting closer. If you are using a surface controller, you can do this:

    public class WordSurfaceController : SurfaceController {

    public ActionResult Index() {

    var context = RoutableRequestContext.Application.Hive.Context;

     

    var mappingGroup = context.Hive.GetProviderMapForRoute("/news/archive/my-news-article"); // First get the providers for the current route
    using (var unit = _mappingGroup.CreateReadOnlyUnitOfWork()) // Get a unit of work for the group
    {
        Assert.IsNotNull(unit.ReadRepository.GetEntityByRoute("/news/archive/my-news-article")); // Retrieve the entity by its route
    }

    return PartialView(); } }

     

    BOOM

  • Lee Cook 28 posts 60 karma points
    Feb 25, 2012 @ 19:46
    Lee Cook
    1

    using System;

    using System.Collections.Generic;

    using System.Web.Mvc;

    using Umbraco.Cms.Web;

    using Umbraco.Cms.Web.Surface;

    using Umbraco.Framework;

    using Umbraco.Hive;

    using Umbraco.Cms.Web.Model;

    using System.Linq;

    List items = RoutableRequestContext.Application.Hive.QueryContent().Where(x => x.ContentType.Alias == "word").ToList();

     

    This looks to be about right, cant confirm atm as its not hitting my controller for some wierd reason.

  • kristian schneider 190 posts 351 karma points
    Feb 25, 2012 @ 20:07
    kristian schneider
    0

    Hi Lee.

    Could you perhaps include your entire code for the controller?

    When I try to use you example I don't have Hive object on the context variable so I won't compile.

    Could be that you include somthing that I've missed

    /Kristian

  • Lee Cook 28 posts 60 karma points
    Feb 25, 2012 @ 20:23
    Lee Cook
    1
    1. using System.Web.Mvc;
    2. using Umbraco.Cms.Web;
    3. using Umbraco.Cms.Web.Surface;
    4. using Umbraco.Framework;
    5. using Umbraco.Hive;
    6. using Umbraco.Cms.Web.Model;
    7. using System.Linq;
    8. namespace UmbracoHackday.Web
    9. {
    10.     /// <summary>
    11.     /// Summary description for HelicopterController
    12.     /// </summary>
    13.     public class HelicopterController : SurfaceController
    14.     {
    15.         public ActionResult Index()
    16.         {
    17.             List<Content> items = RoutableRequestContext.Application.Hive.QueryContent().Where(x => x.ContentType.Alias == "word").ToList();
    18.  
    19.             return View();
    20.         }
    21.      
    22.     }
    23. }
  • Lee Cook 28 posts 60 karma points
    Feb 25, 2012 @ 22:07
  • kristian schneider 190 posts 351 karma points
    Feb 26, 2012 @ 13:36
    kristian schneider
    0

    Hm.. I don't get any items back from the query.

    The  x.ContentType.Alias == "word" is that the alias of the document property ?

    I am talking about this:

     

  • Lee Cook 28 posts 60 karma points
    Feb 26, 2012 @ 14:00
    Lee Cook
    1

    That is what I would expect. Finally got mine to work. This is exactly what I have:

    public class HelicopterController : UmbracoController

    {

    public override ActionResult Index(IUmbracoRenderModel model) {

    List items = RoutableRequestContext.Application.Hive.QueryContent().Where(x => x.ContentType.Alias == "word").ToList();

    return View();

    }

    }

     

    Not sure if I have to use UmbracoController, but it works this way.

  • Ishan Vyas 67 posts 198 karma points
    Dec 05, 2018 @ 05:36
    Ishan Vyas
    0

    hii there!!! i want to know how can we achieve the same for the Umbraco api controller. I mean how to retrieve the umbraco content nodes data into an umbracoapi controller. can you please help me out i am stuck into this and cant find any proper solution. if possible can you give some code snippets regarding my topic of concern?? it will be great help. thanx in advance

  • Ishan Vyas 67 posts 198 karma points
    Dec 06, 2018 @ 11:05
    Ishan Vyas
    0

    Hiiii there !!! Any one has solution for my query please need some inputs in this topic.

  • Marcio Goularte 374 posts 1346 karma points
    Dec 06, 2018 @ 13:25
    Marcio Goularte
    1

    Hi Ishan,

    this post is very old. But for what you want you can use UmbracoApiController or PluginController. Both have in context the UmbracoHelper where you will have access to the content.

    https://our.umbraco.com/documentation/reference/routing/webapi/

    For reference see the RestApi Project https://github.com/umbraco/UmbracoRestApi/blob/master/src/Umbraco.RestApi/Controllers/ContentController.cs

  • Ishan Vyas 67 posts 198 karma points
    Dec 06, 2018 @ 13:32
    Ishan Vyas
    0

    thanks man for the reply!!! i hope i get what i want from the links provided by you. cheers man!!! good day:)

  • Marcio Goularte 374 posts 1346 karma points
    Dec 06, 2018 @ 13:37
    Marcio Goularte
    1

    this simple sample.

    using System.Collections.Generic;
    using System.Linq;
    using Umbraco.Web.WebApi;
    using Umbraco.Web;
    
    namespace MyNamespace
    {
        public class ProductApiController : UmbracoApiController
        {
    
            public IEnumerable<Product> GetAllProducts()
            {
    
                return  Umbraco.TypedContentAtXPath("//product")
                        .Select(x=>
                    new Product {
                    Name = x.Name,
                    Url= x.Url,
                    MyProperty = x.GetPropertyValue<string>("MyPropertyAlias"),
                    });
    
            }
        }
    
        public class Product
        {
            public string Name { get; set; }
            public string Url { get; set; }
    
            public string MyProperty { get; set; }
        }
    }
    
  • Marcio Goularte 374 posts 1346 karma points
    Dec 06, 2018 @ 13:40
    Marcio Goularte
    1

    is a poor example, but it shows how you can do

    public IEnumerable<Product> GetAllProductsByCategoryName(string categoryName)
            {
    
                return  Umbraco.TypedContentAtXPath("//product")
                    .Where(x=> x.GetPropertyValue<string>("category") == categoryName)
                        .Select(x=>
                    new Product {
                    Name = x.Name,
                    Url= x.Url,
                    MyProperty = x.GetPropertyValue<string>("MyPropertyAlias"),
                    });
    
            }
    
  • Marcio Goularte 374 posts 1346 karma points
    Dec 06, 2018 @ 13:45
    Marcio Goularte
    1

    This example is using the UmbracoHelper as a query. But you can do something with Examine, which I advise you to do. This link has an example with Umbraco web API

    https://24days.in/umbraco-cms/2016/custom-data-and-examine-searching/

    snippet

    https://our.umbraco.com/forum/extending-umbraco-and-using-the-api/89947-slow-examine-queries-on-azure-and-odd-examine-results-corrected-by-index-rebuild

  • Ishan Vyas 67 posts 198 karma points
    Dec 06, 2018 @ 13:48
    Ishan Vyas
    0

    Thanks Marcio, this is what i actually needed for retrieving the content data from back office in api. I needed something like this to get the things going and you gave me exactly that thing only. cheers man!!! you made my day😇

  • Marcio Goularte 374 posts 1346 karma points
    Dec 06, 2018 @ 13:50
    Marcio Goularte
    0

    In this case you need is UmbracoAuthorizedApiController or UmbracoAuthorizedJsonController

    https://our.umbraco.com/documentation/reference/routing/webapi/#backoffice-controllers

  • Ishan Vyas 67 posts 198 karma points
    Dec 06, 2018 @ 13:56
    Ishan Vyas
    0

    Yes i will surely look into it and try to use them in my site!! thanks again for your instant response. Umbraicans does not disappoint any one that's for sure!!!

Please Sign in or register to post replies

Write your reply to:

Draft