Saturday, April 27, 2013

SharePoint Add/Update/Delete List Item Programmatically to SharePoint List using C#

Step to Add new list item programmatically to SharePoint List using C#

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




Get SharePoint List Item by ID

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());
}
}

Search List item in SharePoint List using SPQuery

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());
}

}
 }

}

Update List Item Programmatically in SharePoint 2010 using C#

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


Attach File/Document to List Item programmatically in SharePoint 2010 using C#

SharePointBlog

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


That's It





3 comments:

  1. SharePoint Online delivers the powerful features of SharePoint without the associated overhead of managing he infrastructure on your own. Flexible management options ensure that you still retain the
    control you need to meet the compliance requirements of your organization. You can purchase SharePoint in the cloud as a standalone offering or as part of an Office 365 suite where you could also get access to Exchange, Lync, the Office clients and web apps.

    Sharepoint developer
    Sharepoint Site
    Sharepoint Services
    Sharepoint Designer
    Sharepoint Consulting
    Sharepoint Server

    ReplyDelete
  2. Very good , very short and sweet.

    Thanks.

    ReplyDelete
  3. What are the various classes in Server Object Model from development point of view? Please ans me this question and thanks in advance.

    My Signature: The Best Funeral Homes Website Design by Optimized360.Com

    ReplyDelete

SharePoint Deployment Error : The specified path, file name, or both are too long.

Hi, Sometimes we get surprised with below error while deploying wsp file... Solution : To overcome with this issue, we just ne...