SharePoint lookup column values

Recently I started to take look more about the stuff that SharePoint has as a out of box stuff. And one of these features is SPFieldLookUp column (you can create this kind of column in SharePoint list through UI). This column basically looks up stuff from another list and provides a way to easily add a kind of relation between two lists (only one way relation).

So how about a bit of coding (since I am pretty much coder). Now when you look through custom code (like Console Application or something that does not do same kind of rendering like default SharePoint to this column) you’ll see how the data is formatted. Lookup fields have selected data formatted something like this: itemID;#Display text;nextItemID;#Next Display Text and so on. At first I thought that it’s simple, just take it as a string and do something to that. After a little planning I realized that this is not the way to go.

Now the nice stuff comes along. After a bit of digging SharePoint API (and Visual Studio Intellisense) I found out that I can get it as SPFieldLookUpValue and SPFieldLookUpValueCollection (moster named classes;). Here is the code sample that saved my day:

SPFieldLookupValueCollection valueItems = (SPFieldLookupValueCollection)listItem["ColumnThatIsMultiLookupField"];

                            foreach (SPFieldLookupValue valueItem in valueItems)
                            {
                                //access your ID and Text by:

                               valueItem.LookupValue and valueItem.LookupID

                            }

Yet another small and beutiful discovery from SharePoint and it’s goodies.

[Update] BTW. This solution works with PeopleEditor in SharePoint. PeopleEditor uses same kind of structure with selected accounts (userID;#DisplayName).

Advertisements

About Sakarih

I am SharePoint developer/architect from Finland and my blogposts are all about SharePoint 2007/2010/2013 stuff. There might be also something else posted.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s