Copied to clipboard

Flag this post as spam?

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


  • Mark Sommer 15 posts 105 karma points
    Aug 09, 2021 @ 06:35
    Mark Sommer
    0

    Looking for a way to programmatically change property values

    Hey all.

    I'm brand new to Umbraco and I'm currently working on creating my first website.

    I've been looking around to try and find a solution to my issue but I realize I might not know what I'm actualy looking for so I hope someone can help me here.

    I have this string that I'm displaying ex: 'Hello {USERNAME}'. This string is set up from a property in the backoffice/Model and I'm trying to figure out how to change it through my code since I want to replace the {USERNAME}.

    The actual data I want to replace the {USER_NAME} comes from and API/external system.

    So far it doesnt seem like I change the value directly through the Model (Model.WelcomeText) and I had a look at contentservice aswell but I dont want to save/publish since I dont want to change the original string in the base.

  • Lewis Smith 211 posts 620 karma points c-trib
    Aug 09, 2021 @ 08:23
    Lewis Smith
    0

    Hey Mark,

    Welcome to the Umbraco world!

    To update a value, you can use the ContentService. You can find all the information here: https://our.umbraco.com/documentation/Reference/Management/Services/ContentService/

    You should be able to use the SetValue method, passing in the target property name and the new value.

    private readonly IContentService _contentService;
    
    public MyClass(IContentService contentService)
    {
        _contentService = contentService;
    }
    

    Usage:

    var c =_contentService.SetValue("welcomeText", "new value");
    _contentService.Save(c);
    

    Let me know if you need anything else!

    Lewis

  • Mark Sommer 15 posts 105 karma points
    Aug 09, 2021 @ 10:08
    Mark Sommer
    0

    Thanks for the quick reply.

    I did stumple across this option but being new to Umbraco I wasnt sure how to use it.

    As stated previously I dont want to save/publish the changed content so is this still viable? Also since I want to change some content in a nested content data type how would you go about rendering the changed content in the view/template?

    Extra question: is it possible to use the Translation/Dictionary items in the Content page setup? enter image description here

  • Kevin T Reynolds 15 posts 158 karma points
    Aug 09, 2021 @ 21:33
    Kevin T Reynolds
    100

    You want the handlebars replaced in your view?

    If the model is returning that string on the WelcomeText property just do a string.Replace in your view. Something like

    string userName = SomeSevice.GetTheUsername();
    <div "my-welcome-text">@Model.WelcomeText.Replace("{USERNAME}", userName)</div>
    
  • Mark Sommer 15 posts 105 karma points
    Aug 10, 2021 @ 11:22
    Mark Sommer
    0

    Hey Kevin

    Thanks a bunch. This actualy gave me a great idea on how to make a solution, although I dont know if its "best practice".

    I make a function in which I can pass my string (Model property) and api data to then merge them and return the merged string.

  • Kevin T Reynolds 15 posts 158 karma points
    Aug 10, 2021 @ 15:57
    Kevin T Reynolds
    0

    That should work just fine, I think best practice would probably be create a custom view model that interacts with a service and returns the calculated property to this view.

    https://our.umbraco.com/documentation/Implementation/Services/#using-the-siteservice-inside-a-view

    but that might be overkill for this one property

  • 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