1

I'm writing a program that creates a new asset in Content Hub using the upload API. How do I attach that newly created asset to theImageField of an item in Sitecore. Most of the examples show how to create a new ImageField item using something from the media library but I haven't found anything using Content Hub. Here's the basics of what I've got so far which obviously doesn't work but it illustrates what I'm trying to accomplish:

using (new SecurityDisabler())   
{ 
    var providerItem= db.GetItem(this.SiteCorePath);
    if (providerItem.Empty) return false;

    this.providerItem.Fields.ReadAll();

    this.providerItem.Editing.BeginEdit();
    try
    {                    
        this.providerItem.Fields["SelectHealth Image"].Value = this.PublicUrl;
        this.providerItem.Editing.EndEdit();
    }
    catch
    {
        this.providerItem.Editing.CancelEdit();
        return false;
    }
}

1 Answer 1

2

Figured out how to solve this myself so I wanted to post this in case anyone else needs help with this. I realized that the FieldItems are just xml so I created a new ImageField and then added the attributes that I needed.

new ImageField(this.providerItem.Fields["Image"])
{
    Alt = this.ProviderName,
    LinkType = "external",
    Height = Height.ToString(),
    Width = Width.ToString()
};

ImageField imageField = (this.providerItem.Fields["Image"]);
imageField.SetAttribute("stylelabs-content-id", this.Entity.Entity.Id.ToString());
imageField.SetAttribute("thumbnailsrc", "https://contenthub-url/api/gateway/" + img.Id.ToString() + "/thumbnail");
imageField.SetAttribute("src", img.PublicLinkUrl);
imageField.SetAttribute("mediaid", "");
imageField.SetAttribute("stylelabs-content-type", "Image");

this.providerItem.Editing.EndEdit();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.