March 14, 2012

Add a custom section in the config

Here is the method if you need to a custom section into you config file.

Here is my example of config that I need to include into my \App_Config\Include folder:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
 <MyCustomSection>
  <MyCustomElement Name="XXX" MyProperty="YYY" />
  <MyCustomElement Name="AAA" MyProperty="BBB" />
 </MyCustomSection>
  </sitecore>
</configuration>

And here is a way to read it:
using System.Collections.Generic;
using Sitecore.Configuration;
using System.Xml;
using Sitecore.Xml;

/// <summary>
/// TODO: Update summary.
/// </summary>
public class MyConfigElement
{
 //The two properties corresponding to the properties into the config
 public string Name { get; set; }
 public string MyProperty { get; set; }

 //The method who return the list of my custom elements
 public static List<MyConfigElement> GetListMyCustomElements()
 {
  List<MyConfigElement> lst = new List<MyConfigElement>();
  
  //Read the configuration nodes
  foreach (XmlNode node in Factory.GetConfigNodes("MyCustomSection/MyCustomElement"))
  {
   //Create a element of this type
   MyConfigElement elem = new MyConfigElement();
   elem.Name = XmlUtil.GetAttribute("Name", node);
   elem.MyProperty = XmlUtil.GetAttribute("MyProperty", node);
   lst.Add(elem);
  }
  return lst;
 }
}

Don’t forget to use a property “Name” to avoid the conflicts in the include folder (see this post: http://sitecoreblog.blogspot.com/2012/02/config-patching-system-for-external.html)

March 2, 2012

Include the sc namespace

When you have multiple projects in a single solution you will maybe have this error message when you try to use some sitecore controls like the sc:Text:

You have to simple solutions to fix this problem:

You may add this line on the top of each files:
<%@ Register Assembly="Sitecore.Kernel" Namespace="Sitecore.Web.UI.WebControls" TagPrefix="sc" %>


Or you may add a web.config into your project with the minimal information to include this namespace.
But don’t deploy this web.config in your solution because it will override the correct sitecore web.config
<?xml version="1.0"?>
<configuration>
  <system.web>
    <pages validateRequest="false">
      <controls>
        <add tagPrefix="sc" namespace="Sitecore.Web.UI.WebControls" assembly="Sitecore.Kernel" />
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add tagPrefix="sc" namespace="Sitecore.Web.UI.WebControls" assembly="Sitecore.Analytics" />
      </controls>
    </pages>
  </system.web>
</configuration>