Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

My goal is to simply submit a new item to a SharePoint list programmatic-ally through a custom web part that I'm building, however, I'm having trouble putting some pieces together. There are plenty of blogs out there detailing the code, but none of them from start to finish.

I would like to take the data entered into two text boxes and submit the data on a button push to a list. This is an example of what I grabbed off the blo:

    using (SPSite site = new SPSite("http://website url/" ))
// Or SPContext.Current.Site.Url
{
    using (SPWeb Web = site.OpenWeb())
    {
            Web.AllowUnsafeUpdates = true;

            // Open List
             SPList list = Web.Lists["MyList"];
        // Add new item in List
             SPListItem item = list.Items.Add();
             item["Title"] = "Test Title";
             item["Description"] = "Test Description";
             item.Update();

            // Get Item ID
             listItemId = item.ID;

            // Update the List item by ID
             SPListItem item = list.GetItemById(listItemId);
             item["Description"] = "Update Description";
             item.Update();

            // Delete item
             SPListItem item = list.GetItemById(listItemId);
             item.Delete();

            Web.AllowUnsafeUpdates = false;
       }
 }
share|improve this question

1 Answer 1

up vote 1 down vote accepted
SPList list = SPContext.Current.Web.Lists["MyList"]; //Instead of MyList use name of your list
SPListItem newItem = list.Items.Add(); //Adding new item to the MyList
item["Title"] = "Test Title"; //Title is the field name
item["Description"] = "Test Description"; //Description is the field name
item.Update(); //Saving the new item

The above code will add a new item in the List, you need to change the name of the list accordingly... ["Title"] and ["Description"] are the field names, so whatever fields/columns you have in the list, you can assign the values using ListItem["FieldName"] = value (Here field name is Internal, more on Internal, Static and Name properties of field)

EXTRA INFORMATION
If you are using Visual Web Part SPContext.Current will have the current context... Current context has all the information like the User currently logged in, the SPWeb user is currently in and so on!

share|improve this answer
    
I understand how this code works, I'm just not sure how to deploy it. –  Ghoztrider Feb 5 '13 at 18:46
    
What version of Visual Studio and SharePoint are you using? If its SP 2010 with Visual Studio 2010, you can simply right-click the Project and click deploy to deploy your Visual Web Parts! –  Arsalan Adam Khatri Feb 12 '13 at 16:26
    
Hi Arsalan, Thanks for the reply. I wasn't sure what tools I needed to deploy this, I just found out I need a copy of Sharepoint server to create custom web parts using VS for my employer. Not sure that will happen. –  Ghoztrider Feb 12 '13 at 21:02
    
@Ghoztrider exactly you will need VS to develop custom Visual Web Parts and deploy them. –  Arsalan Adam Khatri Feb 12 '13 at 21:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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