Content Hub Gotchas: Working with M.PublicLink ConversionConfiguration

In a Content Hub implementation you might often need to produce multiple different size of an image that you can link to from external locations. One way to do think in Content Hub would be to create multiple renditions and create public links to them, however there might be cases where you want a bit more control over which assets you create these links to and when in the lifecycle of your content you do this. In this case another useful functionality of Content Hub is the ability to apply conversions directly on public links.

Within the UI you see that there a number of options for controlling the public link output:

However if you want to control this programatically you will need to interact with the underlying entity record. Looking at the entity record for a public link, you see that this information is captured in a Property called ConversionConfiguration. However the content of this property is a JSON string -- so how do you interact with it from code?

"ConversionConfiguration": {
			"width": "903",
			"height": "800",
			"original_width": "1300",
			"original_height": "1152",
			"ratio": {
				"name": "free"
			}

The first thing that you will notice with the value of this field is that you can't easily set it as a string. When I tried my script would simply crash with no error message or anything! After some searching (and some input from Jeff Wang at Sitecore professional services) it turned out that you need to parse the value into an object of type Newtonsoft.Json.Linq.JToken before assigning it to the ConversionConfiguration property.

publicLink.SetPropertyValue("ConversionConfiguration", 
	Newtonsoft.Json.Linq.JToken.Parse(@"{
			"width": "903",
			"height": "800",
			"original_width": "1300",
			"original_height": "1152",
			"ratio": {
				"name": "free"
			}}"));

Further investigation also shows that you don't need to provide all the attributes for ConversionConfiguraiton.

If you simply want to resize the image for a public link, maintaining it's aspect ratio, you only need to supply a height (NB. if you provide only a width this doesn't work and you'll just get the original image size).

Here is the final code to create a resized public link.

var assetId = [asset-Id-here];
        var publicLink = await MClient.EntityFactory.CreateAsync("M.PublicLink");
        if (publicLink.CanDoLazyLoading())
        {
            await publicLink.LoadMembersAsync(
                    new PropertyLoadOption(new string[] {"Resource","ConversionConfiguration"}), 
                    new RelationLoadOption("AssetToPublicLink")
                );
        }
   
        publicLink.SetPropertyValue("Resource", "downloadOriginal");
        var conversionProperty = publicLink.GetProperty<ICultureInsensitiveProperty>("ConversionConfiguration");
              publicLink.SetPropertyValue("ConversionConfiguration", Newtonsoft.Json.Linq.JToken.Parse(@"{
                  ""height"": """ + height + @"""
              }"));

        var assetToPublicLinkRelation = publicLink.GetRelation<IChildToManyParentsRelation>("AssetToPublicLink");
        assetToPublicLinkRelation.Parents.Add(assetId);
        await MClient.Entities.SaveAsync(publicLink);

Comments

Popular posts from this blog

Sitecore - multi-site or multi-instance?

The power of modular development

Cloud hosting Sitecore - Scaling for peak loads