SharePoint Events

  5/13/2013 - Conference: SharePoint Summit 2013
  5/21/2013 - Webcast: SharePoint 2013 and ECM: Content Migration and Storage
  5/22/2013 - Webcast: Managing CAD in SharePoint
  5/23/2013 - Webcast: SharePoint Document Automation and E-Forms for Financial Services
  5/24/2013 - Webcast: What's New in Search for SharePoint 2013

 SharePoint Videos

  Why SharePoint 2013
  SharePoint 2013 Launch
  SharePoint 2013 Migration and Governance
  SharePoint 2013 and Enterprise Content Management
  Top Benefits of SharePoint 2013
  What's New in Business Intelligence in Office and SharePoint 2013
  SharePoint and Office 2013 Integration
  SharePoint 2013 Infrastructure Preview
  SharePoint, Lync, and, Exchange in the Cloud with Office 365
  Advanced Reporting in SharePoint with Microsoft Power View

 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).
1 - 10 Next
Programmatically Dealing With Potential Multi-Select Columns

By: Arjun Chakraborty

Problem:

Often it is tempting to simply assign a column value to an SPFieldLookupValue, or an SPFIeldUserValue.  However, there is a danger to this practice; the end user may at some point decide to change a SPUser column type into a multiple select SPUser column.  Today’s article addresses this issue.

Solution:

(lookup columns and user columns):

            My first approach was to see if there is a difference in:

 

                                                Item.Fields[columnName].GetType().Name

 

However, both a lookup field and a multi-select lookup field return "SPFieldLookup".   My next approach was to use Collections, such as:

 

·         SPFieldLookupValueCollection

·         SPFieldUserValueCollection

 

So, it comes out to something like this:

 

string result = string.Empty;

if (workflowProperties.Item.Fields[columnName].GetType().Name == "SPFieldLookup")

{

   SPFieldLookupValueCollection mluv =

      new SPFieldLookupValueCollection (workflowProperties.Item[columnName].ToString());

   bool firstChoice = true;

   foreach (SPFieldLookupValue v in mluv)

   {

      if (firstChoice)

      {

         result = result + v.LookupValue;

         firstChoice = false;

      }

      else

         result = result + ", " + v.LookupValue;

   }

}

 

This will in effect, return each value selected by the user in this column.  Even if the column is set back to single select lookup, this code will still function.  This will also work for User/Group columns as well if you replace:

 

   SPFieldLookupValueCollection mluv =

      new SPFieldLookupValueCollection (workflowProperties.Item[columnName].ToString());

 

With:

 

SPFieldUserValueCollection muv =

new SPFieldUserValueCollection(workflowProperties.Web, workflowProperties.Item[columnName].ToString());

 

And replace:

 

if (workflowProperties.Item.Fields[columnName].GetType().Name == "SPFieldLookup")

 

 

With:

 

if (workflowProperties.Item.Fields[columnName].GetType().Name == "SPFieldUser")

 

(Multi-choice column):

 

What will be a little different is the multiple choice column.  There is no catch-all value collection implementation for a multi-choice column.

 

Instead, we use SPFieldMultiChoiceValue, as an array.

 

Replace:

 

if (workflowProperties.Item.Fields[columnName].GetType().Name == "SPFieldLookup")

 

With:

 

if (workflowProperties.Item.Fields[columnName].GetType().Name == "SPFieldMultiChoice")

 

And replace the foreach loop with the following for loop:

 

SPFieldMultiChoiceValue mcv =

   new SPFieldMultiChoiceValue(workflowProperties.Item[columnName].ToString());

bool firstChoice = true;

for (int i = 0; i < mcv.Count; i++)

{

   if (firstChoice)

   {

      result = result + mcv[i].ToString();

      firstChoice = false;

   }

   else

      result = result + ", " + mcv[i].ToString();

}

 

I hope that helps!  Also, make sure to check if the field has a non-null value, and if the field actually exists for the item as well…

 

By: Arjun Chakraborty

        

Comments

There are no comments yet for this post.
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

  On Demand SharePoint Webcast Recordings
  Upcoming Webinars
  SharePoint Resources
  Business Intelligence Resources
  Gig Werks Website



©2009 Gig Werks. All rights reserved. Privacy Policy