3

I have a visual webpart which creates a list item, but I need to reference the newly created item's ID for later use in the same webpart. How would I do this in C#?

Thanks

1 Answer 1

6

The value of the ID is invalid after Add() and but instead created during Update(). See here.

Example:

SPList itemList = web.Lists["Animal Owner"];
SPListItem newOwner = itemList.Items.Add();
newOwner["Title"] = txtNewOwnerName.Text;
newOwner.Update();

int newID = newOwner.ID; //Here I reference the ID from the newly created list item

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.