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

  1. ============================================
  2. using Microsoft.SharePoint;
  3. SPWeb site = SPControl.GetContextWeb(Context); 
  4. SPListItemCollection items = site.Lists["ListName"].Items;
  5. SPListItem item = items.Add();
  6. item["Field_1"] = OneValue;
  7. item["Field_2"] = TwoValue;
  8. item.Update();
  9. 删除sharepoint list数据
  10. =============================================
  11. using Microsoft.SharePoint;
  12. SPWeb site = SPControl.GetContextWeb(Context);
  13. SPListItemCollection items = site.Lists["ListName"].Items;
  14. items[0].Delete();
  15. 上传文件到sharepoint
  16. =============================================
  17. using System.IO;
  18. using Microsoft.SharePoint;
  19. if( htmlInputFile1.PostedFile != null )
  20. {
  21.           SPWeb site = new SPSite(destinationURL).OpenWeb(); 
  22.           Stream stream = htmlInputFile1.PostedFile.InputStream;
  23.           byte[] buffer = new bytes[stream.Length];
  24.           stream.Read(buffer, 0, (int) stream.Length);
  25.           stream.Close();
  26.           site.Files.Add(destinationURL, buffer);
  27. }
  28. 查询记录及更新数据
  29. ===============================================
  30. using Microsoft.SharePoint;
  31. SPWeb web = new SPSite("http://nick").OpenWeb("test"); //Open website
  32. web.AllowUnsafeUpdates = true;
  33. SPList list = web.Lists["ListName"];
  34. SPQuery query = new SPQuery();
  35. query.Query = "<Where>"+
  36.           "<And><And>"+
  37.           "<Eq><FieldRef Name=/"Filed_1/"/><Value Type=/"Text/">Test</Value></Eq>" +
  38.           "<Eq><FieldRef Name=/"Filed_2/"/><Value Type=/"Text/">" + (string)OneValue + "</Value></Eq>" +
  39.           "</And>"+
  40.           "<Eq><FieldRef Name=/"Filed_3/"/><Value Type=/"Text/">" + (string)TwoValue + "</Value></Eq>" +
  41.           "</And>"+
  42.           "</Where>";
  43. query.RowLimit = 10;
  44. //查询
  45. SPListItemCollection items = list.GetItems(query);
  46. try
  47. {
  48. if (Items.Count != 0)
  49. {
  50.      //更新sharepoint list 数据
  51.      foreach (SPListItem list in listItems)
  52.      {
  53.          list["Filed_1"] = TextBox1.text.ToString();
  54.          list["Filed_2"] = TextBox2.text.ToString();
  55.          list["Filed_3"] = TextBox3.text.ToString();
  56.          listItem.Update();
  57.      }                       
  58. }
  59. else
  60. {   //将数据记录添加进sharepoint
  61.       SPListItem addlist = List.Items.Add();
  62.       addlist["Filed_1"] = TextBox1.Text.ToString();
  63.       addlist["Filed_2"] = TextBox2.Text.ToString();
  64.       addlist["Filed_3"] = TextBox3.Text.ToString();
  65.       addlist.Update();
  66. }
  67. }
  68. catch
  69. {
  70. ...
  71. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值