Monday, May 30, 2011

Item Saving Pipeline change field trigger infinite loop

Recently, my colleague is required to duplicate a field value to another field. let's just say Publish To field. To do this it will need to create a custom pipeline processor on item saving. However we face a problem of infinite loop, because after changing the field value, it triggers the on item saving pipeline again. To solve this issue,

we need to set

item.Editing.EndEdit(true);

after editing the field.

Sunday, May 29, 2011

Sitecore Query

I have come to known this issue whereby Sitecore Query limit the search by default to 100. This means potentially we will get less results than expected. Recently, I have come across a solution where the Query max item is tweaked on the fly.

here is the sample code


///
/// Set Query.MaxItems unlimit
/// Reference : http://blog.bylectric.net/2009/07/sitecore-and-query-maxitems-setting/
///

///
///
///
public static Item[] QueryItemsWithoutLimit(string queryString , Item startRoot )
{
if (string.IsNullOrEmpty(queryString) || startRoot == null)
return null;

Query query = new Query(queryString);
query.GetType().GetField("m_max", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(query, 0);
Object result = query.Execute(startRoot);
QueryContext qc = result as QueryContext;
QueryContext[] qcArray = result as QueryContext[];

var database = global::Sitecore.Context.ContentDatabase ?? global::Sitecore.Context.Database;
if (qcArray != null && qcArray.Length > 0)
{
Item[] itemArray = new Item[qcArray.Length];
for (int i = 0; i < qcArray.Length; i++)
{
itemArray[i] = database.Items[qcArray[i].ID];
}
return itemArray;
}

if (qc != null)
return new Item[] { database.Items[qc.ID] };
return null;
}

Thursday, May 19, 2011

Cache a File and clear it when the file changes

There is a few to achieve this objective, one of the way to do this is by using

FileWatcher class.

however there is an easier approach to do this by using CacheDependency. here is a sample code

Cache.Insert("Name", myArrayList, New CacheDependency("C:\Images")
Cache will update itself if any files are added, deleted, or edited.

ref: http://www.velocityreviews.com/forums/t92933-cache-dependency-question.html

Wednesday, May 11, 2011

Migrating Legacy URL to New URL while maintaining SEO Ranking

Recently, I was asked to solve the issue of maintaining SEO ranking for an existing website that has a new IA implemented. Basically, to achieve this, we need to redirect the old IA which maybe obsolete/not existent to its corresponding IA. For this we have a map of old to new IA/Url.

There are a few ways to tackle this, one way is to write a processor that reads the url and check that against a config file for its corresponding new IA/url. however I may have found a more elegant solution by using Microsoft URLRewrite modules. Some links that I found useful

1. http://www.iis.net/download/URLRewrite
2. http://stackoverflow.com/questions/1173299/rewritemaps-with-an-external-config-to-force-an-application-pool-recycle

There is a potential leveraging this to make a Sitecore module which helps with one to one url mapping of old to new pages. This can be done by creating a "OldUrl" field for each item, with this data we can then create a Config file with old to new pages mapping which can then be fed to URLRewrite module.

ref: http://sitecorejohn.wordpress.com/2010/03/24/cmswire-microsoft-offers-seo-friendly-urls-with-url-rewriter/