.Net & SharePoint '07

Technical blog for .Net and all SharePoint 2007 related Information

About the author

Me(Prince) and my wife are B.E in I.T & C.S.E respectively.  I a certified MCPD: Web from 2007 Dec. I am Intrestes in Web Application, MOSS, EPM, etc.
Now working with Deira International School, as IT Application & Help Manager. I have started my career as "Software Developer" @  REACH Sewn Technologies and Consulting Pvt. Ltd, Bangalore India from Oct 2004 to Feb 2006, then as "Web & Intranet Developer" @ Fosroc International Ltd, Dubai from April 2006 to Sep 2009.
You can catch me on mail@jpy-tech.com or mail@princepy.com. Or on 00971 - 50 - 4284530 

Google Translate

Tag cloud

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

RecentComments

Comment RSS

Google Your Location


Getting\Setting values from\to the lookup, hyperlink and people column fields

  • How to set/get data from a field that is a lookup?
  • How to set/get data into a field that is a hyperlink?
  • How to set/get user into a field that is a people or group? 

For both cases, sharepoint object model exposes classes to help us get or set the data we want. These are the SPFieldLookupValue and the SPFieldUrlValue.

Example 1: Set the url field of a link

Use the SPFieldUrlValue class o create an object that holds the url to link to, and the title to display:

SPList list = web.Lists["Links"];
SPListItem newLink = list.Items.Add();
SPFieldUrlValue value = new SPFieldUrlValue();
value.Description = "test";
value.Url = "http://www.microsoft.com/sharepoint";
newLink["URL"] = value;
newLink.Update();

Example 2: Get the url field of a link

Use the SPFieldUrlValue class to create an object that gets the url and description:

SPList list = web.Lists["Links"];
SPListItem existingLink = list.Items[0];
SPFieldUrlValue value = new SPFieldUrlValue(existingLink["URL"].ToString());
string linkTitle = value.Description;
string linkURL = value.Url;

Example 3: Set the value of a lookup field for a known title and ID

In the following example I am using SPFieldLookupValue to set the value of a lookup field ("Group Name") to item "Program Operations", whose ID is 14:

SPList list = web.Lists["Branches"];
SPListItem newBranch = list.Items.Add();
newBranch["Title"] = "A New Branch";
SPFieldLookupValue newValue = new SPFieldLookupValue(14,"Program Operations");
newBranch["Group Name"] = newValue;
newBranch.Update();

Example 4: Get the value of a lookup field from an item

Here I am reading the value of the group name field (which is a lookup field in the branches list):

SPList list = web.Lists["Branches"];
SPListItem existingBranch = list.Items[0];
SPFieldLookupValue group = new SPFieldLookupValue(existingBranch["Group Name"].ToString());
int lookedUpItemID = group.LookupId;
string lookedUpItemTitle = group.LookupValue;

link : http://www.sharepoint-tips.com/2007/10/code-practices-gettingsetting-values.html


Posted by admin on Tuesday, September 16, 2008 3:51 PM
Permalink | Comments (0) | Post RSSRSS comment feed