Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
SPQuery Hacks Part 1: InfoPathUse SHIFT+ENTER to open the menu (new window).
Adventures in Excel Services 2010Use SHIFT+ENTER to open the menu (new window).
What Is The Easiest Way To Mess Up SharePoint? Use SHIFT+ENTER to open the menu (new window).
Redirect SharePoint Navigation - NYC SDUG Quick DipUse SHIFT+ENTER to open the menu (new window).
SPQuery Hacks Part 2: WorkflowsUse SHIFT+ENTER to open the menu (new window).
Upgrading SharePoint 2007 RTM to SharePoint 2010 Use SHIFT+ENTER to open the menu (new window).
Using JQuery to add charts to your Data ViewsUse SHIFT+ENTER to open the menu (new window).
Quick Rundown: Multi-line Text ColumnsUse SHIFT+ENTER to open the menu (new window).
Document Previews Won’t Open In FAST Search Using HTTPSUse SHIFT+ENTER to open the menu (new window).
Sleazy Reporting: SharePoint 2010 ListsUse SHIFT+ENTER to open the menu (new window).
Helpful Debugging with SharePoint C Sharp CodeUse SHIFT+ENTER to open the menu (new window).
Configuring Remote Blob Storage in SharePoint 2010Use SHIFT+ENTER to open the menu (new window).
Getting E-mail to Work in Your SharePoint 2010 Dev EnvironmentUse SHIFT+ENTER to open the menu (new window).
Sleazy Reporting: SharePoint 2010 External Content TypesUse SHIFT+ENTER to open the menu (new window).
Programmatically Dealing With Potential Multi-Select ColumnsUse SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup Columns

By: Kevin Lin

At some point or another, as a SharePoint developer, you will have to deal with lookup columns. Unfortunately, lookup columns are not as easy to handle as "Single line of text" columns. Fortunately, there is a plethora of information to aid you in this ordeal. The main thing I wanted to discuss with this blog post is how to programmatically deal with blank lookup columns to ensure that your code will function properly even though the lookup column is not populated, and also how the programmatic nature of a lookup column will vary when a lookup list expands beyond a certain point.

The first thing to note is that lookup lists come in two types. In the image below, the Game lookup contains a small amount of items and the Gamer Score lookup contains a large number of items. The Game lookup would be the first type, which is a normal drop down lookup column whereas the Gamer Score lookup would be the second type which not only changes the way the arrow looks but allows for the user to enter text searches to help filter through the list. Once a lookup list grows large enough, SharePoint will convert it from the first type to the second time.

clip_image002

Now that we know that there are two different types of lookup lists, let's try to make an simple workflow. This workflow will take the Game and Gamer Score lookups and put them in a separate comment field. Below is the code:

 

private void SetComment_ExecuteCode(object sender, EventArgs e)

{

    SPListItem item = workflowProperties.Item;

 

    SPFieldLookupValue gameLook = new SPFieldLookupValue(item["Game"].ToString());           

    string game = gameLook.LookupValue;

 

    SPFieldLookupValue gamerScoreLook = new SPFieldLookupValue(item["Gamer Score"].ToString());           

    string gamerScore = gamerScoreLook.LookupValue;

 

    string comment = GetComment(game, gamerScore);

 

    item["Comment"] = comment;

    item.Update();

}

private string GetComment(string game, string score)

{

    return "Game: "+game+ Environment.NewLine+"Score: "+ score;

}

 

Since lookup column data converted to string format looks like "id#value" SPFieldLookupValue is used to parse the value part.

Below is an image of the results when the workflow is run. We see that when run, the workflow shows Error Occurred only when the Game lookup column is blank. But interestingly enough, there is no error when the Gamer Score lookup column is blank. What could be happening?

clip_image004

When coders don't know what's going on there is only one thing left to do, use the debugger. Upon debugging, we find the gamerScoreLook variable appears to be throwing an NullReferenceException. Let's remedy that with code.

clip_image006

Let's try checking for nulls with the code below, in addition I will add "Blank" if the column is blank. Below is an updated version of the code, the highlighted parts are the changes:

 

private void SetComment_ExecuteCode(object sender, EventArgs e)

{

    SPListItem item = workflowProperties.Item;

 

    string game = "Blank";

    string gamerScore = "Blank";

               

    if (item["Game"] != null)

    {

        SPFieldLookupValue gameLook = new SPFieldLookupValue(item["Game"].ToString());

        game = gameLook.LookupValue;

    }

 

    if (item["Gamer Score"] != null)

    {

        SPFieldLookupValue gamerScoreLook = new SPFieldLookupValue(item["Gamer Score"].ToString());

        gamerScore = gamerScoreLook.LookupValue;

    }

 

    string comment = GetComment(game, gamerScore);

 

    item["Comment"] = comment;

    item.Update();

}

 

