Add a List Item Programmatically in SharePoint

The below code demonstrates how to use the SharePoint API to add a list items programmatically:

 
string dashListRootStr= "http://sharepointSite/SubSite/Lists/MyList/";
    using(SPSite site = new SPSite(dashListRootStr))
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPList list = web.Lists["MySPList"];
            SPListItem Item = list.Items.Add();
            item["Title"] = txtCompanyName.Text;
            item["DateReceived"] = System.DateTime.Now;
            item["Description"] = txtDescription.Text;
            //Important: Comma and space required to add the description
            item["EnquiryDetail"] = "http://sharepointSite/SubSite/ListProcess/Assign.aspx?EnquiryID=" + m_iEnquiryId + ", " + txtCompanyName.Text;
            item.Update();
            iListItemId = item.ID;
        }
    }



Array

No comments yet... Be the first to leave a reply!