Thursday, March 11, 2010

Sitecore Fetch Squad

Automated crawler fetching websites and blogs from Sitecore content

Archive for the ‘Tips’ Category

Custom Sitecore fields and master templates

Posted by admin On January - 29 - 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.

Mike’s Blog

SWF files do not play

Posted by admin On January - 21 - 2009

One of the things to remember before you go live

Posted by admin On January - 21 - 2009
As I mentioned in one of my previous posts, it is always a good idea and common sense to block at least a week in project’s timeline for load testing and performance optimization of your web site before the launch. The performance optimization is a good topic for a separate blog and will not fit into one post BUT one of the things can be done pretty easily and give drastic performance

Consulting and Supporting Sitecore Developer Community

jQuery conflicts with PageEditor

Posted by admin On January - 21 - 2009
There is such problem, indeed. It is caused by the conflict with Prototype.js that most of Page Editor, including designer and debugger uses. It can be simply worked around by overriding the $-function by calling "jQuery.noConflict()". <script type="text/javascript" src="/js/jquery-1.2.6.js"></script>

 

<script type="text/javascript">

 

var $j = jQuery.noConflict();

Consulting and Supporting Sitecore Developer Community