1

I created a custom image field, so far its working fine. but when I take an image path and add it manually to the field without clicking browse. It disappears which is not the normal behavior in OOTB image field.

enter image description here

Custom Image Field Class:

public class CustomImage : Image
{
    public string ItemID
    {
        get
        {
            return base.GetViewStateString("ItemID");
        }
        set
        {
            Assert.ArgumentNotNullOrEmpty(value, "ItemID");
            base.SetViewStateString("ItemID", value);
        }
    }

    public string FieldID
    {
        get
        {
            return base.GetViewStateString("FieldID");
        }
        set
        {
            Assert.ArgumentNotNullOrEmpty(value, "FieldID");
            base.SetViewStateString("FieldID", value);
        }
    }

    public override string Source
    {
        get
        {
            return this.GetViewStateString(nameof(Source));
        }
        set
        {
            Assert.ArgumentNotNull((object)value, nameof(value));

            string str = MainUtil.UnmapPath(value);
            if (str.EndsWith("/", StringComparison.InvariantCulture))
            {
                str = str.Substring(0, str.Length - 1);
            }
            else if (!string.IsNullOrWhiteSpace(value))
            {
                var item = Sitecore.Data.Database.GetDatabase("master").GetItem(new ID(ItemID));
                str = Query(value, item);
            }
            this.SetViewStateString(nameof(Source), str);
        }
    }

    public override void HandleMessage(Message message)
    {
        if (message["id"] != base.ID)
            return;

        if (message.Name.Equals("contentimage:customimage"))
        {
            Sitecore.Context.ClientPage.Start(this, "HandleCustomImage");
        }

        base.HandleMessage(message);
    }
}

Does anyone knows how to solve this?

Thanks in advance.

4
  • Could you share code of your custom image field?
    – grg
    Commented Feb 4, 2021 at 11:36
  • @grg the same steps as the answer in this question sitecore.stackexchange.com/questions/6118/… Commented Feb 4, 2021 at 11:45
  • Please post the code you have written for this custom field Commented Feb 4, 2021 at 14:51
  • @AhmedOkour I added the custom field code in the question Commented Feb 7, 2021 at 8:47

1 Answer 1

0

The problem was in calling the base function, it suppose to be in the beginning of the fuction.

public override void HandleMessage(Message message)
    {
        base.HandleMessage(message);

        if (message.Name.Equals("contentimage:customimage"))
        {
            Sitecore.Context.ClientPage.Start(this, "HandleCustomImage");
        }
    }

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.