Copied to clipboard

Flag this post as spam?

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


  • Paul de Quant 403 posts 1520 karma points
    Jan 27, 2015 @ 09:41
    Paul de Quant
    0

    Getting the Id of the page after using CreateContent

    Hello,

    I'm hoping this is an easy question to answer. I need to return the ID of a newly created page when the following code is run:-

    var monthContent = cs.CreateContent(month, yearId, "SmartBlogMonth");
    cs.SaveAndPublishWithStatus(monthContent);
    

    The Save and Publish works fine, but do you know what code I need to get the Id.

    I've tried (the below) but that appears to return 0.

    monthId = monthContent.Id;
    

    Many Thanks

    Paul

  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 27, 2015 @ 09:54
    Sören Deger
    0

    Hi Paul,

    this should be works...

    var monthContent = cs.CreateContent(month, yearId,"SmartBlogMonth");
    cs
    .SaveAndPublishWithStatus(monthContent);
    int monthId = monthContent.Id;

    Is the content node actually published?
    Which version of umbraco do you use? 

     

    Best,

    Sören

  • Paul de Quant 403 posts 1520 karma points
    Jan 27, 2015 @ 09:56
    Paul de Quant
    0

    Hi Sören,

    Thanks for your response, I'm using Umbraco 7.2.

    The strange thing is, is that it actually publishes fine it just displays an error when it does.

    Here are the logs:-

    2015-01-27 08:29:27,137 [59] INFO Umbraco.Core.Publishing.PublishingStrategy - [Thread 39] Content 'Facebook suffers widespread outage' with Id '1467' has been published. 2015-01-27 08:29:27,281 [59] INFO Umbraco.Core.Publishing.PublishingStrategy - [Thread 39] Content '2015' with Id '0' has been published. 2015-01-27 08:29:27,331 [59] INFO Umbraco.Core.Publishing.PublishingStrategy - [Thread 39] Content 'January' with Id '0' has been published. 2015-01-27 08:29:27,426 [59] INFO Umbraco.Core.Publishing.PublishingStrategy - [Thread 39] Content 'Facebook suffers widespread outage' with Id '1467' has been published.

  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 27, 2015 @ 10:01
    Sören Deger
    0

    Hi Paul,

    there is a similar issue:

    http://our.umbraco.org/forum/ourumb-dev-forum/bugs/55049-Error-saving-content

    Do you have install any packages that are not compatible with v7? For example the standard website package? 


    Another possibilitys:

    Is a parent node not published? 

    Do you have any mandatory properties in your document type to publish?

     

    Best,
    Sören 

  • Paul de Quant 403 posts 1520 karma points
    Jan 27, 2015 @ 10:08
    Paul de Quant
    0

    Hi Sören,

    The error message I get is:-

    Server error: Contact administrator, see log for full details. Values of parentId and docNode/@parentID are different.

    It's not quite the .net error page as illustrating in the other thread, it's displayed as a little red bar at the bottom.

    I have installed: Smart Blog, Dialogue and Contour.

    I'm creating parent pages if they don't exist and they should be published before I add a child page to them.

    Thanks

    Paul

  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 27, 2015 @ 10:31
    Sören Deger
    0

    Hi Paul,

    maybe there is a conflict with this packages. But I do not know it currently.

    I would do the following:

    Install a clean 7.2 version. Customize the content service code and check if that will work.

    If yes, install one of the tree packages and check again.

    So you can at least locate from when there are problems.

     


    Have anyone in the community seen this error before?

    Server error: Contact administrator, see log for full details. Values of parentId and docNode/@parentID are different.

     

    Best,
    Sören

  • Paul de Quant 403 posts 1520 karma points
    Jan 27, 2015 @ 12:33
    Paul de Quant
    0

    I've debugged this further and I can see the error is occurring when I run this piece of code (it's the very last piece of code to be run):-

                    IContent content = cs.GetById(pageId);
                    cs.Move(content, monthId);
    

    If I comment this out, it all works as expected. After I move a page I get the error.

    Any thoughts

    Thanks

    Paul

  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 27, 2015 @ 13:13
    Sören Deger
    0

    Hi Paul,

    did you create the new node and call the SaveAndPublish after this lines?

    Can you give me more code? I would need a few lines before and a few lines after the last code snippet, that I can see what you will do exactly.

    Have you debugged with Visual Studio? Then set a breakpoint at line "cs.Move(content, monthId)" and tell me the values of content and monthId.

     

    Best,

    Sören

  • Paul de Quant 403 posts 1520 karma points
    Jan 27, 2015 @ 13:18
    Paul de Quant
    0

    Sure, here it is:-

    private void Document_Publishing(IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
    
            // LETS SET THOSE VARIABLES
            bool requestSuccess = false;
    
            var publishedEnts = e.PublishedEntities;
            int reviewPendingFolderId = 1452; // HOME > NEWS AND BLOG > PENDING REVIEW
            int postsFolderId = 1252; // HOME > NEWS AND BLOG > POSTS
            int articleMonth = DateTime.Now.Month; // DEFAULT TO NOW MONTH
            int articleYear = DateTime.Now.Year; // DEFAULT TO NOW YEAR
            string articleMonthName = string.Empty;
    
            // 1. LETS FIND OUR PAGE
            int pageId = publishedEnts.Select(x => x.Id).FirstOrDefault(); // CURRENT PAGE ID
            int pageParentId = publishedEnts.Select(x => x.ParentId).FirstOrDefault(); // CURRENT PAGE PARENT ID
            IContentType templateType = publishedEnts.Select(x => x.ContentType).FirstOrDefault(); // CURRENT PAGE CONTENT TYPE
    
            // 2. BEFORE WE GO ANY FURTHER LETS MAKE SURE WE'RE ONLY AFFECTING PAGES OF TYPE - SMARTBLOGPOST
            if (templateType.Alias == "SmartBlogPost" && pageParentId == reviewPendingFolderId)
            {
    
                // 3. BEFORE WE MOVE THE PAGE WE NEED TO CHECK IF THE YEAR/MONTH PARENTS EXISTS - IF THEY DONT LETS CREATE THEM
                DateTime pagePublishDate = publishedEnts.Select(x => x.GetValue<DateTime>("smartBlogDate")).FirstOrDefault();
    
                if (pagePublishDate != null)
                {
                    articleMonth = pagePublishDate.Month;
                    articleYear = pagePublishDate.Year;
                    articleMonthName = pagePublishDate.ToString("MMMM", CultureInfo.InvariantCulture);
    
                    ContentService c = new ContentService();
    
                    // CHECK IF YEAR EXISTS
                    IEnumerable<IContent> matchYearPage = c.GetChildrenByName(postsFolderId, articleYear.ToString());
    
                    if (matchYearPage.Count() > 0)
                    {
    
                        // CHECK IF MONTH EXISTS
                        int yearPageId = matchYearPage.Select(x => x.Id).FirstOrDefault();
    
                        IEnumerable<IContent> matchMonthPage = c.GetChildrenByName(yearPageId, articleMonthName.ToString());
    
                        if (matchMonthPage.Count() > 0)
                        {
                            int monthPageId = matchMonthPage.Select(x => x.Id).FirstOrDefault();
    
                            // RUN MOVE ROUTINE
                            requestSuccess = RunPageSetup(true, true, pageId, postsFolderId, articleYear.ToString(), articleMonthName, yearPageId, monthPageId);
                        }
                        else
                        {
                            // CREATE MONTH + RUN MOVE ROUTINE
                            requestSuccess = RunPageSetup(true, false, pageId, postsFolderId, articleYear.ToString(), articleMonthName, yearPageId);
                        }
                    }
                    else
                    {
                        // CREATE YEAR + MONTH + RUN MOVE ROUTINE
                        requestSuccess = RunPageSetup(false, false, pageId, postsFolderId, articleYear.ToString(), articleMonthName);
                    }
    
                    if (!requestSuccess)
                    {
                        LogHelper.Info(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "An error occured while attempt to process this article");
                    }
    
                }
    
            }
    
        }
    

    And the code that contains the Move is here:-

    private bool RunPageSetup(bool yearExistsInTree, bool monthExistsInTree, int pageId, int postsId, string year, string month, int existingYearId = -1, int existingMonthId = -1)
        {
            int monthId = existingMonthId;
            int yearId = existingYearId;
    
            bool readyToMovePage = false;
    
            ContentService cs = new ContentService();
    
            if (!yearExistsInTree && !monthExistsInTree)
            {
                // CREATE NEW YEAR
                var yearContent = cs.CreateContent(year, postsId, "SmartBlogYear");
    
                Attempt<PublishStatus> attYear = cs.SaveAndPublishWithStatus(yearContent);
    
                if (attYear.Success)
                {
    
                    yearId = attYear.Result.ContentItem.Id;
                    LogHelper.Info(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Func Year ID: " + yearId);
    
                    // CREATE NEW MONTH
                    var monthContent = cs.CreateContent(month, yearId, "SmartBlogMonth");
    
                    Attempt<PublishStatus> attMonth = cs.SaveAndPublishWithStatus(monthContent);
    
                    if (attMonth.Success)
                    {
                        monthId = attMonth.Result.ContentItem.Id;
                        readyToMovePage = true;
                    }
                    else
                    {
                        LogHelper.Info(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Month creation failed: " + monthId);
                        readyToMovePage = false;
    
                        return false;
                    }
    
                }
                else
                {
                    LogHelper.Info(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Year creation failed: " + monthId);
                    readyToMovePage = false;
    
                    return false;
                }
    
    
            }
            else if (yearExistsInTree && !monthExistsInTree)
            {
    
                // CREATE NEW MONTH
                var monthContent = cs.CreateContent(month, yearId, "SmartBlogMonth");
    
                Attempt<PublishStatus> attMonth = cs.SaveAndPublishWithStatus(monthContent);
    
                if (attMonth.Success)
                {
                    monthId = attMonth.Result.ContentItem.Id;
                    LogHelper.Info(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Func Month ID: " + monthId);
    
                    readyToMovePage = true;
                }
                else
                {
                    LogHelper.Info(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Month creation failed: " + monthId);
                    readyToMovePage = false;
    
                    return false;
                }
            }
            else
            {
                readyToMovePage = true;
            }
    
            try
            {
                if (readyToMovePage)
                {
    
                    IContent content = cs.GetById(pageId);
                    cs.Move(content, monthId);
    
                }
    
            }
            catch (Exception ex)
            {
                LogHelper.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Error Moving Page", ex);
                return false;
            }
    
            return true;
        }
    
  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 27, 2015 @ 13:31
    Sören Deger
    0

    Hi Paul,

    Unfortunately I can not find anything wrong at the time. I would have replace 

    monthId = attMonth.Result.ContentItem.Id;

     with

    monthId = monthContent.Id;

    But at first, you had it already so.

    Can you tell me the values of content and monthId in cs.Move(content, monthId)? I have asked in my last post above for this. Maybe this helps to find the issue.

    And has the variable pageId the correct value of your currentPage?

     

    Best,

    Sören

     

  • Paul de Quant 403 posts 1520 karma points
    Jan 27, 2015 @ 14:45
    Paul de Quant
    0

    Hi Sören,

    I'm pretty sure the pageID is correct, as despite the error message everything works as it should, the correct pages get published and the correct page gets moved under those newly created pages.

    In my last test, the monthId was 1489 and the content, was what I expected it to be.

    I can't debug it locally, so I'm afraid I can't tell you exactly what the values are.

    Many Thanks

    Paul

  • Sören Deger 733 posts 2844 karma points c-trib
    Jan 27, 2015 @ 15:11
    Sören Deger
    0

    Hi Paul,

    thanks, but without the possibility to debug this it's hard to find out the issue.At least I have currently no idea, sorry.

     

    Best,

    Sören

  • Paul de Quant 403 posts 1520 karma points
    Jan 27, 2015 @ 15:12
    Paul de Quant
    0

    Hi Sören,

    No worries and thank you for you help thus far. Hopefully someone will provide the answer I'm looking for.

    Cheers

    Paul

  • Paul de Quant 403 posts 1520 karma points
    Feb 05, 2015 @ 10:24
    Paul de Quant
    101

    Ok, I've managed to solve this one.

        if (readyToMovePage)
        {
                    umbraco.library.RefreshContent();
    
                    IContent content = cs.GetById(pageId);
                    cs.Move(content, monthId);
    
                    umbraco.library.RefreshContent();
        }
    

    I added the Refresh Content command, before and after and then I changed the event handle to:-

     ContentService.Publishing += Document_Publishing;
    

    Originally I had this set to:-

    ContentService.Published += Document_Publishing;
    
  • Mark Bowser 273 posts 860 karma points c-trib
    Dec 17, 2015 @ 16:49
    Mark Bowser
    0

    I'm having this issue when I preview content on my umbraco 7.2.8 site.

    Values of parentId and docNode/@parentID are different.
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.ArgumentException: Values of parentId and docNode/@parentID are different.
    
    Source Error: 
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
    
    Stack Trace: 
    
    
    [ArgumentException: Values of parentId and docNode/@parentID are different.]
       umbraco.content.GetAddOrUpdateXmlNode(XmlDocument xml, Int32 id, Int32 level, Int32 parentId, XmlNode docNode) +882
       umbraco.presentation.preview.PreviewContent.PrepareDocument(User user, Document documentObject, Boolean includeSubs) +817
       umbraco.presentation.dialogs.Preview.Page_Load(Object sender, EventArgs e) +300
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
       System.Web.UI.Control.OnLoad(EventArgs e) +92
       umbraco.BasePages.BasePage.OnLoad(EventArgs e) +14
       System.Web.UI.Control.LoadRecursive() +54
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
    

    So far, republishing the entire site and hard publishing the home page (that I was previewing in this case) hasn't solved the problem. As far as I can tell, I am not able to preview any nodes right now.

  • Mark Bowser 273 posts 860 karma points c-trib
    Dec 17, 2015 @ 17:16
    Mark Bowser
    0

    Ah ha! It doesn't affect every node. It especially does not affect leaf nodes. It looks like umbraco is having trouble previewing a node when one of the node's descendants is having problems. I followed the trail of broken previews all the way down to a leaf blog post. It was unpublished, but there were mandatory fields on it that were not entered. It looks like umbraco gets upset when you manage to save a node without filling out the mandatory fields.

Please Sign in or register to post replies

Write your reply to:

Draft