Copied to clipboard

Flag this post as spam?

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


  • Bendik Engebretsen 105 posts 202 karma points
    Nov 09, 2016 @ 17:43
    Bendik Engebretsen
    1

    How to use IContent.SetValue for "checkbox list" and "multiple media picker" properties

    I am importing content into an Umbraco site programmatically, using the ContentService. And using IContent.SetValue for simple textbox properties seems to be straightforward. I also figured out how to SetValue for a Repeatable textstrings-property. The "secret" here was to separate the string values with "\r\n". BTW, is this documented somewhere?

    Anyway, I'm now looking for more "secrets": In my document type, I have a Checkbox list with 20+ prevalues from which the user can select when editing interactively (online). But how can I set this property programmatically? When using IPublishedContent.GetPropertyValue, I get a CSV string back, which is fine, easily iterated through string.Split(). But putting the same CSV string into IContent.SetValue doesn't seem to work, no items are selected in the property.

    The next secret I am looking for is how to set multiple media picker properties. I think I have got parts of the solution using this code:

    string bilder = "";
    using (FileStream bildeStream = new FileStream(bildePathname, FileMode.Open, FileAccess.Read))
    {
        medBilde = ms.CreateMedia(bildeFilename, medHavnFolder, "Image");
        medBilde.SetValue("image", bildeFilename, bildeStream);
        bildeStream.Close();
        ms.Save(medBilde);
        if (!string.IsNullOrWhiteSpace(bilder))
            bilder += ", ";
        bilder += medBilde.Id;
    }
    contHavn.SetValue("Bilder", bilder);
    

    As you can see, I'm trying to put together a CSV string of the media IDs to give to SetValue for the property, but this is obviously wrong.

    I've been battling with this for a couple of long days now, so it would be nice to hear from some backoffice experts on this.

    Can anyone shed some light?

  • Bendik Engebretsen 105 posts 202 karma points
    Nov 10, 2016 @ 13:31
    Bendik Engebretsen
    2

    Okay, so I have figured this out myself. (A tad disappointed that I didn't get any answers, but I'm well over it now...) So, again, I'm publishing the outcome of my research here, in case anyone else is interested.

    Umbraco.MultipleMediaPicker images - IContent.SetValue

    The clue here is to first save your image files as IMedia, then put together a comma separated string with the ids of those IMedia. This string is your input to SetValue for your property:

        // "Shortcuts" for the services we need
        IContentService cs = ApplicationContext.Current.Services.ContentService;
        IMediaService ms = ApplicationContext.Current.Services.MediaService;
    
        // Your content node
        IContent yourContentNode = ...
    
        // CSV string that lists the image media IDs
        string imageIds = "";
    
        // Loop through image files
        foreach (string imagePathname in imagePathnames)
        {
            string imageFilename = Path.GetFileName(imagePathname);
            if (System.IO.File.Exists(imagePathname))
            {
                // Read the image file through a file stream
                using (FileStream imageStream = new FileStream(imagePathname, FileMode.Open, FileAccess.Read))
                {
                    // Stream the file into a new umbraco media item
                    IMedia imageMedia = ms.CreateMedia(bildeFilename, medHavnFolder.Id, "Image");
                    imageMedia.SetValue("umbracoFile", imageFilename, imageStream);
                    ms.Save(imageMedia);
                    imageStream.Close();
                    if (!string.IsNullOrWhiteSpace(imageIds))
                        imageIds += ", ";
                    // Add id of this media item to the list
                    imageIds += imageMedia.Id;
                }
            }
        }
    
    // Set the property value to this list of media ids
    if (!string.IsNullOrWhiteSpace(imageIds))
        yourContentNode.SetValue("YourMultipleMediaPickerPropertyName", imageIds);
    // Dont ever forget to publish your content node!
        cs.Publish(yourContentNode);
    

    Hope it's understandable.

    Umbraco.CheckBoxList - IContent.SetValue

    The clue here is to put together a CSV string with the IDs - not the values - of the items you want to be checked. To do this you have to get a list of all the prevalues and their ids, then do a lookup from value to id to make the list of ids.

    // Get the pre-values for your checkbox list in a Dictionary for lookup
    // <your property id> can be found by hovering over the property name in design mode in backoffice for your document type
    var yourPropertyPrevaluesRootIterator = umbraco.library.GetPreValues(<your property id>);
    yourPropertyPrevaluesRootIterator.MoveNext();
    var yourPropertyPrevaluesIterator = yourPropertyPrevaluesRootIterator.Current.SelectChildren("preValue", "");
    Dictionary<string, string> yourPropertyPrevalues = new Dictionary<string, string>();
    
    while (yourPropertyPrevaluesIterator.MoveNext())
    {
        yourPropertyPrevalues.Add(yourPropertyPrevaluesIterator.Current.Value, yourPropertyPrevaluesIterator.Current.GetAttribute("id", ""));
    }
    
    // Now you can add items to check like this:
    string checkItemsIds = "";
    checkItemIds += yourPropertyPrevalues["Some value to be checked"];
    checkItemIds += ", " + yourPropertyPrevalues["Some other value to be checked"];    
    
    // And finally set your property
    yourContentNode.SetValue("<your property name>", checkItemIds);
    
    // Dont ever forget to publish your content node!
    cs.Publish(yourContentNode);
    

    I wrote this code directly in the forum editor, based on my own code which is slightly more complicated. So, if it doesn't compile or doesn't make sense, please don't hesitate to ask!

  • Bendik Engebretsen 105 posts 202 karma points
    Nov 11, 2016 @ 12:17
    Bendik Engebretsen
    2

    Just a quick addendum/improvement. I wasn't quite satisfied with the hardcoded datatype id, so I figured out a better way: find the id by the datatype alias.

    IDataTypeService dts = ApplicationContext.Current.Services.DataTypeService;
    
    IEnumerable<IDataTypeDefinition> allDataTypes = dts.GetAllDataTypeDefinitions();
    int yourPropertyDataTypeId = allDataTypes.Where(dtd => string.Compare(dtd.Name, "Your property datatype alias", true) == 0).First().Id;
    

    Then you can say umbraco.library.GetPreValues(yourPropertyDataTypeId)

Please Sign in or register to post replies

Write your reply to:

Draft