Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, 13 July 2011

Lucene: Compression support not configured

I got this error when trying to extract Hits.Doc from Lucene:

Compression support not configured

at SupportClass.CompressionSupport.CheckCompressionSupport()
at SupportClass.CompressionSupport.Uncompress(Byte[] input)
at Lucene.Net.Index.FieldsReader.Uncompress(Byte[] input)
at Lucene.Net.Index.FieldsReader.AddField(Document doc, FieldInfo fi, Boolean binary, Boolean compressed, Boolean tokenize)
at Lucene.Net.Index.FieldsReader.Doc(Int32 n, FieldSelector fieldSelector)
at Lucene.Net.Index.SegmentReader.Document(Int32 n, FieldSelector fieldSelector)
at Lucene.Net.Index.IndexReader.Document(Int32 n)
at Lucene.Net.Search.IndexSearcher.Doc(Int32 i)
at Lucene.Net.Search.Hits.Doc(Int32 n)


The solution was to add the following missing keys into Sitecore AppSettings.config:

<add key="Lucene.Net.FSDirectory.class" value="Sitecore.Data.Indexing.FSDirectory, Sitecore.Kernel" />
<add key="Lucene.Net.CompressionLib.class" value="Sitecore.IO.Compression, Sitecore.Kernel" />

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:

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...
}

Monday, 12 October 2009

Example Useful C# Extension Functions

Here are a few extension functions that I find useful:


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;
}

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

Wednesday, 8 July 2009

Save Settings to Web.Config file

Here's a quick snippet showing how to save a setting back to the web.config file. You are best wrapping this block in a try/catch in case the ASPNET user (or equivalent) does not have write access permissions to the file.

Configuration config = WebConfigurationManager.OpenWebConfiguration( "~/" );
AppSettingsSection appSettings = config.GetSection( "appSettings" ) as AppSettingsSection;

if ( appSettings != null )
{
if ( appSettings.Settings ["mySetting"] != null )
{
appSettings.Settings.Remove( "mySetting" );
}
appSettings.Settings.Add( "mySetting", text );
}
config.Save( ConfigurationSaveMode.Modified );

Wednesday, 15 April 2009

SqlParameter is Already Contained in Another SqlParameterCollection

This exception occurred between calls to a method I used to populate a SqlCommand object.

SqlCommand cmd = conn.CreateCommand();

...

foreach (SqlParameter sqlParameter in sqlParameters)
{
cmd.Parameters.Add(sqlParameter );
}


The solution was to clear the parameter collection at the end of the method (in a finally block, actually):

cmd.Parameters.Clear();

Wednesday, 4 July 2007

How to Set the Maximum Length of a MultiLine TextBox

The MaxLength property of a TextBox control has no effect when the TextMode is set to MultiLine. The reason for this is that a MultiLine TextBox is rendered as a <textarea> tag, which does not have a maximum length attribute:

<textarea name="txtComments" rows="2" cols="20" id="txtComments">

In contrast, a TextBox with its TextMode set to SingleLine is rendered as an <input> tag, which has the maxlength attribute:

<input name="txtComments" type="text" maxlength="5" id="txtComments" />

If you wish to restrict the amount of text in a MultiLine TextBox, the workaround is to use a RegularExpressionValidator control. In the following example, the validation expression allows between 0 and 5 alphanumeric characters to be entered:

string strMaxLength = "5";

revComments.ValidationExpression = "\\w{0," + strMaxLength + "}$";