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

Posted on 2009-12-16
Last Modified: 2013-11-18
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!
Question by:steam23
    LVL 42

    Expert Comment

    i can provide a little information about the structure behind lookups, although this doesn't answer your question directly.

    see here:
    LVL 16

    Expert Comment

    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];

    Author Comment

    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.
    LVL 16

    Expert Comment

    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.

    Author Comment

    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.
    LVL 16

    Expert Comment

    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

    LVL 5

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Preface In the first article: A Better Website Login System ( I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now