MOSS 对象模型的简单开发DEMO

这几天很忙,但是每天打开QQ,还是MSN,EML关于MOSS开发的问题确实不少,但我看拉下,确实很简单的问题,都是关于一些对象模型的运用,可能大家很少看SDK,里面介绍的很明白噢。
下面介绍一下MOSS对象模型的简单介绍,希望那些才接触MOSS的同仁们,快点入门,希望大家共同进步。
protected void GetSitesAndGroups()
{
    string userList = SPEncode.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 += SPEncode.HtmlEncode(group.Name) + "  ";
                }
                userList += subWeb.ServerRelativeUrl.ToString() +
                    " -- " + listGroups + "<BR>";
            }
        }
    }
    Label1.Text = userList;
}

//简单利用SPWEB遍历所有的LIST
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(SPEncode.HtmlEncode(mySite.Title));

SPWebCollection subSites = mySite.Webs;

foreach(SPWeb site in subSites)
{

   output.Write(SPEncode.HtmlEncode(site.Title) + "<BR>");

   SPListCollection lists=site.Lists;

   foreach(SPList list in lists)
   {

      if (list.ItemCount>10)
      {
          output.Write(SPEncode.HtmlEncode(list.Title) + " : " +
             list.ItemCount + "<BR>");
      }
   }
}

//增加一个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);

//增加一个WEB,定制指定的模板 

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

//删除指定LIST
SPWeb mySite = SPContext.Current.Web;
SPListCollection lists = mySite.Lists;

SPList list = lists[TextBox1.Text];
System.Guid listGuid = list.ID;

lists.Delete(listGuid);

//查询
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(SPEncode.HtmlEncode(item["Title"].ToString()) + "<BR>");
}


private SPWeb mySite;

private void Page_Load(object sender, System.EventArgs e)
{
   mySite = SPControl.GetContextWeb(Context);
}

private void Button1_Click(object sender, System.EventArgs e)
{
   int maxSize = Convert.ToInt32(TextBox1.Text);

   SPFolder myFolder = mySite.GetFolder("Shared Documents");
   SPFileCollection myFiles = myFolder.Files;

   foreach (SPFile file in myFiles)
   {

      if (file.Length>(maxSize*1024))
      {
         Response.Write(SPEncode.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();

你可能感兴趣的:(OS)