Go to C # some basic operations on the Sharepoint list, including adding/deleting/querying/uploading files to the Sharepoint list to add data

Source: Internet
Author: User

========================================================== ====
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 ();


Upload a file 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 );
}

Query records and update 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>" +
"<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;

// Query
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 to 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
{
 
}

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1516166

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.