The following is a brief introduction to the moss object model. We hope that those who have access to the moss will get started quickly and hope everyone can make common progress.
Protected void getsitesandgroups ()
{
String userlist = response code. htmlencode (textbox1.text) +
"Is a user in the following webs: <br> ";
Spsite mysite = spcontext. Current. Site;
Spwebcollection allwebs = mysite. allwebs;
Foreach (spweb subweb in allwebs)
{
String listgroups = "";
/* Use allusers not users to ensure you find the user */
Spusercollection allusers = subweb. allusers;
Foreach (spuser user in allusers)
{
If (user. loginname. toupper () = textbox1.text. toupper ())
{
Spgroupcollection allgroups = user. Groups;
Foreach (spgroup group in allgroups)
{
Listgroups + = response code. htmlencode (group. Name) + "";
}
Userlist + = subweb. serverrelativeurl. tostring () +
"--" + Listgroups + "<br> ";
}
}
}
Label1.text = userlist;
}
// Use spweb to traverse all lists
Static void main (string [] ARGs)
{
Spsite sitecollection = new spsite ("http: // SERVER_NAME ");
Spwebcollection sites = sitecollection. allwebs;
Foreach (spweb site in sites)
{
Splistcollection lists = site. lists;
Console. writeline ("site:" + site. Name + "lists :"
+ Lists. Count. tostring ());
}
Console. writeline ("press enter to continue ");
Console. Readline ();
}
Spweb mysite = spcontext. Current. Web;
Output. Write (response code. htmlencode (mysite. Title ));
Spwebcollection subsites = mysite. webs;
Foreach (spweb site in subsites)
{
Output. Write (response code. htmlencode (site. Title) + "<br> ");
Splistcollection lists = site. lists;
Foreach (splist list in lists)
{
If (list. itemcount> 10)
{
Output. Write (response code. htmlencode (list. Title) + ":" +
List. itemcount + "<br> ");
}
}
}
// Add a web
Spweb mysite = spcontext. Current. Web;
Spwebcollection subsites = mysite. webs;
String currenttemplate = mysite. webtemplate;
String siteurl = textbox1.text. tostring ();
String sitetitle = textbox2.text. tostring ();
String sitedescription = textbox3.text. tostring ();
Subsites. Add (siteurl, sitetitle, sitedescription, 1033,
Currenttemplate, true, false );
// Add a web to customize the specified Template
Spweb mysite = spcontext. Current. Web;
Splistcollection lists = mysite. lists;
String listtitle = textbox1.text;
String listdescription = textbox2.text;
String listtype = listbox1.selecteditem. text;
Splisttemplatetype listtemplatetype = new splisttemplatetype ();
Switch (listtype)
{
Case "generic list ":
{
Listtemplatetype = splisttemplatetype. genericlist;
Break;
}
Case "events ":
{
Listtemplatetype = splisttemplatetype. events;
Break;
}
Case "announcements ":
{
Listtemplatetype = splisttemplatetype. announcements;
Break;
}
}
Lists. Add (listtitle, listdescription, listtemplatetype );
Spsite sitecollection = spcontext. Current. Site;
Spweb mysite = spcontext. Current. Web;
Splisttemplatecollection listtemplates
= Sitecollection. getcustomlisttemplates (mysite );
Splisttemplate listtemplate = listtemplates ["custom list template"];
Mysite. Lists. Add ("custom list", "a list created from
A custom list template in the list template catalog ", listtemplate );
// Delete the specified list
Spweb mysite = spcontext. Current. Web;
Splistcollection lists = mysite. lists;
Splist list = lists [textbox1.text];
System. guid listguid = List. ID;
Lists. Delete (listguid );
// Query
Spweb mysite = spcontext. Current. Web;
Splist list = mysite. Lists ["books"];
Spquery query = new spquery ();
Query. query = "<where> <GT> <fieldref name = 'stock'/> <value type = 'number'> 100 </value> </GT> </where>";
Splistitemcollection myitems = List. getitems (query );
Foreach (splistitem item in myitems)
{
Response. Write (response code. htmlencode (item ["title"]. tostring () + "<br> ");
}
Private spweb mysite;
Private void page_load (Object sender, system. eventargs E)
{
Mysite = spcontrol. getcontextweb (context );
}
Private void button#click (Object sender, system. eventargs E)
{
Int maxsize = convert. toint32 (textbox1.text );
Spfolder myfolder = mysite. getfolder ("Shared Folders ");
Spfilecollection myfiles = myfolder. files;
Foreach (spfile file in myfiles)
{
If (file. length> (maxsize * 1024 ))
{
Response. Write (response code. htmlencode (file. Name) + ":"
+ File. Length/1024 + "kb <br> ");
File. copyto ("Archive/" + file. Name, true );
}
}
}
Public void uploadfile (string srcurl, string desturl)
{
If (! File. exists (srcurl ))
{
Throw new argumentexception (string. Format ("{0} does not exist ",
Srcurl), "srcurl ");
}
Spweb site = new spsite (desturl). openweb ();
Filestream fstream = file. openread (srcurl );
Byte [] Contents = new byte [fstream. Length];
Fstream. Read (contents, 0, (INT) fstream. Length );
Fstream. Close ();
Ensureparentfolder (site, desturl );
Site. Files. Add (desturl, contents );
}
You can test it on the machine.
From: http://www.cnblogs.com/wanghao-3/archive/2007/07/03/803950.html