Wss3sdk: how to add and delete list items

Source: Internet
Author: User
To add data entries to the list, you can use Splistitemcollection Class Add Method To create a list item object, and then call Splistitem Class Update Method to write a new item to the database.
The following example assumes that there are five Textbox, one to specify the name of the list to be added, and the other four to specify the added value. Collect information from these five input sources through indexes.
Note: :
In this example, Microsoft. Sharepoint. spcontent class members are used to obtain the current website set, website, and list. In addition to the HTTP context, such as in a command line application or a Windows application, this method cannot be used.

VB

Dim Mysite As Spweb = Spcontext. Current. Web
Dim Listitems As Splistitemcollection
= Mysite. Lists (textbox1.text). Items

DimItemAsSplistitem=Listitems. Add ()

Item ( " Title " ) = Textbox2.text
Item ( " Stock " ) = Convert. toint32 (textbox3.text)
Item ( " Return date " ) = Convert. todatetime (textbox4.text)
Item ( " Employee " ) = Textbox5.text

Item. Update ()

C #

Spweb mysite = Spcontext. Current. Web;
Splistitemcollection listitems = Mysite. Lists [textbox1.text]. items;

Splistitem item=Listitems. Add ();

Item [ " Title " ] = Textbox2.text;
Item [ " Stock " ] = Convert. toint32 (textbox3.text );
Item [ " Return date " ] = Convert. todatetime (textbox4.text );
Item [ " Employee " ] = Textbox5.text;

Item. Update ();
}

CodeFirstAddMethod To createSplistitemObject. Then, specify the field value by using the index on the list item. For example, item ["title"] specifies the value of the title bar of the list item. Finally, the Code callsUpdateMethod: Write the change back to the database.
The above example must reference the Microsoft. SharePoint namespace.

If the list item to be created contains the metadata to be retained, we can useAuthor,Editor,CreatedAndModifiedThe field is used as the index, whereAuthorOrEditorSpecify a WSS userID. For more information, seeSplistitemClass.

To delete a project from the list, we can use the delete method of the splistitemcollection class. This method requires the index of the set as the parameter.
VB

Dim Mysite As Spweb = Spcontext. Current. Web
Dim Listitems As Splistitemcollection
= Mysite. Lists (textbox1.text). Items
Dim Itemcount As   Integer   = Listitems. Count
Dim K As   Integer

for K = 0 to itemcount - 1
dim item as splistitem = listitems (k)

IfTextbox2.text=Item ("Employee"). Tostring ()Then
Listitems. Delete (k)
End If
NextK

C #

Spweb mysite = Spcontext. Current. Web;
Splistitemcollection listitems = Mysite. Lists [textbox1.text]. items;
Int Itemcount = Listitems. count;

for ( int K = 0 ; k itemcount; k ++ )
{< br> splistitem item = listitems [k];

If(Textbox2.text=Item ["Employee"]. Tostring ())
{
Listitems. Delete (k );
}
}

Based on the input information in the two Textbox, the Code traverses the data item set in the specified list and deletes the data item whose employee field is the specified value.
The above example must reference the Microsoft. SharePoint namespace.

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.