Copied to clipboard

Flag this post as spam?

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


  • Graham Carr 277 posts 389 karma points
    Sep 15, 2014 @ 12:04
    Graham Carr
    0

    Publishing via code issue

    Hi,

    I am using the following piece of code to create a node within Umbraco 7 (Umbraco version 7.1.4 assembly: 1.0.5261.28127)

                    var _contentService = ApplicationContext.Current.Services.ContentService;
                    var application = _contentService.CreateContent(fullname + " (" + username + ")", Convert.ToInt32(Request["vacancyid"]), "VacancyApplication");
                    application.SetValue("pageHeading", fullname + " (" + username + ")");
                    application.SetValue("candidate", Members.GetCurrentMemberId());
                    application.SetValue("applicationForm", media.Id);               
                    Attempt<PublishStatus> pubstatus = _contentService.SaveAndPublishWithStatus(application);


    This code all works fine and the Attempt status returned shows the publish was a success with no errors and the new node set to published.

    I then have some code on my website to pull in applications for the logged in user as follows:

        var applications = Umbraco.ContentAtXPath("//VacancyApplication[./candidate//Picked[@Key='" + Members.GetCurrentMemberId() + "']]");


    The issue is that the newly created and published node is not returned in the query above, it only appears if I go into the administration and click Save And Publish on the node. I have tried updating the cache, etc. within the code to no avail and indeed if I click Republish the entire site in the administration system the node still does not appear and as mentioned I have to actually click Save And Publish on the node to get it to appear.

    Does anyone have any ideas as to what the issue may be as I am at a little bit of a loss as to why this could be happening?

     

  • Alex Skrypnyk 6148 posts 24077 karma points MVP 8x admin c-trib
    Sep 15, 2014 @ 14:45
    Alex Skrypnyk
    0

    Hi Graham,

    Try to add this code. Maybe you have to update document cache?

    var userAdmin = new User(0);
    
    Node.Publish(userAdmin);
    library.UpdateDocumentCache(Node.Id);
    

    Thanks

  • Graham Carr 277 posts 389 karma points
    Sep 17, 2014 @ 10:04
    Graham Carr
    0

    Thanks for the suggestion.

    I have tried the code you suggest however I am still experiencing the same issue, does anyone have any idea what extra clicking Save and Publish does over publishing a node via the _contentService.Publish and _contentService.SaveAndPublishWithStatus methods?

  • Alex Skrypnyk 6148 posts 24077 karma points MVP 8x admin c-trib
    Sep 17, 2014 @ 10:07
    Alex Skrypnyk
    0

    Hi Graham,

    This method is newest. _contentService.SaveAndPublishWithStatus

    Saves and Publishes a single Umbraco.Core.Models.IContent object

    This is obsolete: _contentService.SaveAndPublish

    Thanks

  • Graham Carr 277 posts 389 karma points
    Sep 17, 2014 @ 10:10
    Graham Carr
    0

    Hi,

    I have been using the SaveAndPublishWithStatus method (see initial problem higher up this thread), I only used the Publish method based on what another user posted. Either way I still experience the same problem as originally mentioned.

    Thanks

  • Graham Carr 277 posts 389 karma points
    Sep 17, 2014 @ 10:10
    Graham Carr
    0

    Hi,

    I have been using the SaveAndPublishWithStatus method (see initial problem higher up this thread), I only used the Publish method based on what another user posted. Either way I still experience the same problem as originally mentioned.

    Thanks

  • Steve Morgan 1348 posts 4457 karma points c-trib
    Sep 17, 2014 @ 18:24
    Steve Morgan
    0

    I've just tested this using a Services.ContentService.Publish and I can see my new node in the var applications 

    var applications = Umbraco.ContentAtXPath("//Homepage/Products");

    Perhaps the problem is with your XPath (I confess I don't quite understand what your xpath is trying to achieve). Put a breakpoint on this line and drill into the non public members -> _content and can you see the node there? I've tried this with both Publish and SaveAndPublishWithStatus and it seems to be there...

    Is it possibly whitespace / illegal chars in your xpath?  

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Sep 17, 2014 @ 21:33
    Dan Diplo
    0

    Why use ContentAtXPath? Why not just use the standard API and see if that returns it? I've done this and it always seems to have worked:

    Off top of my head it would be something like this to get every application:

    var applications = Model.Content.AncestorOrSelf().Descendants("VacancyApplication").Where(n => n.GetPropertyAsValue<int>("candidate") == Members.GetCurrentMemberId();  

    (I'm presuming MemberId is an int).

     

  • Graham Carr 277 posts 389 karma points
    Sep 18, 2014 @ 10:38
    Graham Carr
    0

    Hi,

    The reason I am using ContentAtXpath is two-fold, first is that I am still learning the standard API so wasn' sure how to achieve the above and secondly I have applications that exist within the following type of structure:

    A
       - A school 1
            - Live
                  - Job
                       - Application 1
                       - Application 2
            - Pending
            - Closed
       - A school 2
    B
       - B school
            - Live
            - Pending
            - Closed
       - A school 2

    What my code needs to do is grab all of the applications no matter where they exist in the tree and that match the current logged in member id. As far as I can surmise and I have tried you code above, that code does not return all applications regardless of where they exist in the tree, whereas ContentAtXpath does. If you can help me try and work out how to do this without using the ContentAtXpath query that would be great.

  • Graham Carr 277 posts 389 karma points
    Sep 18, 2014 @ 11:45
    Graham Carr
    0

    Apologies the query you sent works if I remove the

    .Where(n => n.GetPropertyAsValue<int>("candidate")==Members.GetCurrentMemberId();  

    As you rightly mention, the candidate property type is not an int and is actually stored as a "nuPickers.LuceneDropDownPicker" pre-value. How would I change this to find the right value?

  • Graham Carr 277 posts 389 karma points
    Sep 18, 2014 @ 11:52
    Graham Carr
    0

    Okay, I have worked out the correct piece of code, see below:

    var applications = Model.Content.AncestorOrSelf(1).Descendants("VacancyApplication").Where(x => Convert.ToInt32(x.GetPropertyValue<Picker>("candidate").PickedKeys.FirstOrDefault()) == Members.GetCurrentMemberId());

    And I can confirm that this has now resolved the original issue of the node not appearing even though published when using the ContentAtXPath query.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Sep 18, 2014 @ 21:48
    Dan Diplo
    0

    Cool, glad you got it sorted! It's hard to write queries off the top of your head, but I'm glad I pointed you in the right direction. You can do most things via the API :)

Please Sign in or register to post replies

Write your reply to:

Draft