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

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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