0

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.

1
  • The answer depends on what you are trying to accomplish. You may be moving data from another system or adding some cross-site automation from within SharePoint. Tell us what you are trying to accomplish. Commented Jul 4, 2011 at 18:38

3 Answers 3

4
SPWeb app = SPContext.Current.Web;
SPList ListName = app.Lists["YourListName"];
SPListItem ListItem = ListName.Items.Add();
ListItem["field1Name"] = value;
ListItem["field2Name"] = value;
ListItem.Update();
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

-1

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

1 Comment

I'll do mysite.update(), but on which file i have to include the code? is there an existing file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.