Copied to clipboard

Flag this post as spam?

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


  • Morten Peter Hagh Jensen 25 posts 85 karma points
    May 29, 2021 @ 09:07
    Morten Peter Hagh Jensen
    0

    Access Umbraco Content in Custom Umbraco Forms workflow

    I am trying to get a custom workflow working where I want to create a node based on a form selection.

    I have this code

    public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
            {
                record.State = FormState.Approved;
    
                var contentService = Current.Services.ContentService;
                var parentNode = Guid.Parse("ff3e93f6-b34f-4664-a08b-d2eae2a0adbd");
    
                var dateField = e.Record.GetRecordFieldByAlias("selectedDate");
                var timeField = e.Record.GetRecordFieldByAlias("selectedTime");
    
                var nodeExists = Umbraco.Content(parentNode).DescendantsOfType("appointmentItemDate").Where(x => x.Name == theDate);
                var dateItem = contentService.Create(dateField.ValuesAsString(), parentNode, "appointmentItemDate");
                contentService.SaveAndPublish(dateItem);
    
                return WorkflowExecutionStatus.Completed;
    
                //Current.Logger.Debug<TimeDateWorkflowType>("The record with unique id {RecordId} that was submitted via the Form {FormName} with id {FormId} has been changed to {RecordState} state",
                //   record.UniqueId, e.Form.Name, e.Form.Id, "approved");
    
            }
    

    The problem is this one

    var nodeExists = Umbraco.Content(parentNode).DescendantsOfType("appointmentItemDate").Where(x => x.Name == theDate);
    

    The type or namespace name 'Content' does not exist in the namespace 'Umbraco' (are you missing an assembly reference?)

    How can I access published nodes in this workflow?

    I want to check if there already exists a node with a given name, f.x. "29-05-2021" and then not create a new node with that name.

  • Chris Evans 137 posts 353 karma points c-trib
    May 31, 2021 @ 04:44
    Chris Evans
    0

    Hi Morten,

    The error you're getting there is because your custom workflow class doesn't have the UmbracoHelper available (so when calling Umbraco.Content it will throw the error)

    You can get a handle on the UmbracoHelper using this code:

    var helper = Umbraco.Web.Composing.Current.UmbracoHelper;
    

    and then call this line:

    var nodeExists = helper.Content(parentNode).DescendantsOfType("appointmentItemDate").Where(x => x.Name == theDate);
    

    Hope that helps!

  • Morten Peter Hagh Jensen 25 posts 85 karma points
    May 31, 2021 @ 19:39
    Morten Peter Hagh Jensen
    0

    That works perfectly! :)

    Thanks alot!

Please Sign in or register to post replies

Write your reply to:

Draft