0

I can create a list and add a custom column to it but there are no items present in the list when I try to add them programmatically. Below is the code snippet.

Guid listId = web.Lists.Add("TestList", "TestList", SPListTemplateType.GenericList);
SPList list = web.Lists[listId];
list.OnQuickLaunch = false;
list.Fields.Add("Enabled", SPFieldType.Boolean, true);
list.Update();

SPListItem item = list.AddItem();
item["Title"] = "ABC";
item["Enabled"] = true;
list.Update();

item = list.AddItem();
item["Title"] = "DEF";
item["Enabled"] = true;
list.Update();
1

Try doing:

item.Update()

instead of:

list.Update()

1

You can try like below :

            SPListItem itemToAdd = list.Items.Add();
            itemToAdd["Title"] = "Test Title";
            itemToAdd["Description"] = "Test Description";
            itemToAdd.Update();
  • Please add useful parts of the post in your answer, as link-only answers tend to get void if the link is broken. – Arsalan Adam Khatri Dec 30 '14 at 9:27
0

You can try below code :

 using (SPSite site = new SPSite("Your siteurl"))
        {
            using (SPWeb web = site.OpenWeb())
            {
                web.AllowUnsafeUpdates = true;

                SPList list = web.Lists["Your listName"];
                SPListItem item = list.Items.Add();
                item["Name"] = txtName.Text;
                item["Address"] = txtAdd.Text;
                item["Phone"]=txtPhn.Text;
                item.Update();
                web.AllowUnsafeUpdates = false;
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.