0
votes

Normally, when I want to access the current Item in Sitecore, I go through Sitecore.Context.Item. This works well when creating a tool which will be used by the end user, but not for a tool which is consumed by the Sitecore admins. If I want something to show up as a custom field in the Content Editor itself, the Context.Item is a reference to the Content Editor and not to the node which is selected in the editor.

For the most part I can get around this by using the ItemID property, but if I have event dispatchers on the field, those will no longer have access to the ItemID. Eg:

protected override void OnPreRender(EventArgs e)
{
    if (IsEvent)
    {
        // ItemID is defined here!
        Button live = FindControl(GetID(LiveButton)) as Button;
        if (live != null)
        {
            live.ServerProperties["Click"] = string.Format("{0}.LiveClicked", ID);
        }
    }
}

public void LiveClicked()
{
   // ItemID is blank here!
   DoSomething();
}

How do I gain access to ItemID in my listener (like LiveClicked above)?

1
I realize that this is not what the custom fields are really supposed to do, but the client has stated explicitly that he wants certain functionality in the main body of the Content Editor. - cwallenpoole

1 Answers

0
votes

The way I solved it was through something called ServerProperties and I called the equivalent to this function in every listener.

private void SyncID()
{
    var live = FindControl(GetID(LiveButton)) as Button;
    if (live != null)
    {
        if(string.IsNullOrEmpty(ItemID))
        {
            ItemID = live.ServerProperties["owner_id"].ToString();
        }
        live.ServerProperties["owner_id"] = ItemID;
    }
}