<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>common mistakes &#8211; blog.boro2g .co.uk</title>
	<atom:link href="https://blog.boro2g.co.uk/tag/common-mistakes/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.boro2g.co.uk</link>
	<description>Some ideas about coding, dev and all things online.</description>
	<lastBuildDate>Thu, 23 Jun 2011 13:25:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.8</generator>
	<item>
		<title>Common mistakes when programming with Sitecore pt1</title>
		<link>https://blog.boro2g.co.uk/common-mistakes-when-programming-with-sitecore-pt1/</link>
					<comments>https://blog.boro2g.co.uk/common-mistakes-when-programming-with-sitecore-pt1/#respond</comments>
		
		<dc:creator><![CDATA[boro]]></dc:creator>
		<pubDate>Thu, 23 Jun 2011 13:22:09 +0000</pubDate>
				<category><![CDATA[Sitecore]]></category>
		<category><![CDATA[common mistakes]]></category>
		<category><![CDATA[sitecore]]></category>
		<guid isPermaLink="false">http://blog.boro2g.co.uk/?p=95</guid>

					<description><![CDATA[<p>The aim of this post is to highlight some pitfalls I have run into in the past when working with the Sitecore API. Hopefully some ideas demonstrated here will help people avoid some common mistakes when programming with Sitecore. Over time I&#8217;d like this list to grow so if anyone has any feedback or suggestions [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/common-mistakes-when-programming-with-sitecore-pt1/">Common mistakes when programming with Sitecore pt1</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The aim of this post is to highlight some pitfalls I have run into in the past when working with the Sitecore API. Hopefully some ideas demonstrated here will help people avoid some common mistakes when programming with Sitecore.</p>
<p>Over time I&#8217;d like this list to grow so if anyone has any feedback or suggestions for more items, please let me know.</p>
<p>For each item I will highlight some examples of where I have seen the mistakes and how they can best be avoided.</p>
<ol>
<li>Direct database access</li>
<li>Expensive content queries</li>
</ol>
<p><strong>1. Direct database access</strong><br />
There are several ways to get a reference to a Sitecore database. Note, these are defined within the config (&lt;databases&gt;). In the following example, the first 2 items get a specific database, the last the context database.</p><pre class="crayon-plain-tag">Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase(&quot;master&quot;);
Sitecore.Data.Database web = Sitecore.Configuration.Factory.GetDatabase(&quot;web&quot;);

Sitecore.Data.Database contextDatabase = Sitecore.Context.Database;</pre><p><strong><em>Why is this bad?</em></strong><br />
In larger Sitecore builds one of the common tasks is to run through the ideas stored in the following guides on the sdn: <a href="http://sdn.sitecore.net/reference/sitecore%206/security%20hardening%20guide.aspx">security hardening</a> and <a href="http://sdn.sitecore.net/developer/configuring%20production%20environments.aspx">configuring production environments</a>.</p>
<p>One of key steps in these documents is that the content delivery site only has a reference to the web database. Typically you would have an independent content authoring environment which has references to core, master and web. In this setup, if your code has a direct reference to master, you will get an exception since master doesnt exist.</p>
<p><strong><em>When might you want to do this?</em></strong><br />
It may be that in certain circumstances you do want to target a specific database. Consider an import routine. In this example you would want to ensure that new items are only added to master &#8211; note you would need to ensure the import routine is run from an environment which can access the master database.</p>
<p><strong>2. Expensive content queries</strong><br />
Sitecore.Data.Items.ItemAxes exposes a set of methods which can be used to drill up and down through the content tree. Some examples of this are:</p><pre class="crayon-plain-tag">namespace Sitecore.Data.Items
{
    // Summary:
    //     Provides access to other items relative to a source item.
    //
    // Remarks:
    //     Implements the Sitecore.Data.Items.Item.Axes property.
    //     You should never instantiate this class.
    public class ItemAxes
    {
        ...

        // Summary:
        //     Gets an ancestor item.
        //
        // Parameters:
        //   itemID:
        //     The item ID.
        //
        //   includeSelf:
        //     if set to true this instance is include self.
        //
        // Returns:
        //     The ancestor.
        public Item GetAncestor(ID itemID, bool includeSelf);
        
        // Summary:
        //     Gets all ancestors.
        //
        // Returns:
        //     The ancestors.
        public Item[] GetAncestors();
        //
        // Summary:
        //     Gets a child item.

        ...

        //
        // Summary:
        //     Gets a list of items that is in descendant axis of the source item.
        //
        // Returns:
        //     The descendants.
        public Item[] GetDescendants();            
    }
}</pre><p>Within your sublayouts you could then call eg:</p><pre class="crayon-plain-tag">Sitecore.Data.Items.Item[] descendants = Sitecore.Context.Item.Axes.GetDescendants();</pre><p><strong><em>Why is this bad?</em></strong><br />
If your content tree contains thousands of content items and you call GetDescendants from the root node &#8211; you will effectively be loading every single item in the tree &#8211; I can guarantee the bigger the tree, the slower this will go!</p>
<p><strong><em>When might you want to do this?</em></strong><br />
If you are comfortable that the result of a descendants call will expose a controlled set of nodes then you may find them more useful than querying direct children. An example of this is if folders are used in the structure you are querying.</p>
<p><strong><em>Where might you make this mistake?</em></strong><br />
A typical place I have seen this implemented is building footer navigation. Consider the following: &#8216;<em>A developer understands your template structure and sees there is a common base template for each page so adds a new checkbox &#8216;Show in footer navigation&#8217;. In the footer control they then start at the home node calling GetDescendants, checking each item for the new checkbox.</em>&#8216;</p>
<p><strong><em>What can I do instead?</em></strong><br />
In the footer example, try to consider alternative solutions for defining which items should be shown in the footer. How about a configuration area of the content tree where the footer navigation is defined as its own  node (and children if needed). Your descendants call could then target these specific items.</p>
<p>Other alternatives are using shallower axes for your queries for example: direct children or siblings.</p>
<p>The post <a rel="nofollow" href="https://blog.boro2g.co.uk/common-mistakes-when-programming-with-sitecore-pt1/">Common mistakes when programming with Sitecore pt1</a> appeared first on <a rel="nofollow" href="https://blog.boro2g.co.uk">blog.boro2g .co.uk</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.boro2g.co.uk/common-mistakes-when-programming-with-sitecore-pt1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
