Skip Ribbon Commands
Skip to main content

Point2Share | Daniel McPherson's SharePoint Blog

:

The zevenseas Community > Blogs > Point2Share | Daniel McPherson's SharePoint Blog > Posts > Announcing: MOSS SQL SearchCoder
June 04
Announcing: MOSS SQL SearchCoder

DOWNLOAD

Search in MOSS, from a programmatic perspective, has come a long way in MOSS, specifically it is more robust (though could still be MORE robust), more bug free and therefore more suited for use as plumbing in applications. I have found it particularly useful to get over some of the scale restrictions SharePoint places on developers, and taken advantage of it to meet the need for aggregation.

In a recent project however I found myself writing this little piece of spaghetti code.

   1: //Build the Select Statement
   2: string baseQuery =
   3:     @"SELECT Title, Rank, Description, Created, Path, NumComments, NumViews, NumLinkbacks, Tags, Categories FROM portal..scope()";
   4:  
   5: //Set the base scope
   6: string baseScope = @" (""SCOPE"" = 'All http://" + strSiteURL.Host + @"/ Blogs') ";
   7:  
   8: // Build the Where Clause
   9: string baseWhere;
  10: switch (tag)
  11: {
  12:     case "":
  13:         baseWhere = @"WHERE " + baseScope;
  14:         break;
  15:     default:
  16:         //TODO: MUST ADD TAG FIELD SUPPORT
  17:         baseWhere = @"WHERE CONTAINS (""Categories"",'""" + tag + @"""') AND " + baseScope;
  18:         break;
  19: }
  20:  
  21: //Build the OrderBy Clause
  22: string localSortField;
  23: if (_sortField == "")
  24:     localSortField = "Created";
  25: else
  26:     localSortField = _sortField;
  27:  
  28: string orderQuery;
  29: if (localSortField == "")
  30:     orderQuery = "";
  31: else
  32:     orderQuery = @"ORDER BY """ + localSortField;
  33:  
  34: //End the Query
  35: string endQuery = @""" DESC";
  36:  
  37: //Putting it all together
  38: string strsql = baseQuery + baseWhere + orderQuery + endQuery;
  39: Debug.Write(strsql);
  40:  
  41: return strsql;

I'm not proud of it, and couple of small bugs aside, was revisiting it to ensure it was "solid" (as if code like this could EVER be solid) when I threw in the towel, sick of writing code to write code every time I hit a project that needs to use search. There should be an easier/better way.

The result of my frustration is a tool that takes spaghetti code like this and turns it into:

   1: MOSSSearch mossSearch = new MOSSSearch();
   2:  
   3: ArrayList Scopes = new ArrayList();
   4: Scopes.Add("All http://" + strSiteURL.Host + @"/ Blogs");
   5: mossSearch.Scopes = Scopes;
   6:  
   7: ArrayList returnProperties = new ArrayList();
   8: returnProperties.Add("Title");
   9: returnProperties.Add("Path");
  10: returnProperties.Add("Rank");
  11: returnProperties.Add("Description");
  12: returnProperties.Add("Created");
  13: returnProperties.Add("NumComments");
  14: returnProperties.Add("NumViews");
  15: returnProperties.Add("NumLinkbacks");
  16: returnProperties.Add("Tags");
  17: returnProperties.Add("Categories");
  18: mossSearch.ReturnProperties = returnProperties;
  19:  
  20: WhereContains whereStatement = new WhereContains();
  21: whereStatement.FirstWhereProperty = "Categories";
  22:  
  23: if (!string.IsNullOrEmpty(tag))
  24: {
  25:     WhereProperty whereTags = new WhereProperty();
  26:     whereTags.Property = "Tags";
  27:     whereTags.SearchTerm = tag;
  28:     whereTags.LogOperator = WhereProperty.LogicalOperator.OR;
  29:     whereStatement.WhereProperties.Add(whereTags);
  30:  
  31:     WhereProperty whereCategories = new WhereProperty();
  32:     whereCategories.Property = "Categories";
  33:     whereCategories.SearchTerm = tag;
  34:     whereStatement.WhereProperties.Add(whereCategories);
  35: }
  36: mossSearch.Where = whereStatement;
  37:  
  38: SortStatement sortStatement = new SortStatement();
  39: sortStatement.SortProperty = _sortField;
  40: sortStatement.ResultSortOrder = SortDirection.Descending;
  41: mossSearch.SortStatement = sortStatement;
  42:  
  43: Debug.Write(mossSearch.GetSQLStatement());
  44: return mossSearch.GetSQLStatement();

Which looks much more elegant to me. Of course actually creating the query in the first place is tricky too, and while a couple of test tools are available, I wanted to put one together that:

  • Tested the SQL generated by the OM
  • Output not only the SQL Statement, but the OM code as well, making using it as simple as cut and paste.
  • Was inspired by the now missing in action tool MossQueryTool.exe written by SharePoint legend Steve Peschka (was on GotDotNet).

The result is the zevenseas SearchCoder and it looks like this:

image

Here you build your query graphically, when done you click on the "Build SQL" or "Execute Query" buttons which take you here:

image

Note the SQL Statement is generated, an on the C# SearchCoder OM Tab:

image

Anyway, this is the .9 release, still a little polishing to do, including:

- Add Sort order (it is in the OM, but not in the tool)

- Add support for multiple sort orders

- Better exception handling (of course!)

But I would rather it was out there than sitting on my hard disk, so grab it now from CodePlex:

CodePlex Project

DOWNLOAD

Comments

Chris OBrien

This looks pretty damn useful. I remember going through an exercise to de-spaghettify some search code not too long ago - next time I'll take a look at this, well done mate.

Daniel McPhersonNo presence information on 16/11/2008 07:05

Jessica Hardy

Oh yes.  We'll be using this one for sure.  Thanks, Dagsy.
System Account on 26/08/2009 06:51

cool stuff

It saved lot of time


<akhil/>
 on 28/11/2010 09:18

AHAPPYDEAL

Article was written well ,thanks for sharing.I also like to share a nice product
<a href=http://www.ahappydeal.com/hobbies-toys-c-103.html>cheap toys</a>I hope you will like it
 on 04/04/2013 23:57

Add Comment

Title *


Body *


CommentUrl


CAPTCHA *

 

Attachments

 

 Statistics

 
Views: 648
Comments: 3
Tags:
Published:1644 Days Ago