Copied to clipboard

Flag this post as spam?

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


  • K.Garrein 121 posts 419 karma points
    May 24, 2017 @ 07:54
    K.Garrein
    0

    Unable to set value of content picker

    Hey all,

    I'm having this little issue. I don't seem to be able to set the value of a content picker data type.

    I have 2 Document Types:

    • Product

    • Ordered Product

    Ordered Product has a property "Product Picker" (new data type based on content picker).

    I need to set the value programmatically, but I don't seem to be able to get that to work :'(

    My code: (cntsvc is my ContentService)

    var Product = UmbHelper.TypedContent( productId );
    var OrderedProduct = cntsvc.CreateContent( productname, parentId, "orderedProduct" );
    OrderedProduct.SetValue( "productPicker", Product.Id );
    cntsvc.SaveAndPublishWithStatus( OrderedProduct );
    

    What am I doing wrong? Do I need to set some other value other than the Id ?

    Thank you.

    K.

  • Alex Skrypnyk 4460 posts 15997 karma points MVP admin
    May 24, 2017 @ 08:07
    Alex Skrypnyk
    0

    Hi

    What version of Umbraco are you using? If it's 7.6 you have to use UID instead of ID.

    Thanks,

    Alex

  • K.Garrein 121 posts 419 karma points
    May 24, 2017 @ 08:14
    K.Garrein
    0

    Version = 7.6.1

    How do I get the UID? IPublishedContent doesn't seem to have such a property...

    Thanks

  • Alex Skrypnyk 4460 posts 15997 karma points MVP admin
    May 24, 2017 @ 08:20
    Alex Skrypnyk
    1
    var node =
    ApplicationContext.Services.ContentService.GetById(productId);
    
    var locaUdi = Udi.Create(Constants.UdiEntityType.Document, node.Key);
    
    var OrderedProduct = cntsvc.CreateContent( productname, parentId, "orderedProduct" );
    OrderedProduct.SetValue( "productPicker", locaUdi );
    cntsvc.SaveAndPublishWithStatus( OrderedProduct );
    
  • K.Garrein 121 posts 419 karma points
    May 24, 2017 @ 08:25
    K.Garrein
    0

    Error:

    The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)' has some invalid arguments

  • Alex Skrypnyk 4460 posts 15997 karma points MVP admin
    May 24, 2017 @ 08:28
    Alex Skrypnyk
    101
    OrderedProduct.SetValue( "productPicker", locaUdiю.ToString() );
    
  • K.Garrein 121 posts 419 karma points
    May 24, 2017 @ 08:33
    K.Garrein
    1

    That worked :) Thank you!

    But why is this all necessary, there is no documentation about this (as far as I can tell) and the previous way of doing it (with an integer Id) seems much more straightforward to me!?

    Anyway, thanks again!

  • Alex Skrypnyk 4460 posts 15997 karma points MVP admin
    May 24, 2017 @ 08:44
    Alex Skrypnyk
    0

    You are welcome! Glad to help.

    I think there are not so many documentation because it's pretty new feature. And Guys from HQ haven't enough time for upgrading all stuff. Maybe we can contribute to documentation section ) Nice idea.

    Alex

  • Kevin 2 posts 72 karma points
    Jan 11, 2018 @ 15:28
    Kevin
    0

    I tried Alex's approach in 7.7.6, and it half works; it does save successfully, when I go to the edit page I see the correct related document, but when I try loading the published page I get some errors in my template accessing the related document's properties.

    In terms of the OP's original example, this

    CurrentPage.ProductPicker.Name
    

    would fail with error 'Umbraco.Core.GuidUdi' does not contain a definition for 'Name', and this

    Model.Content.ProductPicker.Name
    

    Would fail with error Object reference not set to an instance of an object, ProductPicker being null.

    Any idea why?

    [EDIT]

    I think I figured it out. The related document was also created via the ContentService, and when I check its properties, the "Link to document" says "This document is published but is not in the cache". After I click "Save and publish", I get an actual link to document and after I click "preview" the original problem I had is fixed. This is not ideal, as I have 300+ more documents I will create via the ContentService, but at least I can move on.

  • Jonathan Richards 233 posts 1370 karma points MVP
    May 24, 2017 @ 08:12
    Jonathan Richards
    1

    Hi K,

    If like myself you only ever use Multi Node Tree Pickers, instead of the actual real Content Picker, because of their superior capabilities then you need to set the value to a string.

    var OrderedProduct = cntsvc.CreateContent( productname, parentId, "orderedProduct", 0 );
    OrderedProduct.SetValue( "productPicker", productId.ToString() );
    cntsvc.SaveAndPublishWithStatus( OrderedProduct );
    

    If you really are using the old Content Picker data type, then yeah memory serves me right, it does take an int. So I'm not sure what the issue is - presuming the alias of the property field really is 'productPicker' etc.

    Cheers

    Jonathan

  • K.Garrein 121 posts 419 karma points
    May 24, 2017 @ 08:20
    K.Garrein
    0

    I am using a Content Picker data type, not a Multinode Treepicker.

Please Sign in or register to post replies

Write your reply to:

Draft