Moss I believe everyone understands the importance of the object model. The author has also outlined the previous chapter. Today, I will add it to this simple demo.
The same object is implemented using different methods.
SpsiteSite =New Spsite("Http: // mosingserver: 8080");
SpwebWeb = site. openweb ();
//You can also find the root website first.
// Spweb web = site. rootweb;
//You can directly find the current website through the current context when running on WSS.
// Spweb web = spcontext. Current. Web;
Web. allowunsafeupdates =True;
////Get List objects directly by document name
SplistList1 = web. Lists ["Shared document"];
////Obtain the list object from the path where the document is located.
// Splist list2 = web. getlistfromurl ("");
////Get List objects using guid
// Guid listid = new GUID ("guid ");
// Splist list3 = web. Lists [listid];
////Get the list object directly through the context object
// Splist list4 = spcontext. Current. List;
//****************************//
//Perform related operations on the list
// List1.title = "Microsoft productes ";
//Add a content type to the list
// List1.fields. Add ("texting", spfieldtype. Text, false );
//Set whether the list is displayed in the Quick Start column.
// List1.onquicklaunch = true;
// List1.update ();
//***************************//
//UseCodeDirectly create a list
// Guid newlistid = web. Lists. Add ("Microsoft product documentation "," store some product documentation ", splisttemplatetype. documentlibrary );
// Splist newlist = web. Lists [newlistid];
// Newlist. onquicklaunch = true;
// Newlist. Update ();
//**************************/
//When converting a Document Object (the document library object inherits from the list object, most operations in the document library can be performed through the list object model,
//Only objects in the document library are required for special operations in the document library.) to obtain the object in the document library, you must first obtain the corresponding list object and then convert the object type.
// Spdocumentlibrary doclib = (spdocumentlibrary) list1;
//Obtain all check-in documents
// Ilist <spcheckedoutfile> checkeoutdoc = doclib. checkedoutfiles;
//*************************//
//You can use the view API to add views to a list. Common Operations include adding, modifying, and deleting views. view objects depend on List objects and can be obtained from view set attributes of the list.
//View names, view IDs, and indexes can be used to obtain views.
//Obtained by view name
// Spview view1 = list1.views ["View name "];
//Obtained by view ID
// Spview view2 = list1.views [New GUID ("Gudi")];
//Obtained by index
// Spview view3 = list1.views [0];
//Add view (set display fields, filter, and pagination, same as the default view)
// Spview newview = list1.views. Add ("New View ", list1.defaultview. viewfields. tostringcollection (), list1.defaultview. query, list1.defaultview. rowlimit, list1.defaultview. paged, false );
// Newview. viewfields. Add (list1.fields ["2009/4/2"]);
// Newview. Update ();
//Operations related to list items
//Output all list items
SplistitemcollectionAllitems = list1.items;//Get list item set
Foreach(SplistitemItemInAllitems)
{
Console. Writeline (item. Title );
}
//Delete a list item
// Splistitem Item1 = list1.items [0];
// Item1.delete ();
//Add a list item
// Splistitem item2 = list1.items. Add ();
// Item2 ["Company address "] =" Guangzhou ";
// Item2 ["Production Time "] =" 2009/04/01 ";
//You can obtain a list item by using the list item ID.
// Splistitem Item1 = list1.items. deleteitembyid (1 );
// Splistitem item2 = list1.getitembyid (1 );
//After obtaining the list items, you can get the object through file, or know the file path to directly obtain the object through the spweb object. For the document library, the list items and file objects
//Yes, they can be converted to each other.
SpdocumentlibraryDoclibry = (Spdocumentlibrary) List1;
//Delete a list item
// Splistitem item3 = list1.items [0];
// Item3.delete ();
SplistitemItem4 = list1.items [1];
//Get object
SpfileFile2 = item4.file;
//Open file data
Byte[] Filedata = file2.openbinary ();
//Get object through File URL
// Spfile fiel3 = web. GetFile ("http: // mosingserver: 8080/doclib1/Forms/allitems. aspx ");
//Get the list item object through the file object
// Splistitem item3 = file2.item;
//First load the file to the BYT Array
//Open a file, read the file content into a string, and close it.
Byte[] Newfiledata = system. Io.File. Readallbytes (@ "C:" test.doc");
//And then add it to the folder's file set.
SpfileNewfile = doclibry. rootfolder. Files. Add ("Test.doc", Filedata );//The Code shows that the list is different from the file addition: the list is item. Add (),
//Files are added through the file. Add () object in the document library folder. The deletion and modification are the same!
Web. Dispose ();
site. dispose ();