Partial Moss operations on the list

Source: Internet
Author: User

1. query all fields in the list

View plaincopy to clipboardprint?
Spsite site = new spsite ("http: // mosssite /");

Spweb web = site. openweb ();

Splist list = web. getlist ("/information ");

Foreach (spfield SF in list. fields)

{

Console. writeline (SF. Title );

}

Site. Dispose ();

Web. Dispose ();
Spsite site = new spsite ("http: // mosssite /");

Spweb web = site. openweb ();

Splist list = web. getlist ("/information ");

Foreach (spfield SF in list. fields)

{

Console. writeline (SF. Title );

}

Site. Dispose ();

Web. Dispose ();
 

2. Create a list using the object model. splisttemplatetype. Announcements specifies that the notification content type is used as the template to create the list.

Be sure to call the update () method.

View plaincopy to clipboardprint?
String listname = "testlist ";

Foreach (splist currlist in Web. Lists)

{

If (currlist. Title. Equals (listname, stringcomparison. invariantcultureignorecase ))

{

List = currlist;

Break;

}

}

If (list = NULL)

{

Guid listid = web. Lists. Add (listname, "New nnouncements", splisttemplatetype. Announcements );

List = web. Lists [listid];

List. onquicklaunch = true;

List. Update ();

}
String listname = "testlist ";

Foreach (splist currlist in Web. Lists)

{

If (currlist. Title. Equals (listname, stringcomparison. invariantcultureignorecase ))

{

List = currlist;

Break;

}

}

If (list = NULL)

{

Guid listid = web. Lists. Add (listname, "New nnouncements", splisttemplatetype. Announcements );

List = web. Lists [listid];

List. onquicklaunch = true;

List. Update ();

}
 

3. Use the object model to add items to the list. Be sure to call the update () method.

View plaincopy to clipboardprint?
Splistitem newitem = NULL;

Newitem = List. Items. Add ();

Newitem ["title"] = "annouitem1 ";

Newitem ["body"] = "the first annouitem1 ";

Newitem ["expires"] = datetime. Now + timespan. fromdays (2 );

Newitem. Update ();

Newitem = List. Items. Add ();

Newitem ["title"] = "annouitem2 ";

Newitem ["body"] = "the second annouitem2 .";

Newitem ["expires"] = datetime. Now + timespan. fromdays (5 );

Newitem. Update ();
Splistitem newitem = NULL;

Newitem = List. Items. Add ();

Newitem ["title"] = "annouitem1 ";

Newitem ["body"] = "the first annouitem1 ";

Newitem ["expires"] = datetime. Now + timespan. fromdays (2 );

Newitem. Update ();

Newitem = List. Items. Add ();

Newitem ["title"] = "annouitem2 ";

Newitem ["body"] = "the second annouitem2 .";

Newitem ["expires"] = datetime. Now + timespan. fromdays (5 );

Newitem. Update ();
 

4. query item-related information

View plaincopy to clipboardprint?
Foreach (splistitem listitem in list. Items)
{
Foreach (spfield field in list. fields)

{

If (! Field. Hidden &&! Field. readonlyfield)

Console. writeline ("{0 }={ 1}", field. Title, newitem [field. ID]);

}

}
Foreach (splistitem listitem in list. Items)
{
Foreach (spfield field in list. fields)

{

If (! Field. Hidden &&! Field. readonlyfield)

Console. writeline ("{0 }={ 1}", field. Title, newitem [field. ID]);

}

}
 

5. If you want to get a list item, you can get it through webid, listid, and ID.

View plaincopy to clipboardprint?
Spweb parentweb = web. Site. openweb (New GUID (row ["webid"]. tostring ()));

Splist list = parentweb. Lists [New GUID (row ["listid"]. tostring ()];

Splistitem item = List. getitembyid (Int. parse (row ["ID"]. tostring ())));
Spweb parentweb = web. Site. openweb (New GUID (row ["webid"]. tostring ()));

Splist list = parentweb. Lists [New GUID (row ["listid"]. tostring ()];

Splistitem item = List. getitembyid (Int. parse (row ["ID"]. tostring ())));
 

6. Use spquery to query the item information in the list:

View plaincopy to clipboardprint?
Spquery query = new spquery ();
Query. viewfields = @ "<fieldref name = 'title'/> <fieldref name = 'created '/> ";

Query. query = @ "<where>

<NEQ>

<Fieldref name = 'created '/>

<Value type = 'datetime'>

<Today/> </value>

</NEQ>

</Where> ";

Splist list = web. Lists ["annoulist"];

Splistitemcollection items = List. getitems (query );

Foreach (splistitem expireditem in items)

{

Console. writeline (expireditem ["title"]);

Console. writeline (expireditem ["created"]);

}
Spquery query = new spquery ();
Query. viewfields = @ "<fieldref name = 'title'/> <fieldref name = 'created '/> ";

Query. query = @ "<where>

<NEQ>

<Fieldref name = 'created '/>

<Value type = 'datetime'>

<Today/> </value>

</NEQ>

</Where> ";

Splist list = web. Lists ["annoulist"];

Splistitemcollection items = List. getitems (query );

Foreach (splistitem expireditem in items)

{

Console. writeline (expireditem ["title"]);

Console. writeline (expireditem ["created"]);

}
 

6.1. viewfields indicates the field to be returned after your query

6.2. query indicates the expression used for query filtering. It uses the caml language.

7. Use spsitedataquery to query item information in the list

View plaincopy to clipboardprint?
Spsitedataquery query = new spsitedataquery ();

Query. Lists = @ "<lists servertemplate = '2013'/> ";

Query. viewfields = @ "<fieldref name = 'title'/> <fieldref name = 'created '/> ";

Query. webs = "<webs scope = 'sitecollect'/> ";

String querytext mailto :=@% 22% 3 cwhere>

<NEQ>

<Fieldref name = 'created '/>

<Value type = 'datetime'>

<Today/> </value>

</NEQ>

</Where> ";

Query. query = querytext;

Datatable table = web. getsitedata (query );

Foreach (datarow row in table. Rows)

{

Console. writeline (row ["title"]. tostring () + row ["created"]. tostring ());

}
Spsitedataquery query = new spsitedataquery ();

Query. Lists = @ "<lists servertemplate = '2013'/> ";

Query. viewfields = @ "<fieldref name = 'title'/> <fieldref name = 'created '/> ";

Query. webs = "<webs scope = 'sitecollect'/> ";

String querytext mailto :=@% 22% 3 cwhere>

<NEQ>

<Fieldref name = 'created '/>

<Value type = 'datetime'>

<Today/> </value>

</NEQ>

</Where> ";

Query. query = querytext;

Datatable table = web. getsitedata (query );

Foreach (datarow row in table. Rows)

{

Console. writeline (row ["title"]. tostring () + row ["created"]. tostring ());

}

 

7.1. query. Lists = @ "<lists servertemplate = '000000'/>"; "104" indicates the notification list type.

7.2 query. webs = "<webs scope = 'sitecollect'/>"; indicates the query range.

7.3. the basic format of caml is as follows: "<where> <operator> <operand/> </operator> </where> ".

. Splistitemcollection is returned using spquery, and spsitedataquery can be queried from different lists or the entire website set. In fact, An ADO. Net datatable object is returned.

This article from csdn blog http://blog.csdn.net/showilove/archive/2009/08/18/4458927.aspx

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.