How can I pull the ID from a Lookup field in WSS 3.0

Hi experts:
Given two lists: lstStudents and lstReview where lstReview contains a lookup field to the 'Student Name' in lstStudents, how can I get the ID of the referenced student?

From my initial searching it seems there used to be a fairly easy way to accomplish this in WSS 2.0 and FrontPage, where you would just serparate the returned value on the '#' and the first part of the field would ID. It doesn't seem like this method works using WSS 3.0 with SDP 2007.

I also came across a reference to using the ddwrt:Urllookup function to accomplish this but when I tried <xsl:value-of select="ddwrt:UrlLookup('{GUID-of-List}','Student',string(@ID))"/> it doesn't return anything.

This seems like it should be simple, and I just know I'm going to facepalm when I get the answer!
Who is Participating?
Using SPDesigner in a clean web page (with attached master page if you want)

1)  open Data Source Library (menu bar -> Task Panes - Data Source Library)
2)  now that you have a list of list libraries in design view, right click on the student list library and click "insert data source control"
3) right click this datasource and make sure to choose ID and student name as your columns.  Make any other filter and sorting configurations at this point.
4)  then, open the toolbox and drag on drop a asp:dropdown (or datalist) control on your web page (do not drop into web part zone this will not work)
5)  right click on your dropdown control and go into the properties where you'll select the data source from step #2.
6) now create a data form web part (hope you know how to that) and drop that into a web part zone.
7)  you should still be in design view, so highlight this new DVWP using the mouse and on the SPD menu "Data View" find "Data View Parameters".
8)  Create a new parameter, call it "prmID"  and set the parameter source = Form and set the Form Field to the name of that drop down control from step 4.  Remember, this is an asp control so the name of this control would be something like "ctl00$PlaceHolderMain$DropDownList1".
9) Now, after you've completed these steps you can access the ID inside your DVWP using XSLT.  Here is the code for that:
<xsl:value-of select="$prmID"/>

A few steps but it works for me and it is very flexible solution.  

Good luck.
zephyr_hex (Megan)DeveloperCommented:
i can provide a little information about the structure behind lookups, although this doesn't answer your question directly.

see here:
All you need to do is split the string by ;#

So it will look like this:


U can use javascript, C#... it's just a split.

var myLU = "4;#steam23";
var myID = myLU.split(";#")[0];
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

steam23Author Commented:
Zaphyr Hex: That's an interesting articel. I'm going to play around with that if only to see a bit more of what's going on under the hood.
Raybies: Are you sure that is the case for WSS 3.0? Like I said I remember doing that kind of transformation with Wss 2.0 (with xsl) but I think they changed the way it works in 3.0.
 Here's the script I just tried:
       <script type="text/javascript">
       var luStudent = <xsl:value-of select="@Student"/>;
       var idStudent = luStudent.split(";#")[0];
       Document.write idStudent
To be fair, I'm not to familiar with mixing javascript an xslt in this fashion so I may be doing it wrong. However, the script as above didn't get any result.
steam23: I'm sure WSS3 stores lookups as <id>;#<title>

But your js is wrong. <xsl:value-of select="@Student"/> is not a valid string for the script engine.

Try setting a hidden input field value to <xsl:value-of select="@Student"/>, then using javascript like

<script type="text/javascript">
      var luStudent = document.getElementById('myHiddenFld').value;
      var idStudent = luStudent.split(";#")[0];
      Document.write idStudent

'myHiddenFld' should be unique id on the page.
steam23Author Commented:
sorry i didn't get to this sooner, I've been busy getting stuff wrapped up for year end.. so it goes.
I tried the changes you suggested, still with no result. Here's the code I used:
      <td width="25%" class="ms-vb">
         <SharePoint:FormField runat="server" id="student" controlmode="Edit" fieldname="Student" itemid="{@ID}" __designer:bind="{ddwrt:DataBind('u','student','Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Student')}" />
      <td width="75%" class="ms-vb">
       <script type="text/javascript">
       var luStudent = document.getElementById('student').value;
       var idStudent = luStudent.split(";#")[0];
       Document.write idStudent
       </script> </td>
With that code, I can see the sharepoint field no problem, but it's blank where the ID should be. For the record, I've tried formatting the Student field as a SP List Field, Text box and a label, all with the same result.
OK... I think I understand the issue.... your not getting back the ID of the lookup at all.

Read this:

It seems like the only way is to join the 2 lists together and get the ID.

By the way if you did have a string with <id>;#<title> the below works 100%, just save as an html file.

<input type="text" id="student" value="123;#Student Name">
<script type="text/javascript">
var luStudent = document.getElementById('student').value;
var arStudent = luStudent.split(";#");
alert("ID = "+ arStudent[0] +"\nName = "+ arStudent[1]);
document.write("ID = "+ arStudent[0] +"\nName = "+ arStudent[1]);

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.