Copied to clipboard

Flag this post as spam?

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


  • Ayo Adesina 419 posts 1001 karma points
    Jun 26, 2015 @ 22:30
    Ayo Adesina
    0

    Read Only property on Umbraco doc type

    I want to have a property on a doc type that is read-only (Via the Umbraco UI), I want to populate its value after the umbraco event 'Saved'

    Its going to be a string, but I don't want a text box for the user to edit it.

    Any ideas

  • Robert Foster 440 posts 1684 karma points MVP admin c-trib
    Jun 26, 2015 @ 22:49
    Robert Foster
    0

    Best thing in these cases is to use the Label data type.

  • Ayo Adesina 419 posts 1001 karma points
    Jun 26, 2015 @ 23:07
    Ayo Adesina
    0

    thanks, didnt see that one :-)

    Do you know how you can restrict the saved Umbraco event to fire only on one doc type?

  • Robert Foster 440 posts 1684 karma points MVP admin c-trib
    Jun 26, 2015 @ 23:10
    Robert Foster
    0

    You can inspect the entities in the event data for the ContentType (Document Type) Alias and then use that to restrict your code to a specific Document Type - you can also inspect the properties to see if a specific property exists on the entity, or has a value etc.

    E.g.:

        void ContentService_Saving(IContentService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IContent> e)
        {
            foreach (var article in e.SavedEntities)
            {
                if (article.ContentType.Alias == "Article")
                {
                    // Check for Post Date and set to current date if not already set.
                    if (article.HasProperty("datePosted"))
                    {
                        DateTime? datePosted = article.GetValue<DateTime?>("datePosted");
                        if (!datePosted.HasValue)
                            article.SetValue("datePosted", DateTime.Now);
    
                    }
                }
            }
        }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies