C# 对sharepoint 列表的一些基本操作,包括添加/删除/查询/上传文件给sharepoint list添加数据

这篇博客介绍了如何使用C#进行SharePoint列表的基本操作,包括向列表添加和删除数据,上传文件以及查询并更新列表记录。示例代码展示了如何与SharePoint交互,如设置字段值、读取输入流、更新列表项等。

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

// 查询
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
{
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值