Database db = Sitecore.Context.ContentDatabase;
LinkDatabase linkDb = Globals.LinkDatabase;
// lookupItem is the multilist item
ItemLink[] links = linkDb.GetReferrers( lookupItem );
foreach( ItemLink link in links )
{
Item linkedItem = db.Items[link.SourceItemID];
// Process item...
}
Wednesday, 28 October 2009
Get a list of all Sitecore Items tagged with specific multilist items
If you wish to return all items in the content tree that have been tagged with a specific multilist item, you might be tempted to use Sitecore.Context.Database.SelectSingleItem(), passing it an XPath query. The problem with this is that performance degrades as the content tree grows. A far more efficient way is to use the GetReferrers method of the LinkDatabase class as shown below:
Monday, 12 October 2009
Example Useful C# Extension Functions
Here are a few extension functions that I find useful:
The latter two functions call this method:
public static bool IsNullOrEmpty(
this string expression )
{
bool result = string.IsNullOrEmpty(
expression );
return result;
}
public static string ToFormat(
this string expression, params object[] args )
{
string result = String.Format(
expression, args );
return result;
}
public static bool IsValidEmailAddress(
this string expression )
{
bool result = BaseRegexHelper.IsValid(
expression, emailRegex );
return result;
}
public static bool IsValidGuid(
this string expression )
{
bool result = BaseRegexHelper.IsValid(
expression, guidRegex );
return result;
}
The latter two functions call this method:
public static bool IsValid(
string input, string pattern )
{
bool isValid = false;
if( input != null )
{
string trimmedExpression =
input.Trim();
if( trimmedExpression.Length > 0 )
{
Regex regexGuid = new Regex(
pattern, RegexOptions.IgnoreCase );
isValid = regexGuid.IsMatch(
trimmedExpression );
}
}
return isValid;
}
Labels:
C#
Friday, 14 August 2009
Write to Sitecore log file from XSLT
It is simple to write to the Sitecore log file from within an XSLT file without having to write any custom code. We can just hook up to the Sitecore.Diagnostics.Log class and call one of its methods.
In the web.config, define the extension under <xslextensions>:
Within the XSLT file, add a prefix that matches the namespace you used in the above step:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sc="http://www.sitecore.net/scReal"
xmlns:dot="http://www.sitecore.net/dot"
xmlns:log="http://www.sitecore.net/log"
exclude-result-prefixes="dot sc log">
The Log class contains a number of overloads for Info, Warn, Error and so on. Some of these expect an exception as a parameter, so we cannot call these direct from XSLT. We can, however match the overloads that expect a string and an object (eg. Warn( String, Object )):
<xsl:value-of select="log:Warn('Warning message', 'Another string... perhaps the name of your rendering file')" />
That's it! Now you can log whatever you like from within your renderings.
In the web.config, define the extension under <xslextensions>:
<extension mode="on"
type="Sitecore.Diagnostics.Log, Sitecore.Kernel"
namespace="http://www.sitecore.net/log"
singleInstance="true" />
Within the XSLT file, add a prefix that matches the namespace you used in the above step:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:sc="http://www.sitecore.net/scReal"
xmlns:dot="http://www.sitecore.net/dot"
xmlns:log="http://www.sitecore.net/log"
exclude-result-prefixes="dot sc log">
The Log class contains a number of overloads for Info, Warn, Error and so on. Some of these expect an exception as a parameter, so we cannot call these direct from XSLT. We can, however match the overloads that expect a string and an object (eg. Warn( String, Object )):
<xsl:value-of select="log:Warn('Warning message', 'Another string... perhaps the name of your rendering file')" />
That's it! Now you can log whatever you like from within your renderings.
Labels:
Sitecore,
Tips,
Troubleshooting,
Xslt
Thursday, 6 August 2009
Using Linq lambda expressions to retrieve XML elements based on the value of an attribute
The following example program loads an XML file, then selects all the elements named "field" within it. It then uses a lambda expression to get the inner text of the "alternativeTitle" element.
class Program
{
static void Main( string[] args )
{
XDocument xd = XDocument.Load( "..\\..\\file.xml" );
var fields = from el in xd.Descendants()
where el.Name.ToString().StartsWith( "field" )
select el;
Console.WriteLine( GetElementValue(fields, "alternativeTitle") );
Console.ReadLine();
}
static string GetElementValue( IEnumerable<XElement> elements, string attributeName )
{
return elements.Single( p => p.Attributes().Single( q => q.Name == "name" ).Value == attributeName ).Value;
}
}
The XML file used in the example is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<add>
<doc>
<field name="id">some_id</field>
<field name="readableid">interesting_publications</field>
<field name="name">Interesting Publications</field>
<field name="alternativeTitle">Online publications</field>
</doc>
</add>
The program outputs the text:
Online publications
class Program
{
static void Main( string[] args )
{
XDocument xd = XDocument.Load( "..\\..\\file.xml" );
var fields = from el in xd.Descendants()
where el.Name.ToString().StartsWith( "field" )
select el;
Console.WriteLine( GetElementValue(fields, "alternativeTitle") );
Console.ReadLine();
}
static string GetElementValue( IEnumerable<XElement> elements, string attributeName )
{
return elements.Single( p => p.Attributes().Single( q => q.Name == "name" ).Value == attributeName ).Value;
}
}
The XML file used in the example is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<add>
<doc>
<field name="id">some_id</field>
<field name="readableid">interesting_publications</field>
<field name="name">Interesting Publications</field>
<field name="alternativeTitle">Online publications</field>
</doc>
</add>
The program outputs the text:
Online publications
Tuesday, 28 July 2009
Enable XSLT IntelliSense within Visual Studio
Wouldn't it be nice to have Visual Studio list the available template names when typing out a call-template statement in XSLT? Wouldn't it also be nice to have it list available parameter names when typing out a with-param statement?
To enable these autocompletions and more, you need to back up your registry and then run regedit. Within regedit, create a string value called "XsltIntellisense" under "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\XmlEditor" key, giving it the value "True". That's it!
Ultimately, I'd like to see full IntelliSense for XSLT in Visual Studio, but this is a good start.
To enable these autocompletions and more, you need to back up your registry and then run regedit. Within regedit, create a string value called "XsltIntellisense" under "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\XmlEditor" key, giving it the value "True". That's it!
Ultimately, I'd like to see full IntelliSense for XSLT in Visual Studio, but this is a good start.
Labels:
Tips,
Visual Studio,
Xslt
The type initializer for 'Sitecore.Search.SearchManager' threw an exception
If you get this (or similar) error, check that the version of the Sitecore.Kernel.dll referenced by class libraries is the same as used by the web site. It fixed a problem I had anyway!
Labels:
Sitecore,
Troubleshooting
Tuesday, 14 July 2009
ASP.NET Accessibility Top Ten
Here are my top ten tips for designing accessible ASP.NET sites. These are based on my understanding and opinion. If I am radically wrong about any of these, please feel free to correct me by leaving a comment.
Always set the display of validation controls to ‘dynamic’
In this way, screen readers ignore the text of the error message when there is no error.
Do not use image buttons
They are invisible to screen readers. Use a button with a background image instead.
Do not use AutoPostBack
They are impossible to navigate using a keyboard.
Do not use separator characters between links
Characters in breadcrumbs, footers, etc. are read out by screen readers. Use a span instead with a style instead. Or better still, use a bulleted list with a background image.
Display sets of results or links in a bulleted list
In a landing page or search results page, having each result marked up as a bullet makes it easier to navigate between results.
Insert hidden header text at the start of every navigation list
Screen reader users can navigate by headers and choose to skip the navigation lists.
Surround the individual digits in telephone numbers with span tags
Otherwise, the whole number will be read out as a single value.
Use hidden images with pagination and other non-descriptive hyperlinks
If pagination links are in the format: 1 | 2 | 3, a screen reader will read the links as ‘link 1’, ‘link 2’ and so on. So, insert a hidden image that has the alt text set to something more descriptive.
Include file sizes and types in hyperlink text
Unwittingly clicking a link to a file attachment can cause confusion.
Don’t bother using access keys
They are device-dependent, they can conflict with other shortcut keys and there is no standardisation as to what each key combination should do. Better to have clear navigation.
Always set the display of validation controls to ‘dynamic’
In this way, screen readers ignore the text of the error message when there is no error.
Do not use image buttons
They are invisible to screen readers. Use a button with a background image instead.
Do not use AutoPostBack
They are impossible to navigate using a keyboard.
Do not use separator characters between links
Characters in breadcrumbs, footers, etc. are read out by screen readers. Use a span instead with a style instead. Or better still, use a bulleted list with a background image.
Display sets of results or links in a bulleted list
In a landing page or search results page, having each result marked up as a bullet makes it easier to navigate between results.
Insert hidden header text at the start of every navigation list
Screen reader users can navigate by headers and choose to skip the navigation lists.
Surround the individual digits in telephone numbers with span tags
Otherwise, the whole number will be read out as a single value.
Use hidden images with pagination and other non-descriptive hyperlinks
If pagination links are in the format: 1 | 2 | 3, a screen reader will read the links as ‘link 1’, ‘link 2’ and so on. So, insert a hidden image that has the alt text set to something more descriptive.
Include file sizes and types in hyperlink text
Unwittingly clicking a link to a file attachment can cause confusion.
Don’t bother using access keys
They are device-dependent, they can conflict with other shortcut keys and there is no standardisation as to what each key combination should do. Better to have clear navigation.
Labels:
Accessibility,
ASP.NET,
Tips
Subscribe to:
Posts (Atom)