private string GetComment(string game, string score)

{

    return "Game: "+game+ Environment.NewLine+"Score: "+ score;

}

Let's try running our modified code. Below are the results when the workflow is run again on all items, what's interesting is that Score is still empty, it is not recording a "Blank" what could be happening?

clip_image008

Let's go back to the debugger and see what is going on, we will focus on seeing what value is stored in the gamerScore string. It appears that gamerScore is getting flipped from "Blank" to null. How is this possible? We already have an if statement detecting nulls.

clip_image010

In order to find the source of the problem we will have to add the items to quick watch. The values of item["Game"] and item["Gamer Score"] are listed below and we see that they have different values despite both being lookup columns. This is where my first point I mentioned comes into play. Remember that there are two types of lookup columns, the moment the lookup becomes the second type with a query box the programmatic nature changes. The item lookup field is never null even when blank, however when blank the LookupValue is null.

clip_image012

Keeping that in mind, let's rewrite the code again to also check for a blank or null lookup value.

 

private void SetComment_ExecuteCode(object sender, EventArgs e)

{

SPListItem item = workflowProperties.Item;

 

string game = "Blank";

string gamerScore = "Blank";

           

if (item["Game"] != null)

{

              SPFieldLookupValue gameLook = new SPFieldLookupValue(item["Game"].ToString());

              if (!string.IsNullOrEmpty(gameLook.LookupValue))

              {

                     game = gameLook.LookupValue;

              }

}

 

if (item["Gamer Score"] != null)

{

              SPFieldLookupValue gamerScoreLook = new SPFieldLookupValue(item["Gamer Score"].ToString());

               if (!string.IsNullOrEmpty(gamerScoreLook.LookupValue))

              {

                           gamerScore = gamerScoreLook.LookupValue;

              }

}

 

string comment = GetComment(game, gamerScore);

 

item["Comment"] = comment;

item.Update();

}

 

private string GetComment(string game, string score)

{

return "Game: "+game+ Environment.NewLine+"Score: "+ score;

}

Below are the results, the code finally works correctly.

clip_image014

On a final note, it is also possible to use (string)item["Game"] instead of item["Game"].ToString() and the correct code for that is listed below:

 

private void SetComment_ExecuteCode(object sender, EventArgs e)

{

    SPListItem item = workflowProperties.Item;

 

    string game = "Blank";

    string gamerScore = "Blank";

 

    SPFieldLookupValue gameLook = new SPFieldLookupValue((string)item["Game"]);

    if (!string.IsNullOrEmpty(gameLook.LookupValue))

    {

        game = gameLook.LookupValue;

    }   

 

    SPFieldLookupValue gamerScoreLook = new SPFieldLookupValue((string)item["Gamer Score"]);

    if (!string.IsNullOrEmpty(gamerScoreLook.LookupValue))

    {

        gamerScore = gamerScoreLook.LookupValue;

    }

   

    string comment = GetComment(game, gamerScore);

 

    item["Comment"] = comment;

    item.Update();

}

 

private string GetComment(string game, string score)

{

    return "Game: "+game+ Environment.NewLine+"Score: "+ score;

}

I hope you found this guide useful.  If anything is unclear, or there are any issues feel free to post a comment or shoot me an email at klin@gig-werks.com

-- Kevin W. Lin

        

Comments

Brian Brinley

While I can see how this would be useful, your scenerio I feel gives people the wrong idea when to use something like this.

You use a workflow to set the comment, which is an aggregate of two fields. A computed column would be a much better solution.

In any case, thanks for taking the time for the writeup and im sure i'll find a use for this.
at 3/26/2010 11:15 AM

Jordans 3

The blog article very surprised to me! Your writing is good. In this I learned a lot! Thank you!
at 6/18/2010 4:08 AM

Jordan AJF8

Thanks for getting the time to talk about this, I sense strongly about it and really enjoy understanding extra on this subject. If probable, as you obtain expertise, would you thoughts updating your weblog with alot more specifics? It really is really useful for me.
at 7/5/2010 11:55 PM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  SharePoint Resources
  Business Intelligence Resources
  Upcoming Webinars



©2009 Gig Werks. All rights reserved. Privacy Policy