0

Hello Friends I am trying to check or print the component values after the saving event is triggered.

I want to check on each component save. User added the www.blahblh.com URL or not then take action as per URL I am getting.

    public void ComponentSaveAction(Component subject, SaveEventArgs args, EventPhases phase)
            {
                   Component comp = subject;
                    
                    string str = comp.Content.Value; // in str values getting empty
            }

updated code.

    public void ComponentSaveAction(Component subject, SaveEventArgs args, EventPhases phase)
    {
        ItemFields fields = new ItemFields(component.Content, component.Schema);
        foreach (var items in fields)
        {
            if(items.ToString().Contains("www.blahblah.com"))
            {
                //throw pop up exception 
            }
            else{
                //save logic
            }
        }
    }

Using this code I am able to check whether the component field contains the URL or not. Bur problem is its works only for the text fields. How to read the data if the component field contains EmbeddedSchemaField, ComponentLinkField.

Any solution to catch this all filed in parent node level and read as string?

1 Answer 1

2

I wrote a few blog posts about this back in the day, you have a few options. Check this article series:

I can't seem to find part 3 at the moment.

These articles should point you in the right direction. They show an approach with XML schema validation constraints (not user friendly), an approach with Event System (as you are trying) that allows for a message popup with some details as to why the field is not valid, and - the most user friendly, but more complex solution - using a UI extension.

6
  • Thank you for valuable reply. my scenario is i have multiple components which having multiple fields so my question is when user try to save any component. i want to check each filed contain the URL e.g www.blahblah.com then show a popup message for url Commented Jul 14, 2022 at 7:58
  • Is it possible to get component all filed and its values ? i want to check each component filed and its values while saving. Commented Jul 14, 2022 at 9:28
  • 1
    @ketanpatil - yes It's possible, Nuno example "Validating Content on Save: part 1" - already has the sample code ItemFields fields = new ItemFields(component.Content, component.Schema); you need to play around based on your schema requirements.
    – Velmurugan
    Commented Jul 14, 2022 at 12:27
  • @Velmurugan and Nuno I have updated my code please check Commented Jul 19, 2022 at 5:31
  • All those field classes (except EmbeddedSchemaField, which is a schema) have a .Value property. Check that and play with it.
    – Nuno Linhares
    Commented Jul 19, 2022 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.