How to get data from a field that is a lookup? how to set data into a field that is a hyperlink?
I don't believe I didn't write about this yet. It's so common!
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 to 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;
'IT 관련 이야기 > SharePoint ' 카테고리의 다른 글
Sharepoint 하위 사이트 마스터페이지 및 테마 일괄적용법 (0) | 2008.06.15 |
---|---|
MOSS search 관련 이벤트 오류 발생에 대한 핫픽스 (0) | 2008.06.14 |
Sharepoint 및 MOSS PDF iFilter 8 - 64-bit Support (0) | 2008.05.16 |
Sharepoint Navigation handle (0) | 2008.05.07 |
Activating Office SharePoint Server Publishing Infrastructure - Access Denied (2) | 2008.04.04 |