Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Can someone please post sample code which shows how to deal with a lookup column during an item insert (using object model).

Here is the basic framework for an insert:

SPListItem oListItem = oList.AddItem();

    // Text field - very easy
    oListItem["Title"] = SomeStringValue;

    //Hyperlink - requires some work but format is like this:
    oListItem["HyperlinkField"] = URL+ ", " + Desc;

So what I'm missing is how to add a lookup field, thanks in advance

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The class you're looking for is the SPFieldLookupValue class. MSDN has a good example.

In short, something like:

SPListItem lookedUpItem =  GetItemSomeHow();
newItem[lookupField] = new SPFieldLookupValue( lookedUpItem.ID, lookedUpItem.Title);
newItem.Update();

I'd also recommend setting your hyperlink field using the SPFieldUrlValue class.

share|improve this answer
    
Very nicely done, didn't know about the SPFieldURLValue... –  user879 Dec 23 '10 at 10:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.