asked    Celeste     2018-10-12       asp.net-mvc       13 view        1 Answer

[SOLVED] Custom Sections & Custom Trees MVC approach

I have spent hours and hours trying to get a custom tree to show up in a custom section in umbraco. - With out success.

So far I have managed to create a new section, but when I click that section nothing happens.

What should happen is a custom tree should be displayed with one node. When you click that node It should display an MVC view.

This is what I have done so far, it is based around this tutorial.

A. Create the section

{
    [Application("rewards", "Rewards", "icon-gift", 15)]
    public class RewardsSection: IApplication
    {
    }
}

B. Create the Tree

[Tree("rewards", "rewardsTree", "Rewards")]
[PluginController("Rewards")]
public class RewardsTree : BaseTree
{
    public RewardsTree(string application)
        : base(application)
    { }

    protected override void CreateRootNode(ref XmlTreeNode rootNode)
    {
        rootNode.NodeType = "rewards";
        rootNode.NodeID = "-1";
        rootNode.Menu = new List<IAction> { ActionRefresh.Instance };

    }

    public override void Render(ref XmlTree tree)
    {
        var IndexNode = XmlTreeNode.Create(this);
        IndexNode.NodeID = "0";
        IndexNode.NodeType = "Home";
        IndexNode.Text = "Home";
        IndexNode.Action = "javascript:openPage('/umbraco/backoffice/Plugins/Rewards/Index');";
        IndexNode.Icon = "icon-home";
        IndexNode.HasChildren = false;
        IndexNode.Menu = new List<IAction>();
        OnBeforeNodeRender(ref tree, ref IndexNode, EventArgs.Empty);

        if (IndexNode != null)
        {
            tree.Add(IndexNode);
            OnAfterNodeRender(ref tree, ref IndexNode, EventArgs.Empty);
        }
    }

    public override void RenderJS(ref StringBuilder Javascript)
    {
        var js = $"function openPage(url){{UmbClientMgr.contentFrame(url);}}";
        Javascript.Append(js);
    }

    protected override void CreateAllowedActions(ref List<IAction> actions)
    {
        actions.Clear();
        actions.Add(ActionNew.Instance);
        actions.Add(ActionDelete.Instance);
        actions.Add(ContextMenuSeperator.Instance);
        actions.Add(ActionRefresh.Instance);
    }
}

C. RegisterRoutes (called on ApplicationStarted)

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
          name: "Default",
          url: "umbraco/backoffice/Plugins/{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );
}
public class StartUpHandlers : ApplicationEventHandler
{
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
}

D. Back-end Controller - Returning view I want to see.

public class RewardsController : UmbracoAuthorizedController
{
    public ActionResult Index()
    {
        return View("~/App_Plugins/Rewards/Views/RewardsHome/Index.cshtml");
    }
}

So what am I missing?

I put break points in the RewardsTree Class but none of them are hit.

I also put break points on Application Started and those break points are being hit so I am pretty sure the route is being configured correctly.

Is there anything that I am missing? I have seen other examples using a TreeController insted of TreeBase which has confused me a bit.

ANY ideas? - very stuck

  1 Answer  

        answered    Olive     2018-10-12      

I have managed to get a custom tree displaying in a custom section! At last! -

The problem I had was two fold.

From all the tutorials online that walk you through how to do this when defining the custom tree some tutorials inherit from BaseTree and others inherit from TreeController -

I have got it to work inheriting from TreeController, from what I understand using the BaseTree is the 'old way' of doing it.

I'm running Umbraco version 7.6.4 assembly: 1.0.6396.36621 - I'm not sure if the old way works using this version or not, but I couldn't get it to work.

So after following the guide lines in chapter 16 (Custom Sections, Trees and Actions) of this book:

I got my project to compile and run, the custom section showed up, but the custom tree didn't, when I clicked the custom section an error was thrown.

System.NullReferenceException: Object reference not set to an instance of an object.

Part of stack trace looks like this:

Umbraco.Web.Trees.ApplicationTreeExtensions.TryLoadFromControllerTree(ApplicationTree appTree, String id, FormDataCollection formCollection, HttpControllerContext controllerContext) at Umbraco.Web.Trees.ApplicationTreeController.d__17.MoveNext() --- End of stack trace from previous location where exception was thrown -

It wasn't obvious why this was happening, but after a bit of reasearch I worked out this was becuase....................

I am using a IoC contanier and for this to work I needed to make sure the TreeController is registered in the containier! - This is something that is not mentioned in any documentation or tutorials relating to creating custom trees. The documentation assumes your using Umbraco with out an IoC container.

In my case I'm using Autofac so all I had to do was add this to my container registration code:

builder.RegisterApiControllers(typeof(RewardsTreeController).Assembly);

and suddenly everything works!

Really hope this helps someone else.

Umbraco (7.6. 4) / Autofac.Mvc5 (4.0.2) / Autofac.WebApi2 (4.0.1) / << That's the combination of nuget packages that are working for me.





Your Answer





 2018-10-12         Tobey

Get umbraco fields inside form view

I need to have a custom view for an umbraco 7 form, that have a special column layout, but my problem is that I cannot output data from an umbraco field with .GetPropertyValue("formHeader")I am rendering the form with @Umbraco.RenderMacro("FormsRenderForm", new { FormGuid = "6189b8c2-980a-4f02-bcec-33b170020a22" })Inside my Form.cshtml I would like to insert an umbraco field .GetPropertyValue("formHeader")But I cannot seem to get it to work@inherits Umbraco.Web.Mvc.UmbracoTemplatePage will return Error loading Partial View script (file: ~/Views/MacroPartials/InsertUmbra...
 razor                     2 answers                     93 view
 2018-10-12         Magee

Umbraco Forms image upload to media picker

I've read on our.umbraco.org that the Umbraco Forms' "file upload" can be used to upload images that can then be used by the media picker.I've created a workflow which creates an Umbraco document, and I mapped the file upload to the document type's media picker and all the other properties are of course mapped as well.Whenever I submit the form, it creates the unpublished node as it should, and it maps all the properties EXCEPT the media picker. I can't figure out why...I'm on Umbraco version: 7.3.7.Thank you in advance. This is an old question but just for the records. ...
 c#                     1 answers                     94 view
 2018-10-12         Martina

Import entries to Umbraco Contour

Is there a way (the best would be an Excel importer) to import entries to an Umbraco Contour form? As far as I know, there is currently no way to import records to Contour out of the box I'm afraid. You'd have to use the Contour APIs and do it yourself, although there will be some complexities around items that have lists, like dropdown lists or multiple checkboxes. [XXX]
 umbraco7                     1 answers                     95 view