【转】MOSS 对List的操作

原文链接:http://www.cnblogs.com/netdazhi/articles/1229637.html

 

using Microsoft.SharePoint;

SPWeb site = SPControl.GetContextWeb(Context);
SPListItemCollection items = site.Lists["ListName"].Items;

SPListItem item = items.Add();

item["Field_1"] = OneValue;

item["Field_2"] = TwoValue;

item.Update();


删除sharepoint list数据
=============================================
using Microsoft.SharePoint;

SPWeb site = SPControl.GetContextWeb(Context);

SPListItemCollection items = site.Lists["ListName"].Items;

items[0].Delete();


上传文件到sharepoint
=============================================
using System.IO;

using Microsoft.SharePoint;

if( htmlInputFile1.PostedFile != null )
{
          SPWeb site = new SPSite(destinationURL).OpenWeb();
          Stream stream = htmlInputFile1.PostedFile.InputStream;

          byte[] buffer = new bytes[stream.Length];

          stream.Read(buffer, 0, (int) stream.Length);

          stream.Close();

          site.Files.Add(destinationURL, buffer);
}

查询记录及更新数据
===============================================
using Microsoft.SharePoint;

SPWeb web = new SPSite("http://nick").OpenWeb("test"); //Open website

web.AllowUnsafeUpdates = true;

SPList list = web.Lists["ListName"];

SPQuery query = new SPQuery();

query.Query = "<Where>"+
          "<And><And>"+
          "<Eq><FieldRef Name=\"Filed_1\"/><Value Type=\"Text\">Test</Value></Eq>" +
          "<Eq><FieldRef Name=\"Filed_2\"/><Value Type=\"Text\">" + (string)OneValue + "</Value></Eq>" +
          "</And>"+
          "<Eq><FieldRef Name=\"Filed_3\"/><Value Type=\"Text\">" + (string)TwoValue + "</Value></Eq>" +
          "</And>"+
          "</Where>";

query.RowLimit = 10;

 

对日期格式进行查询:

query.Query = "<Where><Eq><FieldRef Name='LastChange' /><Value Type='DateTime'>1971-01-01T00:00:00Z</Value></Eq></Where>";

//查询

     查询是对internalname进行匹配,所以要先找到对应的子段的internalname.

                SPField costcentrefield = list.Fields.GetField("CostCentre");
                CostCentreInternalName = costcentrefield.InternalName.ToString();
                SPField titlefield = list.Fields.GetField("JobTitle");
                TitleInternalName = titlefield.InternalName.ToString();

     注意语法两个查询条件的是:<and><eq></eq><eq></eq></and>

   一个查询条件的是:<eq></eq>,是没有and的。


SPListItemCollection items = list.GetItems(query);
try
{
if (Items.Count != 0)
{
     //更新sharepoint list 数据
     foreach (SPListItem list in listItems)
     {
         list["Filed_1"] = TextBox1.text.ToString();
         list["Filed_2"] = TextBox2.text.ToString();
         list["Filed_3"] = TextBox3.text.ToString();

         listItem.Update();
     }                      
}
else
{   //将数据记录添加进sharepoint
      SPListItem addlist = List.Items.Add();

      addlist["Filed_1"] = TextBox1.Text.ToString();
      addlist["Filed_2"] = TextBox2.Text.ToString();
      addlist["Filed_3"] = TextBox3.Text.ToString();

      addlist.Update();
}
}
catch
{
...
}

 

posted on 2008-09-27 22:12  王丹小筑  阅读(294)  评论(0)    收藏  举报

导航