Copied to clipboard

Flag this post as spam?

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


  • Srdjan 19 posts 53 karma points
    Aug 14, 2015 @ 07:26
    Srdjan
    0

    Error on programmatically created content

    Hello all,

    I am using Umbraco 7.1.8. with uDateFoldersy 2.0. When I try to add node through Umbraco back office everything is fine, all containers (year, month and day) are created and node is nested in the right position. The problem occurs when I try to create node through source code, I am getting null reference exception on creating first missing container. So if there is no year container error is thrown in uDateFoldersy GetYearFolder method, the same goes for month and day, if day container is missing error will occur in GetDayFolder method. If all containers exist then no error will occur. Please check exception details bellow:

    at Umbraco.Web.Security.WebSecurity.ValidateCurrentUser(Boolean throwExceptions)
    at umbraco.presentation.webservices.nodeSorter.UpdateSortOrder(Int32 ParentId, String SortOrder)
    at uHelpsy.Helpers.IContentHelper.SortNodes(Int32 parentId, IEnumerable`1 contentItems, IComparer`1 comparer) in d:\_PROJECTS\Personal\uHelpsy - Library\uHelpsy\Source\uHelpsy.Web\Helpers\IContentHelper.cs:line 51
    at uDateFoldersy.DateFolderService.GetDayFolder(IContent monthFolder, DateTime postDate, String dayFolderAlias) in d:\_PROJECTS\Personal\uDateFoldersy\Source\uDateFoldersy.Web\DateFolderService.cs:line 586
    at uDateFoldersy.DateFolderService.GetCorrectParentForPost(IContent doc, DateTime date, String dayFolderAlias, String monthFolderAlias, String yearFolderAlias) in d:\_PROJECTS\Personal\uDateFoldersy\Source\uDateFoldersy.Web\DateFolderService.cs:line 433
    at uDateFoldersy.DateFolderService.EnsureCorrectParentForPost(IContent doc, Boolean isCreatedEvent) in d:\_PROJECTS\Personal\uDateFoldersy\Source\uDateFoldersy.Web\DateFolderService.cs:line 189
    

    In the case above when I already have year and month folders and day should be created, when I refresh Umbraco back office after the error occurred Day folder is created at the right place and Node is created under the RootDocTypeAliases node (the similar case is if Year folder is missing, only the year container is created and specific node is created under the RootDocTypeAlias).

    The code I am trying to use to write content programmatically is:

    var contentService = ApplicationContext.Current.Services.ContentService;
    var newEntity = contentService.CreateContent("new node name", 1233, "newEntityDocTypeAlias");
    // seting property...
    contentService.Save(newEntity , raiseEvents: true);
    

    Thanks, Srdjan

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Aug 14, 2015 @ 13:13
    Alex Skrypnyk
    0

    Hi Srdjan,

    Can you try to save like that?

    ApplicationContext.Services.ContentService.SaveAndPublishWithStatus(playerFromUmbraco, 0, false);
    

    Thanks, Alex

  • Srdjan 19 posts 53 karma points
    Aug 14, 2015 @ 13:28
    Srdjan
    0

    Hi Alex,

    just to correct myself if I run saving without raising an event I am able to create node without an error but node is not published and it is created under RootDocTypeAliase without folders structure I needed. So I need to rise an event in order to create folder structure and no mater if I pass user id or not the error occur.

    Thanks, Srdjan

  • Srdjan 19 posts 53 karma points
    Aug 18, 2015 @ 13:31
    Srdjan
    0

    Hi to all,

    I have inspected uDateFoldersy source code, so 'GetYearFolder' method calls 'SortNodes' method from uHelpsy which then call:

    var nodeSorter = new umbraco.presentation.webservices.nodeSorter();
    ...
    nodeSorter.UpdateSortOrder(parentId, sortOrder.TrimEnd(','));
    

    'UpdateSortOrder' method looks like this:

    [WebMethod]
        public void UpdateSortOrder(int ParentId, string SortOrder)
        {
            if (AuthorizeRequest() == false) return;
            if (SortOrder.Trim().Length <= 0) return;
    
            var isContent = helper.Request("app") == "content" | helper.Request("app") == "";
            var isMedia = helper.Request("app") == "media";
    
            //ensure user is authorized for the app requested
            if (isContent && AuthorizeRequest(DefaultApps.content.ToString()) == false) return;
            if (isMedia && AuthorizeRequest(DefaultApps.media.ToString()) == false) return;
    
            var ids = SortOrder.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            if (isContent)
                SortContent(ids, ParentId);
    
            if (isMedia)
                SortMedia(ids);
    
            if (isContent == false && isMedia == false)
                SortStylesheetProperties(ids);
        }
    

    Is it possible that only back office logged in users could perform changing SortOrder by using UpdateSortOrder method?

    Is there a way to achieve this without changing source code of uDateFoldersy by removing sort order updates? I have already tried to log in admin user before I perform contentService.Save(newEntity, 0, true) method by this piece of code but I am getting the same error:

    using (var umbSecurity = new Umbraco.Web.Security.WebSecurity(new HttpContextWrapper(HttpContext.Current), ApplicationContext.Current))
    {
        umbSecurity.PerformLogin(0);
        contentService.Save(newPrescriptionContent, 0, true);
        umbSecurity.ClearCurrentLogin();
    }
    

    Thanks in advance, Srdjan

Please Sign in or register to post replies

Write your reply to:

Draft