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.
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
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.
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; }
}
}
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
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😇
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!!!
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
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
}
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
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.
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
This is a very good example as well:
http://our.umbraco.org/forum/core/umbraco-5-general-discussion/26029-Umbraco-5-How-to-create-a-custom-controller?p=0#comment108786
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:
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.
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
Hiiii there !!! Any one has solution for my query please need some inputs in this topic.
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
thanks man for the reply!!! i hope i get what i want from the links provided by you. cheers man!!! good day:)
this simple sample.
is a poor example, but it shows how you can do
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
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😇
In this case you need is UmbracoAuthorizedApiController or UmbracoAuthorizedJsonController
https://our.umbraco.com/documentation/reference/routing/webapi/#backoffice-controllers
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!!!
is working on a reply...