Copied to clipboard

Flag this post as spam?

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


  • Stefan 8 posts 78 karma points
    26 days ago
    Stefan
    0

    Get unpublished items Umbraco 8

    Hello everyone!

    I have an idea but I need your help to steer me in the right direction if possible.

    I have a comment section that I created with the following:

    1. Blogpost doctype with permission to create Comment doctype as a child node
    2. Comment doctype can have its on Comment doctype children

    I created it that way so I can have nested comments and render them in the view.

    I also created a custom controller that will allow an anonymous person to create a comment using a form and post it using the SaveAndPublish method.

    I would like to replace that SaveAndPublish method with Save, so everyone can post comments but I will have to "allow" them in the backoffice by publishing them manually. I know how to do that, but the main problem for me is that I want to create a dashboard in the backoffice that will show me all the unpublished content I have in my Umbraco instance. If possible, I would want to isolate the selection to just show content with Comment doctype but since those will be the only unpublished content on my website, maybe that is not needed at all.

    Thank you all in advance!

  • Lucas Michaelsen 14 posts 148 karma points
    26 days ago
    Lucas Michaelsen
    1

    Hello Stefan.

    You will need to make a ApiController for the backoffice. In that controller I´ll suggest you to use the Examine, to search for all node with a DoctypeAlias and published status.

    The code could like this:

        private void test()
        {
            IExamineManager examineManager = ExamineManager.Instance;
    
            if (!examineManager.TryGetIndex(Constants.UmbracoIndexes.InternalIndexName, out IIndex index))
                throw new InvalidOperationException($"No index found by name {Constants.UmbracoIndexes.InternalIndexName}");
            var search = index.GetSearcher().CreateQuery().NativeQuery("+__NodeTypeAlias:\"comments\" +__Published:\"n\"").Execute();
    
            IEnumerable<ISearchResult> results = search;
    
            int totalResults = Convert.ToInt32(search.TotalItemCount);
        }
    
  • Stefan 8 posts 78 karma points
    25 days ago
    Stefan
    0

    Okay, so by doing that I have all the unpublished items of doctype Comment. I just need a little more guidance.

    Once I have those results, do I use Angular to create the view on the dashboard?

    I am sorry if I am asking stupid questions, but I have lost myself in the documentation and can not quite understand it.

  • Lucas Michaelsen 14 posts 148 karma points
    25 days ago
    Lucas Michaelsen
    1

    No question a stupid question :) Yea, you search for all unpublished with the spefic doctype alias.

    You need to create a custom dashboard, i think i´ll be the best idea. a prefekt guid to create dashboard

    You will from the dashbord, ask angular to go to a C# controller, where you will make the search and return the data to bee showed on your dashboard. But again the link above is a greate tutorial :)

  • Stefan 8 posts 78 karma points
    25 days ago
    Stefan
    0

    Lucas I cant thank you enough for this!

    I have done everything from the guide, I just have one more question and I will get out of your hair. I created the controller and the view, but I seem to have made a mistake when telling Angular where to find the controller, can you tell me what is the correct path?

    I have created the folder in the root folder of the solution, and there I created the controller in C#, but can not seem to hit them in my call in Angular.

    This is the path I entered: ~/Api/CommentsApi/GetComments

  • Lucas Michaelsen 14 posts 148 karma points
    25 days ago
    Lucas Michaelsen
    1

    No worries.

    Well I can see Umbraco is´n explanin the best way to do that. So I´ll show you have I normaly do it.

    First create a c# controller where you Inheritance from UmbracoAuthorizedJsonController thats insecure you that only loggin backoffice users can access the api :

    [Umbraco.Web.Mvc.PluginController("Test")
    public class TestApiController : UmbracoAuthorizedJsonController
    {
        [HttpGet]
        public IHttpActionResult Get()
        {
            // your code
        }
    }
    

    In the angular I´ll normaly create a resources factory for angular like this: Notice that i use something called umbRequestHelper.resourcePromisethis a Umbraco func, witch will create the pefect url.

    "backoffice/Test/TestApi/get" is my URI i want to hit, "Test" is the plugincontrollerattr name diffinde in the C# class, "TestApi" is the controller name, and "get" is the c# action.

    angular.module('umbraco.resources').factory('testResource', function ($q, $http, umbRequestHelper) {
        return {
            get: function (id) {
                 return umbRequestHelper.resourcePromise($http.get("backoffice/Test/TestApi/get", 'error'));
            }
        }
    });
    

    At your angular controller, you simply inject your factory ind, and use it.

    angular.module("umbraco").controller("testController", function($scope, testResource) {
         testResource.get().then(function(data) {
             // data is now the response from the api
         });
    }
    

    Hope it helps :)

  • Stefan 8 posts 78 karma points
    25 days ago
    Stefan
    1

    Lucas, you are a lifesaver! Thank you so much for your time!

Please Sign in or register to post replies

Write your reply to:

Draft