SPListItem Add #sharepoint
//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;
}
}
}