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.
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?
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.
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.
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.
Usage:
Let me know if you need anything else!
Lewis
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?
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
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.
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
is working on a reply...