Copied to clipboard

Flag this post as spam?

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


  • Varun 7 posts 77 karma points
    Dec 13, 2019 @ 05:19
    Varun
    0

    Hi everyone I have created a Umbraco WebApi controller and trying to get the content of my home page. It has properties like welcomeText, description as aliases and some content inside them. The Id that I can see on umbraco is 1055 and this is cs code I have in my umbraco project. When I'm normally returning a string it is working fine. But when i try to return content it's throwing SystemNullReferenceException error or object reference not set to an instance of an object error in postman. please let me know how can i get the content if any page I want using the Id. below is the code and I'm using Umbraco 8. Let me know what all changes I can make. Thanks

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Web.Http;
    using System.Net.Http;
    using Umbraco.Web;
    using Umbraco.Web.WebApi;
    using Newtonsoft.Json;
    using Umbraco.Core.Models.PublishedContent;
    
    namespace Umbraco.WebApi
    {
        public class EventsController : UmbracoApiController
        {   [HttpGet]
    
            public string GetAllEvents()
            {
                // var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                var content = Umbraco.Content(1055).Value<IEnumerable<IPublishedElement>>("welcomeText").ToList();
                // var contentService = new ContentService();
                // IContent content = contentService.GetById(123);
                // if(content == null) return new{};
                // var res_obj = new Result();
                // res_obj.id = content.Id;
                // res_obj.name = content.Name;
                // var str = content.welcomeText;
                // Object res = new {
                //     id = content.Id,
                //     name = content.Name
                // };
                Console.WriteLine("This is the content",content);
                return "hey";
            }
        }
    }
    
  • Saif Obeidat 79 posts 296 karma points
    Dec 13, 2019 @ 08:50
    Saif Obeidat
    0

    Is WelcomeText property a list?

    if it is just a normal text

    var content = Umbraco.Content(1055).Value<string>("welcomeText");
    
  • Varun 7 posts 77 karma points
    Dec 13, 2019 @ 09:15
    Varun
    0

    And in this will work if welcomeText is a property on my page with 1055 right?

  • Varun 7 posts 77 karma points
    Dec 13, 2019 @ 08:55
    Varun
    0

    Thanks for the reply Saif there is one more issue I'm posting as I need content from back office this is the code I'm having and could you please hlpe me out here. In this code when I'm trying to use it in post man it's always throwing Authentication to this request has been denied how to get access can you help me out

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Web.Http;
    using System.Net.Http;
    using Umbraco.Web;
    using Umbraco.Web.WebApi;
    using Newtonsoft.Json;
    using Umbraco.Core.Models.PublishedContent;
    
    namespace Umbraco.WebApi
    {
        [UmbracoAuthorize]
        public class EventsController : UmbracoAuthorizedApiController
        {   [HttpGet]
    
            public object GetAllEvents()
            {
                // var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                var content = Umbraco.Content(1067);
                Console.WriteLine("content obtained");
                var prop = content.HasValue("title") ? content.Value<string>("title") : "hey";
                Console.WriteLine("property obtained",prop);
                // var contentService = new ContentService();
                // IContent content = contentService.GetById(123);
                // if(content == null) return new{};
                // var res_obj = new Result();
                // res_obj.id = content.Id;
                // res_obj.name = content.Name;
                // var str = content.welcomeText;
                // Object res = new {
                //     id = content.Id,
                //     name = content.Name
                // };
                Console.WriteLine("This is the content",prop);
                return Json(prop);
            }
        }
    }
    
  • Saif Obeidat 79 posts 296 karma points
    Dec 13, 2019 @ 09:11
    Saif Obeidat
    0

    Okay you're inheriting from UmbracoAuthorizedApiController, instead of that you need to inherent UmbracoApiController.

    Inheriting from UmbracoAuthorizedApiController will make sure that your API is only available for backoffice users, see this https://our.umbraco.com/documentation/reference/routing/webapi/authorization

  • Varun 7 posts 77 karma points
    Dec 13, 2019 @ 09:14
    Varun
    0

    But I want the content from back-office itself I want the values of the properties of the pages which I have created in back-office so how to get those could you please help me out here. I'm a noob at Umbraco.

    Also for the above question on welcomeText even though I use it as a string it's still throwing the null pointer exception. I'm totally confused and struck please help me out.

  • Saif Obeidat 79 posts 296 karma points
    Dec 13, 2019 @ 09:40
    Saif Obeidat
    0

    I just created this simple example to get content from the content in the backoffice:

    using Umbraco.Web;
    using Umbraco.Web.WebApi;
    
    namespace Umbraco8Project.Controllers
    {
       public class EventsController : UmbracoApiController
       {
           public string GetContent()
           {
              var content = Umbraco.Content(1063);
              var prop = content.Value<string>("pageName");
              return prop;
           }
       }
    }
    

    and I use this URL to call the API

    http://localhost:26948/umbraco/api/Events/getcontent

    enter image description here

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Dec 15, 2019 @ 23:32
    Alex Skrypnyk
    0

    Hi Varun

    I wouldn't recommend you to hardcode the page id in the code, it's really bad practice. Try to identify how to find your page in the content tree and do the search with UmbracoHelper. Then use Models Builder and strongly typed models to retrieve the property value, like this:

          return stronglyTypedModel.PageName;
    

    Thanks,

    Alex

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Dec 15, 2019 @ 23:33
Please Sign in or register to post replies

Write your reply to:

Draft