Reprint: http://www.cnblogs.com/kivenhou/archive/2013/02/22/2921954.html
Add SharePoint list Data
============================================
Using Microsoft.SharePoint;
SPWeb site = spcontrol.getcontextweb (Context);
SPListItemCollection items = site. lists["ListName"]. Items;
SPListItem item = items. ADD ();
item["field_1"] = Onevalue;
item["field_2"] = Twovalue;
Item. Update ();
Delete SharePoint list data
=============================================
Using Microsoft.SharePoint;
SPWeb site = spcontrol.getcontextweb (Context);
SPListItemCollection items = site. lists["ListName"]. Items;
Items[0]. Delete ();
Uploading Files to SharePoint
=============================================
Using System.IO;
Using Microsoft.SharePoint;
if (htmlinputfile1.postedfile! = null)
{
SPWeb site = new SPSite (destinationurl). OpenWeb ();
Stream stream = HtmlInputFile1.PostedFile.InputStream;
byte[] buffer = new Bytes[stream. Length];
Stream. Read (buffer, 0, (int) stream. Length);
Stream. Close ();
Site. Files.add (destinationurl, buffer);
}
Querying records and updating data
===============================================
Using Microsoft.SharePoint;
SPWeb Web = new SPSite ("Http://nick"). OpenWeb ("test"); Open website
Web. Allowunsafeupdates = true;
SPList list = web. lists["ListName"];
SPQuery query = new SPQuery ();
Query. Query = "<Where>" +
"<And><And>" +
"<eq><fieldref name=/" filed_1/"/><value type=/" text/">Test</Value></Eq>" +
"<eq><fieldref name=/" filed_2/"/><value type=/" text/">" + (String) Onevalue + "</value></ Eq> "+
"</And>" +
"<eq><fieldref name=/" filed_3/"/><value type=/" text/">" + (String) Twovalue + "</value></ Eq> "+
"</And>" +
"</Where>";
Query. RowLimit = 10;
Inquire
SPListItemCollection items = list. GetItems (query);
Try
{
if (Items.Count! = 0)
{
Update SharePoint list data
foreach (SPListItem list in listItems)
{
list["filed_1"] = TextBox1.text.ToString ();
list["filed_2"] = TextBox2.text.ToString ();
list["filed_3"] = TextBox3.text.ToString ();
Listitem.update ();
}
}
Else
{//Add data records into SharePoint
SPListItem addlist = List.Items.Add ();
addlist["filed_1"] = TextBox1.Text.ToString ();
addlist["filed_2"] = TextBox2.Text.ToString ();
addlist["filed_3"] = TextBox3.Text.ToString ();
Addlist. Update ();
}
}
Catch
{
}
C # Some basic operations on SharePoint lists, including adding/deleting/querying/Uploading files to SharePoint list add data