2

Q) How do I access my website content, e.g. books collection in my WebAPI class?

In an Umbraco 8 website project, I am using the following in my razor view to get content, which works fine, allowing me to then iterate over the collection and build my page, all great.

@inherits Umbraco.Web.Mvc.UmbracoViewPage
@using ContentModels = Umbraco.Web.PublishedModels;
@using System.Text.RegularExpressions;


@{
    var books = Model.Root().DescendantsOfType("Book")
                .Cast<Book>().Where(x => x.ShowInWebsite);
}
//...

However, this doesn't compile in my WebAPI class - I don't know how to fix the references needed.

Error I'm getting:

The name 'Model' does not exist in the current context

Other things I've tried:

var books = Umbraco.ContentAtRoot().DescendantsOrSelf().OfTypes("Book");

// Which gives error:

IEnumerable<IPublishedContent>' does not contain a definition for 'DescendantsOrSelf' and the best extension method overload 'PublishedContentExtensions.DescendantsOrSelf(IPublishedContent, string)' requires a receiver of type 'IPublishedContent
Dave
  • 4,621
  • 7
  • 40
  • 56

2 Answers2

0

You WebApi class doesn't have a model, but as long as it inherits from UmbracoApiController you should be able to do

Umbraco.ContentAtRoot()...

instead?

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

According to https://our.umbraco.com/documentation/Reference/Querying/IPublishedContent/Collections/#oftypes you should then be able to do

Umbraco.ContentAtRoot().DescendantsOrSelf().OfTypes("Book")
Jannik Anker
  • 2,985
  • 1
  • 12
  • 20
  • Might I then suggest taking a look at which methods are available, in the official documentation? :-) Unless you've already tried it, have a look at https://our.umbraco.com/documentation/Reference/Querying/IPublishedContent/Collections/#oftypes – Jannik Anker Jun 22 '20 at 14:51
  • Updated my answer as well ;-) – Jannik Anker Jun 22 '20 at 14:54
  • I tried that too - see my updated question with the output of the line you suggested. The docs are of no help either - as they have absolutely no detail on webapi implementations, extremely poor in fact. Thanks. – Dave Jun 22 '20 at 15:24
  • @Dave I see no attempt to use the line I suggested. There is no .DescendantsOfType but there IS a .DescendantsOrSelf() which can be combined with .OfTypes("...")? – Jannik Anker Jun 22 '20 at 17:50
  • that was a typo from another attempt, I tried that too and have updated the question with the relevant code line & error string. – Dave Jun 22 '20 at 18:30
  • 1
    Fair enough! Of course ContentAtRoot() is an enumerable, so you'll have to either throw a .First() after it, or iterate through it and perhaps add results of each DescendantsOrSelf() etc to a collection of your own? – Jannik Anker Jun 22 '20 at 19:17
0

Thanks to the comment by @Jannik Anker

I managed to get my collection by so:

var parent = Umbraco.ContentAtRoot().First().Children().FirstOrDefault(x => x.Name == "Booklist");
if (parent != null) 
{
    var books = parent.Children();
    var test = "";
    foreach (var book in books) 
    {
        test += book.Id + ": " + book.Name + "\r\n";
    }
    return test;
}
Dave
  • 4,621
  • 7
  • 40
  • 56