Copied to clipboard

Flag this post as spam?

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


  • MB 273 posts 936 karma points
    Mar 09, 2019 @ 21:41
    MB
    0

    .setValue for IPublishedContent?

    Hi guys,

    I'm trying to set all bools for a specific document type to false but I can't seem to figure out how.

    Using the code below I can retrieve all pages with the "notForSale" set to false. However I would like to change that value to true, but how?

    I can't use .setValue("notForAle", "1") on IPublishedContent

        foreach (var item in ((IPublishedContent)Umbraco.Content(1128)).Descendants().Where(m => m.DocumentTypeAlias.Equals("productPage") && m.GetPropertyValue<bool>("notForSale") == false))
        {
           item.SetValue not working
        }
    
  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Mar 10, 2019 @ 02:08
    Alex Skrypnyk
    1

    Hi MBE

    IPublishedContent is only for reading data. For all changes to the database use IContent and ContentService, read about content service - https://our.umbraco.com/documentation/Reference/Management/Services/ContentService/

    Thanks,

    Alex

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Mar 10, 2019 @ 02:16
    Alex Skrypnyk
    2

    Also, do not use .Descendants() it's a very slow method

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    Mar 10, 2019 @ 02:17
    Alex Skrypnyk
    102

    So your code will look like:

    var contentItem = ApplicationContext.Services.ContentService.GetById(id);
    contentItem.SetValue("propertyAlias", value);
    ApplicationContext.Services.ContentService.SaveAndPublish(contentItem);
    
  • MB 273 posts 936 karma points
    Mar 10, 2019 @ 14:57
    MB
    0

    Hi Alex,

    Thank you very much for your response! I wasn't aware of the difference between IPublichedContent and IContent so thank you for helping me out in that regard.

    However, I can get it to change for a single page. But how can I loop through every page of a specific documentType?

    //Mike

    EDIT: I found a way, probably not the best solution but it worked:

    @{
        Layout = "Master.cshtml";
        IContentService contentService = ApplicationContext.Services.ContentService;
        var contentItem = ApplicationContext.Services.ContentService.GetById(1064);
    
    
        foreach (var item in contentItem.Descendants())
        {
            if (item.HasProperty("ikkeTilSalg"))
            {
                item.SetValue("ikkeTilSalg", "1");
                ApplicationContext.Services.ContentService.SaveAndPublish(item);
            }
        }
    }
    

    Thank you Alex, for providing useful links and info's!

Please Sign in or register to post replies

Write your reply to:

Draft