Archive for April, 2009


I’m heading to RailsConf next week!

While I don’t spend the majority of my time coding with Ruby in Rails, I really do enjoy it and was excited at the opportunity to attend my very first RailsConf this year. I’ve heard nothing but great things about this conference and am hoping to up my ruby and rails skills a little bit.

Anyone else planning to go?

RailsConf 2009

Ok, if you’re like me, writing CAML queries can sometimes make your head hurt. Not because it’s terribly complicated, but because there’s not much useful information out there that demonstrates, with REAL examples, how these should be written. So let’s see if we can clarify this:

Scenario 1

Get me all items in a list WHERE fullName equals the currently logged in user.

1
2
3
4
5
6
7
8
9
10
11
SPWeb web = SPControl.GetContextWeb(Context);
 
string fullName = web.CurrentUser.Name;
 
SPQuery oQuery = new SPQuery();
 
oQuery.Query = 
    "<Where>" + 
    "<Eq><FieldRef Name='FullName'/><Value Type='Text'>'" + fullName + "'</Value></Eq>" + 
    "</Where>" +
    "<OrderBy><FieldRef Name='StartTime' Ascending='FALSE'></FieldRef></OrderBy>";

Scenario 2

Get me all items in a list WHERE fullName equals the currently logged in user AND status equals ‘Complete’.

1
2
3
4
5
6
7
8
9
10
11
12
13
SPWeb web = SPControl.GetContextWeb(Context);
 
string fullName = web.CurrentUser.Name;
 
SPQuery oQuery = new SPQuery();
oQuery.Query = 
    "<Where>" +
    "<And>" +
        "<Eq><FieldRef Name='FullName'/><Value Type='Text'>'" + fullName + "'</Value></Eq>" +
        "<Eq><FieldRef Name='Status'/><Value Type='Text'>Complete</Value></Eq>" + 
    "</And>" +
    "</Where>" +
    "<OrderBy><FieldRef Name='StartTime' Ascending='FALSE'></FieldRef></OrderBy>";

Scenario 3

Get me all items in a list WHERE fullName equals the currently logged in user AND status equals ‘Complete’ AND manager is James Lane.

This is where it gets a bit tricky. The following example is INCORRECT and will produce an error when run:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SPWeb web = SPControl.GetContextWeb(Context);
 
string fullName = web.CurrentUser.Name;
 
SPQuery oQuery = new SPQuery();
oQuery.Query = 
    "<Where>" +
    "<And>" +
        "<Eq><FieldRef Name='FullName'/><Value Type='Text'>'" + fullName + "'</Value></Eq>" +
        "<Eq><FieldRef Name='Status'/><Value Type='Text'>Complete</Value></Eq>" +
        "<Eq><FieldRef Name='Manager'/><Value Type='Text'>James Lane</Value></Eq>" +
    "</And>" +
    "</Where>" +
    "<OrderBy><FieldRef Name='StartTime' Ascending='FALSE'></FieldRef></OrderBy>";

This is the correct way to do it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SPWeb web = SPControl.GetContextWeb(Context);
 
string fullName = web.CurrentUser.Name;
 
SPQuery oQuery = new SPQuery();
oQuery.Query = 
    "<Where>" +
    "<And>" +
        "<And>" +
            "<Eq><FieldRef Name='FullName'/><Value Type='Text'>'" + fullName + "'</Value></Eq>" +
            "<Eq><FieldRef Name='Status'/><Value Type='Text'>Complete</Value></Eq>" +
        "</And>" +
        "<Eq><FieldRef Name='Manager'/><Value Type='Text'>James Lane</Value></Eq>" +
    "</And>" +
    "</Where>" +
    "<OrderBy><FieldRef Name='StartTime' Ascending='FALSE'></FieldRef></OrderBy>";

Scenario 4

Get me all items WHERE fullName equals the currently logged in user AND status equals ‘Complete’ OR status equals ‘On Hold’.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SPWeb web = SPControl.GetContextWeb(Context);
 
string fullName = web.CurrentUser.Name;
 
SPQuery oQuery = new SPQuery();
oQuery.Query = 
    "<Where>" +
    "<And>" +
        "<Eq><FieldRef Name='FullName'/><Value Type='Text'>'" + fullName + "'</Value></Eq>" +
        "<Or>" +
            "<Eq><FieldRef Name='Status'/><Value Type='Text'>Complete</Value></Eq>" +
            "<Eq><FieldRef Name='Status'/><Value Type='Text'>On Hold</Value></Eq>" +
        "</Or>" +
    "</And>" +
    "</Where>" +
    "<OrderBy><FieldRef Name='StartTime' Ascending='FALSE'></FieldRef></OrderBy>";

I really do recommend if you’re doing any work with CAML queries that you download the U2U CAML Query Builder 2007. It doesn’t come without it’s issues, for example, doing a query on a list with a column of type Lookup does not always yield the results I would expect, but it’s still quite helpful. I also often find myself changing the Value Type from Lookup to Text but other than that it’s a huge resource when trying to determine if the data is actually in the list or if there is a problem with the query i’ve written.

Any feedback or questions, please drop me a line.

Developing custom web applications, workflows or web parts within SharePoint require some level of error handling to ensure smooth operation of your custom solution.

There are several techniques for doing this such as writing to a file or to the event log, but I came across a fantastic article a while back and just recently came around to trying it out. I’m happy to report that it works quite well.

The source of this information comes from the Blue Surf Tech blog titled The Sharepoint way for Application Error Alerts.

Now just to summarize the technique here:

1. Create a SharePoint custom list with custom columns based on the type of information you want to capture. For example you may want to capture the exception source, stack trace or inner exception to name a few. Or you may simply want to log informational messages that indicate areas of success in your code. The link above does a great job of describing the former, I’ll outline the latter in the next few steps.

2. The next step is to write the method that will log the information messages at specific intervals within your code to the custom SharePoint list you just built. Here is what my LogInfo method looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void LogInfo(string message, string sItemUrl, int sItemID, string sTitle)
{
    // create an handle to the current web context (works only in application pages)
    SPWeb web = SPContext.Current.Web;
 
    // create new item
    SPListItem item = web.Lists["Application Log"].Items.Add();
 
    item["Title"] = sTitle;
    item["Application"] = "SharePoint Employee List - Edit Form";
    item["Url"] = sItemUrl;
    item["ItemID"] = sItemID;
    item["Message"] = message;
    item["User"] = web.CurrentUser.Name;
 
    // save
    item.Update();
}

3. Finally, I just pick specific areas in my methods or workflow, for example an button submit event or a SendEmail method, to log the successes or any other type of message that might be helpful to track.

1
LogInfo("Successful item update.", listItem.Url, listItem.ID, listItem.Title);

There you have it, let me know how it works out. Feedback is most welcome.

Criteria For Hiring a Good Developer

If anyone is interested, Black Ninja posted a relevant article on what we consider to be a good developer. The article in reference is actually written by Daniel over at inter-sections.net and is the best thing I’ve seen to date that describes how we feel about web development and the community we’re a part of.

I’ll caution everyone that’s it meant to be a guide, and not something we need to live by to the letter. Feedback is most welcome!

Powered by WordPress | Theme: Motion by 85ideas.