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开发者_StackOverflow中文版.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.
SPWeb app = SPContext.Current.Web;
SPList ListName = app.Lists["YourListName"];
SPListItem ListItem = ListName.Items.Add();
ListItem["field1Name"] = value;
ListItem["field2Name"] = value;
ListItem.Update();
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.
you are doing some modification on site ...so after that we need to update the site ..Use ListItem.update();
精彩评论