Copied to clipboard

Flag this post as spam?

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


  • Paul de Quant 394 posts 1477 karma points
    Sep 04, 2017 @ 15:56
    Paul de Quant
    0

    Create new Crop Sizes Dynamically in C#

    Hello,

    I need to create 4 crop profiles for the Image Cropper datatype dynamically in C#. How would one do this? Can this be done using the DTS?

    var dt = ApplicationContext.Current.Services.DataTypeService;

    Thanks

    Paul

  • Kevin Jump 1867 posts 11859 karma points MVP 4x c-trib
    Sep 06, 2017 @ 08:07
    Kevin Jump
    101

    Hi

    the crop information is stored as a bit of JSON in the datatype. under the 'crops' property.

    e.g.

      {
        "alias": "Hero",
        "width": 800,
        "height": 250
      },
      {
        "alias": "Featured",
        "width": 700,
        "height": 250
      }
    

    yes you can use the datatype service to get to this. as an example (but try to avoid ApplicationContext.Current if you can)

    var datatypeService = ApplicationContext.Current.Services.DataTypeService;
    var dataType = datatypeService.GetDataTypeDefinitionByName("nameOfMyCropper");
    
    if (dataType != null)
    {
        var preValues = datatypeService.GetPreValuesCollectionByDataTypeId(dataType.Id)
            .FormatAsDictionary();
    
        if (preValues.Any(x => x.Key.InvariantEquals(crops)))
        { 
            var cropValue = preValues.SingleOrDefault(x => x.Key.InvariantEquals("crops"));
            // minipulate the crop values here
            ........ 
            cropValue.Value.Value = newCropsJsonString;
        }
    
    
        datatypeService.SavePreValues(dataType, preValues);
    
    }
    

    That should be a start, it might need the odd tweak as i haven't tested that code just typed it.

    Kevin

  • Paul de Quant 394 posts 1477 karma points
    Sep 06, 2017 @ 08:39
    Paul de Quant
    1

    Hi Kevin,

    Thanks for this - I managed to sort it in the end just didn't get round to posting my solution yet.

    Thanks for your help though.

                    var services = ApplicationContext.Current.Services;
    
                var dt = services.DataTypeService;
                var imgCrop = dt.GetDataTypeDefinitionByName("Image Cropper");
                var preValues = dt.GetPreValuesCollectionByDataTypeId(imgCrop.Id).PreValuesAsDictionary;
    
                var dict = new Dictionary<string, PreValue>
                {
                    { "Crops", new PreValue("[{\"alias\": \"6 by 4\",\"width\": 720,\"height\": 480}]") },
                };
    
                dt.SaveDataTypeAndPreValues(imgCrop, dict);
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies