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";
}
}
}
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);
}
}
}
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.
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;
}
}
}
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:
Umbraco 8 get Content
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
Is WelcomeText property a list?
if it is just a normal text
And in this will work if welcomeText is a property on my page with 1055 right?
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
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
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.
I just created this simple example to get content from the content in the backoffice:
and I use this URL to call the API
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:
Thanks,
Alex
Umbraco Models Builder docs - https://our.umbraco.com/documentation/reference/templating/modelsbuilder/
is working on a reply...