Fabian
Fabian

Reputation: 3

Add items to existing SharePoint lists by coding

I want to add items to a still existing SharePoint list by self-coding.

So I searched the internet and found a lot of information how to create lists, add items and so on..

SPWeb mySite = SPContext.Current.Web;
SPListItemCollection listItems = mySite.Lists[TextBox1.Text].Items;
SPListItem item = listItems.Add();

The coding seems easy, but where i have to put the code? I programmed Web Parts and deployed these to my web application. This was no problem, but here i am missing an approach.

I am using VisualStudion 2008 and SharePoint WSS 3.0

Thank you for any help.

Upvotes: 0

Views: 5638

Answers (3)

Govind
Govind

Reputation: 544

you are doing some modification on site ...so after that we need to update the site ..Use ListItem.update();

Upvotes: -1

naivists
naivists

Reputation: 33511

It depends on the purpose - why are you doing this?

  • Do you have one specific list where you (as the owner/administrator) need to create items and this has to be done only once? If yes, then you can do it with Powershell, as in this Karine Bosch's post.
  • If you need your users to be able to add items to list using program code, then web parts is the way to go. Then you will want to build a nice user interface for that webpart. And then you can deploy your webpart to your server, create a page in your site and add the webpart to the page.
  • If you need to create the new list item as soon as something else happens (for instance, a list item is created in some other list, then think of event handlers.

Upvotes: 2

Shankar
Shankar

Reputation: 448

SPWeb app = SPContext.Current.Web;
SPList ListName = app.Lists["YourListName"];
SPListItem ListItem = ListName.Items.Add();
ListItem["field1Name"] = value;
ListItem["field2Name"] = value;
ListItem.Update();

Upvotes: 4

Related Questions