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

The following code is what is I use to insert new list items:

 using (SPSite oSPsite  = SPContext.Current.Site)
 ' using (SPWeb oSPWeb = oSPsite.OpenWeb())
 ' 
 SPList list = oSPWeb.Lists["SampleList"];

 SPListItem itemToAdd = list.Items.Add();
 itemToAdd["Title"] = TextBox1.Text;
 itemToAdd.Update();

How do I do "itemToAdd["Title"] = TextBox1.Text", by modifying an existing item where ID=4 rather than add a new item?

share|improve this question
    
What do you mean "where ID = 4"? If the item has not been added to the list, it does not have an ID yet. –  Robert Kaucher Mar 27 '12 at 20:23
    
Assuming there are 10 items in the list with column x and y, I would like to add "SampleText" under field y if field x is "test" for that particular item. –  user7400 Mar 28 '12 at 1:03
    
That's not what you put in the question - do you want to update an item based on it's ID or based on the value of another field? –  Mike Redden Mar 28 '12 at 1:52
    
@MikeRedden I apologize if the question was not very clear. But what spdevsolutions has posted below is exactly what I was looking for.Thanks –  user7400 Mar 28 '12 at 13:17
add comment

2 Answers

up vote 2 down vote accepted

I was going to edit Mike's more, but here's a cleaner answer instead (sorry Mike!):

Basically there is no reason to make a new spsite object, also you should never really call .items (like ever, it will go get the entire list), and you don't need to call dispose on a spsite.Rootweb...

Anywhere here you go:

SPWeb oSPWeb = SPContext.Current.Site.RootWeb;
SPList list = oSPWeb.Lists["SampleList"];
SPListItem ItemToModify = list.GetItemById(4);
ItemToModify["Title"] = TextBox1.Text;
ItemToModify.Update();

As a side comment, since I don't have enough rep over here to comment on the question post: It isn't a good idea to do list.items.add() (MSOCAF may actually even error on this, don't remember), as calling .items will pull the entire list. Do this instead: http://sharepointdevsolutions.blogspot.com/2010/02/adding-items-to-large-lists.html

share|improve this answer
    
Thanks for the code snippet. It was exactly what I wanted to do. –  user7400 Mar 28 '12 at 13:19
add comment

To modify the title of an item in your list with an ID of 4, do the following:

using (oSPSite = new SPSite(SPContext.Current.Site.URL))
{
     SPWeb oSPWeb = oSPSite.RootWeb;
     SPList list = oSPWeb.Lists["SampleList"];
     SPListItem ItemToModify = list.Items.GetItemById(4);
     ItemToModify["Title"] = TextBox1.Text;
     ItemToModify.Update();

}

And another thing: NEVER put a site out of SPContext into a "using" statement - that will dispose of the current context and will cause a lot of heartache in the future. If you insist on having your own SPSite object instead of just utilising the context, create a new SPSite object like I have above.

share|improve this answer
    
Thanks again Mike. –  user7400 Mar 28 '12 at 13:19
add comment

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.