This post has been migrated from www.experimentsincode.com, we apologise if some of the images or content is missing

This post has been migrated, original date 29 Jan 2009. For a client I had to write a custom Sitecore field. The field was to contain a number that incremented every time a new item using the field was created. I created the field and so that they could seed the number created a Sitecore item that contained the current highest field number. This worked great for a few days but then during testing for some reason the field stopped incrementing and always used the same number. After a bit of investigation I realised that this was caused by a value being set on master template. My code worked by checking to see if the field had a value and if not setting a value using the seed:
 	    if (!Sitecore.Context.ClientPage.IsEvent)
            {
                this.isEvent = false;

                if (string.IsNullOrEmpty(this.Value))
                {
                    this.Value = GetNewValue();
                }

                this.Controls.Add(_SeedValue);

                _SeedValue.ID = GetID("SeedVal");
                _SeedValue.Enabled = false;
                _SeedValue.Disabled = true;
            }
            else
            {
                _SeedValue = FindControl(GetID("SeedVal")) as Edit;

                this.Value = _SeedValue.Value;
            }
What my code did not do is check that the current item being editted was not a master. To do this I had to add the following:
private bool IsMaster(Item item)
{
	if (item == null) return false;
	else if (item.ID == new Sitecore.Data.ID(new Guid("{BAD98E0E-C1B5-4598-AC13-21B06218B30C}"))) return true;
	else if (item.Parent != null) return IsMaster(item.Parent);
	else return false;
}
This method checks to see if the current item being edited is a descendant of the Master item. The guid in the method is the guid of the Master item. Unfortunately you can not check the template of master of the item being edited because a master is based on the template that it is the master for and is not made from a master itself. The main code then changes as follows so that only if the field is blank and not a master should a value be generated:
            if (!Sitecore.Context.ClientPage.IsEvent)
            {
                this.isEvent = false;

                Item edittingItem = Sitecore.Configuration.Factory.GetDatabase("master").GetItem(this.ItemID);

                if (string.IsNullOrEmpty(this.Value) && IsMaster(edittingItem) == false )
                {
                    this.Value = GetNewValue();
                }

                this.Controls.Add(_SeedValue);

                _SeedValue.ID = GetID("SeedVal");
                _SeedValue.Enabled = false;
                _SeedValue.Disabled = true;
            }
            else
            {
                _SeedValue = FindControl(GetID("SeedVal")) as Edit;
                this.Value = _SeedValue.Value;
            }
Also be sure to use
Sitecore.Configuration.Factory.GetDatabase("master").GetItem(this.ItemID);
If you try to use
Sitecore.Context.Database.GetItem(this.ItemID);
You will have problems because the current context is set to the Core database.
comments powered by Disqus