Copied to clipboard

Flag this post as spam?

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


  • Linden Koppejan 15 posts 165 karma points
    Sep 09, 2022 @ 09:14
    Linden Koppejan
    0

    ContentFinder get Property from Node and set Value (string)

    If I want to acces a Property I'll have to get the node:

    enter code herevar content = contentRequest.UmbracoContext.Content.GetById(18427);
    

    then I have to get the property itself:

    var lastNameField = content.GetProperty("lastName");
    

    But now how can I set the Value to "Hello World". The Following variant doesn't work:

    lastNameField = "HelloWorld!";
    

    Can someone help me with this problem?

  • Linden Koppejan 15 posts 165 karma points
    Sep 09, 2022 @ 10:08
    Linden Koppejan
    0

    So I figured I'll have to solve that different, beacuse the Property gets it's value from the database.

    Maybe I'm wrong and the Value can be set, I'd be looking forward to heare from you.

    I'm stilll figuring out how to do it, so if there are any other ways, I'd appreciate to heare from you :).

    Greetings Linden

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Sep 09, 2022 @ 10:16
    Huw Reddick
    0

    Hi,

    You need to do something like.

    content.SetValue("property",value)

  • Linden Koppejan 15 posts 165 karma points
    Sep 09, 2022 @ 10:18
    Linden Koppejan
    0

    Hi Reddick

    Thanks for answering, but sadly the setValue() doesn't exist on content, because it's a IPublishedContent...

  • Lucas Bisgaard 19 posts 128 karma points c-trib
    Sep 09, 2022 @ 10:30
    Lucas Bisgaard
    1

    Hej Linden,

    The IPublishedContent is a kind of a read only. So there is no way to set the propertyValue.

    What you can do is DI IContentService into your controller, do like this

    public class SomeController {
         private readonly IContentService _contentService;
    
         public SomeController(IContentService contentService) {
                  _contentService = contentService;
         }
    
         public void SomeAction(int nodeId) {
                  var node = _contentSerivce.GetById(nodeId);
                  node.SetValue(alias, "hello world", culture);
    
                  _contentService.Save(node, culture)
         }
    }
    

    you can read more about the contentService here https://our.umbraco.com/documentation/Reference/Management/Services/ContentService/

    Or an old post I found on the same question: https://our.umbraco.com/forum/using-umbraco-and-getting-started/96142-setvalue-for-ipublishedcontent

  • Linden Koppejan 15 posts 165 karma points
    Sep 09, 2022 @ 11:38
    Linden Koppejan
    100

    Although all the posts were very helpful, they did not meet the specifications, which to be true weren't very exact from my side.

    I've came up with this soulution:

    var salesAction = new PageSalesAction(content);
    salesAction.LastNameOverwrite = resultLastName;
    salesAction.SalutationOverwrite = resultSalutation;
    
    contentRequest.PublishedContent = salesAction;
    

    I'm creating a new Page and pass the content from the ContentFinder. Now I created a new Model which includes the getter and setter for the properties:

     public partial class PageSalesAction
    {
        public string LastNameOverwrite { get; internal set; }
        public string SalutationOverwrite { get; internal set; }
    }
    

    In my .cshtml View, which is a PageSalesAction, I can get the data via @Model.LastNameOverwrite and @Model.SalutationOverwrite

    Thanks everyone for you inputs!

    Greetings Linden

Please Sign in or register to post replies

Write your reply to:

Draft