0

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

1 Answer 1

1

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.

1
  • Very nicely done, didn't know about the SPFieldURLValue...
    – user879
    Commented Dec 23, 2010 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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