C # Some basic operations on SharePoint lists, including adding/deleting/querying/Uploading files to SharePoint list add data

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.