Copied to clipboard

Flag this post as spam?

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


  • Duong Dang 5 posts 25 karma points
    May 09, 2014 @ 08:29
    Duong Dang
    0

    Get an error when try to save content using ContentService

    I'm using ApplicationEventHanlder to create a content once application started. Here is my code

    public class CreateContent : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var root = applicationContext.Services.ContentService.GetRootContent();
    
            var content = applicationContext.Services.ContentService.CreateContent("Profile", -1, "umbTextPage");
    
            applicationContext.Services.ContentService.Save(content);
    
            base.ApplicationStarted(umbracoApplication, applicationContext);
        }
    }
    

    However, it gave me an error (applicationContext.Services.ContentService.Save(content);), the stack trace here:

    [NullReferenceException: Object reference not set to an instance of an object.]
       Umbraco.Core.Security.AuthenticationExtensions.GetCurrentIdentity(HttpContextBase http, Boolean authenticateRequestIfNotFound) +127
       Umbraco.Web.Security.WebSecurity.GetUserId() +55
       Umbraco.Web.Security.WebSecurity.get_CurrentUser() +63
       Umbraco.Web.NotificationServiceExtensions.SendNotification(INotificationService service, IUmbracoEntity entity, IAction action, UmbracoContext umbracoContext, ApplicationContext applicationContext) +221
       Umbraco.Web.NotificationServiceExtensions.SendNotification(INotificationService service, IUmbracoEntity entity, IAction action, UmbracoContext umbracoContext) +118
       Umbraco.Web.NotificationServiceExtensions.SendNotification(INotificationService service, IUmbracoEntity entity, IAction action, ApplicationContext applicationContext) +111
       Umbraco.Web.Strategies.<>c__DisplayClass7.

    Is anyone similar with this error and provide me a solution?

    Thanks!

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 09, 2014 @ 09:00
    Jan Skovgaard
    0

    Hi Duong

    What version of Umbraco are you using? And are you using webforms or MVC as the template engine?

    /Jan

  • Duong Dang 5 posts 25 karma points
    May 09, 2014 @ 09:38
    Duong Dang
    0

    Hi Jan,

    I'm using Umbraco version 7.1.1 and MVC template. Please help me on this error.

    Thanks

  • Duong Dang 5 posts 25 karma points
    May 09, 2014 @ 11:22
    Duong Dang
    0

    I changed to contentService.SaveAndPublishWithStatus(profile, raiseEvents: false); to dose not fire any event but it's still error.

    Ps: I got the exception but the content is saved but the "Link to Document" is " Oops: this document is published but is not in the cache (internal error)"

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 09, 2014 @ 23:16
    Jan Skovgaard
    0

    Hi Duong

    Could you share your code please?

    /Jan

  • Duong Dang 5 posts 25 karma points
    May 10, 2014 @ 05:35
    Duong Dang
    0

    Here is my code, my project contains this class only

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Xml.Linq;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.web;
    using Umbraco.Core;
    
    namespace MyUmbraco
    {
        public class CreateContent : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                var root = applicationContext.Services.ContentService.GetRootContent();
                var content = applicationContext.Services.ContentService.CreateContent("get start", -1, "umbTextPage");
    
                applicationContext.Services.ContentService.SaveAndPublishWithStatus(content);
                base.ApplicationStarted(umbracoApplication, applicationContext);            
            }
        }
    }
    
  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 10, 2014 @ 10:45
    Jan Skovgaard
    0

    Hi Duong

    Sorry, missed the sample you posted in the first post.

    However I'm thinking...do you have the document type alias of "umbTextPage" in your solution?

    /Jan

  • Duong Dang 5 posts 25 karma points
    May 10, 2014 @ 12:28
    Duong Dang
    0

    Jan, yes I have the document type with alias "umbTextPage" in my site. "umbTextPage" is document type of Txt starter kit

Please Sign in or register to post replies

Write your reply to:

Draft