Thursday, October 13, 2011

Sitecore Search - MustNot Query

From personal experience, it seems that it is not possible to run a Sitecore.Search.CombinedQuery using only a single fieldquery set to Sitecore.Search.QueryOccurance.MustNot.  For example, if you wanted to run a query against an index that returned items that didn't have a value of "1" (checked) in an "Inactive" checkbox field, you might try doing:

Sitecore.Search.CombinedQuery completequery = new Sitecore.Search.CombinedQuery();

Sitecore.Search.QueryBase fq1 = new Sitecore.Search.FieldQuery("Inactive", "1");
completequery.Add(fq1, Sitecore.Search.QueryOccurance.MustNot);


var Index = Sitecore.Search.SearchManager.GetIndex(IndexName);
Sitecore.Search.SearchResultCollection results;
using (Sitecore.Search.IndexSearchContext context = new Sitecore.Search.IndexSearchContext(Index))
{
    var hits = context.Search(completequery);
    results = hits.FetchResults(0, hits.Length);
}


However, this will not ever return results by itself - again, because it seems like a MustNot cannot be the lone clause in a CombinedQuery.  An easy way around this is to add the following code (where TemplateID = the ID of the template from your index):

//Have to have a "Must" query - the lone MustNot for fq1 will not work by itself.  This is simply a "placeholder" type query that supplies the "Must"
//By itself, this query should not eliminate nor incorporate any additional records from the index.
Sitecore.Search.QueryBase fq0 = new Sitecore.Search.FieldQuery(Sitecore.Search.BuiltinFields.Template,Sitecore.Data.ShortID.Encode(TemplateID).ToLowerInvariant());
completequery.Add(fq0, Sitecore.Search.QueryOccurance.Must);

Wednesday, October 12, 2011

Sitecore Search and Standard Values

As far as I am able to tell, it is not possible to do a Sitecore.Search (Lucene) fieldquery (or any other query for that matter) that will return items using the standard value for the field you're querying.

For example - Let's say you have an item with a checkbox field "DisplayOnPage", and the standard values for the Items template has "DisplayOnPage" set to be checked by default (thus the standard value for this field is "1").  If you do a field query against an index containing your item, and look for items where "displayonpage" = "1", your item that is using the standard value (again, to set the value of the DisplayOnPage field to 1) will NOT return as a result.  If, on your item, you uncheck the DisplayOnPage field, save the item, then recheck the DisplayOnPage field, save again and publish - your item will be returned by your FieldQuery. 

I believe the cause to this is that standard value fields are not stored by items explicitly in the database, so the Sitecore Search doesn't make the connection that your item implicitly has the value you're searching for.

Tuesday, October 11, 2011

Precompiled Website with Sitecore

If you are using a Web Deployment Project to precompile a .NET website that is run by Sitecore, you must check the "Allow this precompiled site to be updateable" checkbox on the WDP property pages or you will get an error when trying to access the Sitecore desktop.  The error will tell you something to the effect of "The file '/sitecore/default.aspx' has not been pre-compiled, and cannot be requested"

Grant Execute Access to all Stored Procs

When setting up a new user for a .NET website, the user often needs execute rights on stored procedures.  The easiest way to do this is the following SQL command:

/* CREATE A NEW ROLE */
CREATE ROLE db_executor

/* GRANT EXECUTE TO THE ROLE */
GRANT EXECUTE TO db_executor

After that, just set your new user account up to be in the "db_executor" role for the database they need execute permissions on.

SQL Find Text

If you need to find text in a SQL stored procedure or function, the following query is helpful:

SELECT ROUTINE_NAME, ROUTINE_DEFINITION
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%[InsertTextHere]%'