csharp SPListItem添加

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp SPListItem添加相关的知识,希望对你有一定的参考价值。

//Below code update the list item Title with Title+ID whose Title is equal to mohit

using (SPSite _site = newSPSite(SPContext.Current.Site.Url))
{
 using (SPWeb _web = _site.OpenWeb())
{
 SPListoList = _web.Lists["myTestList"];
 SPQuery _query = newSPQuery();

 _query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>Mohit</Value></Eq></Where>";
SPListItemCollection _itemCollection = oList.GetItems(_query);

if (_itemCollection.Count> 0)
{
 _web.AllowUnsafeUpdates = true;

       foreach (SPListItem Item in _itemCollection)
       {
         Item["Title"] = Item["Title"].ToString() + Item.ID.ToString();
         Item.Update();
       }

       _web.AllowUnsafeUpdates = false;
     }
 }
}
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
        using (SPWeb web = site.OpenWeb())
        {
                SPList list = web.Lists["MyListName"];
                web.AllowUnsafeUpdates = true;
                SPListItem item = list.Items.Add();
                item["Title"] = "Hello";        
                item.Update();
               web.AllowUnsafeUpdates = false;
        }
}
//Here is the code to Search Item in SharePoint whose Title is equal to mohit

using (SPSite _site = newSPSite(SPContext.Current.Site.Url))
{
   using (SPWeb _web = _site.OpenWeb())
  {
    SPListoList = _web.Lists["myTestList"];
    SPQuery _query = newSPQuery();
    _query.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>Mohit</Value></Eq>  </Where>";
  SPListItemCollection _itemCollection = oList.GetItems(_query);

if (_itemCollection.Count> 0)
{
foreach (SPListItem Item in _itemCollection)
{
  Response.Write("Item ID: " + Item.ID.ToString() + ", Item Title: " +       Item["Title"].ToString());
}

}
 }

}
using (SPSite _site = newSPSite(SPContext.Current.Site.Url))
{
using (SPWeb _web = _site.OpenWeb())
{
SPListolist = _web.Lists["myTestList"];
SPListItem Item = olist.GetItemById(ItemID);
Response.Write("Item Title: " + Item["Title"].ToString());
}
}
using (SPSite _site = newSPSite(SPContext.Current.Site.Url))
   {

    using (SPWeb _web = _site.OpenWeb())
    {
     //Let's suppose your Item Id is 1
     intItemId = 1;

     SPListoList = _web.Lists["myTestList"];
     SPListItem _item = oList.GetItemById(ItemId);

     if (FileUpload1.HasFile)
        {
           _web.AllowUnsafeUpdates = true;
          Stream fs = FileUpload1.PostedFile.InputStream;
          byte[] _bytes = new byte[fs.Length];
          fs.Position= 0;
          fs.Read(_bytes, 0, (int)fs.Length);
          fs.Close();
          fs.Dispose();

          _item.Attachments.Add(FileUpload1.PostedFile.FileName, _bytes);
          _item.Update();

          _web.AllowUnsafeUpdates = false;
        }
    }
}

以上是关于csharp SPListItem添加的主要内容,如果未能解决你的问题,请参考以下文章

csharp 新增或者更新SPListItem中的多选字段

csharp 使用列名称中的CAML获取SharePoint项目(SPListItem)。

SharePoint 2013 - 通过 REST 获取 SPListItem 版本

SPListItem.UpdateOverwriteVersion()真的不会创建新版本吗?

SPList的下一个SPListItem ID

LINQ 怎么消除重复数据 要求返回类型是IEnumerable<MySPListitem>类型