Saturday, 31 August 2013

Upload multiple file with Jquery into ListItem in SharePoint Server 2010

Upload multiple file with Jquery into ListItem in SharePoint Server 2010

                    <tr>
                        <td width="25%">
                            Upload Document:
                        </td>
                        <td align="left" colspan="2" width="50%">
                            <input id="ipAttachments" type="file" class="multi" runat="server" />
                        </td>
                        <td width="25%">
                            &nbsp;
                        </td>
                    </tr>

SPListItem rnListItem = SPContext.Current.Web.Lists["EmployeeList"].GetItemById(Convert.ToInt32(1));
AttachFiles(rnListItem);
rnListItem.SystemUpdate();


        protected void AttachFiles(SPListItem listItem)
        {
            Stream fStream;
            byte[] contents;
            string fileName = string.Empty;
            HttpFileCollection fileCol = Request.Files;
            if (fileCol != null)
            {
                if (fileCol[0].FileName != "")
                {
                    foreach (string flName in fileCol)
                    {
                        HttpPostedFile file = fileCol[flName];
                        fileName = Path.GetFileName(file.FileName);
                        fStream = file.InputStream;
                        fStream.Position = 0;

                        contents = new byte[fStream.Length];

                        fStream.Read(contents, 0, (int)fStream.Length);
                        fStream.Close();
                        listItem.Attachments.Add(fileName, contents);
                    }
                }
            }
        }

    <script src="/_layouts/FarmTeachnetSolution/jquery.MultiFile.js" type="text/javascript"></script>


FileName Should be = jquery.MultiFile.js
/*
 ### jQuery Multiple File Upload Plugin v1.48 - 2012-07-19 ###
 * Home: http://www.fyneworks.com/jquery/multiple-file-upload/
 * Code: http://code.google.com/p/jquery-multifile-plugin/
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 ###
*/

/*# AVOID COLLISIONS #*/
;if(window.jQuery) (function($){
/*# AVOID COLLISIONS #*/

// plugin initialization
$.fn.MultiFile = function(options){
if(this.length==0) return this; // quick fail

// Handle API methods
if(typeof arguments[0]=='string'){
// Perform API methods on individual elements
if(this.length>1){
var args = arguments;
return this.each(function(){
$.fn.MultiFile.apply($(this), args);
    });
};
// Invoke API method handler
$.fn.MultiFile[arguments[0]].apply(this, $.makeArray(arguments).slice(1) || []);
// Quick exit...
return this;
};

// Initialize options for this call
var options = $.extend(
{}/* new object */,
$.fn.MultiFile.options/* default options */,
options || {} /* just-in-time options */
);

// Empty Element Fix!!!
// this code will automatically intercept native form submissions
// and disable empty file elements
$('form')
.not('MultiFile-intercepted')
.addClass('MultiFile-intercepted')
.submit($.fn.MultiFile.disableEmpty);

//### http://plugins.jquery.com/node/1363
// utility method to integrate this plugin with others...
if($.fn.MultiFile.options.autoIntercept){
$.fn.MultiFile.intercept( $.fn.MultiFile.options.autoIntercept /* array of methods to intercept */ );
$.fn.MultiFile.options.autoIntercept = null; /* only run this once */
};

// loop through each matched element
this
.not('.MultiFile-applied')
.addClass('MultiFile-applied')
.each(function(){
//#####################################################################
// MAIN PLUGIN FUNCTIONALITY - START
//#####################################################################

       // BUG 1251 FIX: http://plugins.jquery.com/project/comments/add/1251
       // variable group_count would repeat itself on multiple calls to the plugin.
       // this would cause a conflict with multiple elements
       // changes scope of variable to global so id will be unique over n calls
       window.MultiFile = (window.MultiFile || 0) + 1;
       var group_count = window.MultiFile;
     
       // Copy parent attributes - Thanks to Jonas Wagner
       // we will use this one to create new input elements
       var MultiFile = {e:this, E:$(this), clone:$(this).clone()};
     
       //===
     
       //# USE CONFIGURATION
       if(typeof options=='number') options = {max:options};
       var o = $.extend({},
        $.fn.MultiFile.options,
        options || {},
    ($.metadata? MultiFile.E.metadata(): ($.meta?MultiFile.E.data():null)) || {}, /* metadata options */
{} /* internals */
       );
       // limit number of files that can be selected?
       if(!(o.max>0) /*IsNull(MultiFile.max)*/){
        o.max = MultiFile.E.attr('maxlength');
       };
if(!(o.max>0) /*IsNull(MultiFile.max)*/){
o.max = (String(MultiFile.e.className.match(/\b(max|limit)\-([0-9]+)\b/gi) || ['']).match(/[0-9]+/gi) || [''])[0];
if(!(o.max>0)) o.max = -1;
else           o.max = String(o.max).match(/[0-9]+/gi)[0];
}
       o.max = new Number(o.max);
       // limit extensions?
       o.accept = o.accept || MultiFile.E.attr('accept') || '';
       if(!o.accept){
        o.accept = (MultiFile.e.className.match(/\b(accept\-[\w\|]+)\b/gi)) || '';
        o.accept = new String(o.accept).replace(/^(accept|ext)\-/i,'');
       };
     
       //===
     
       // APPLY CONFIGURATION
$.extend(MultiFile, o || {});
       MultiFile.STRING = $.extend({},$.fn.MultiFile.options.STRING,MultiFile.STRING);
     
       //===
     
       //#########################################
       // PRIVATE PROPERTIES/METHODS
       $.extend(MultiFile, {
        n: 0, // How many elements are currently selected?
        slaves: [], files: [],
        instanceKey: MultiFile.e.id || 'MultiFile'+String(group_count), // Instance Key?
        generateID: function(z){ return MultiFile.instanceKey + (z>0 ?'_F'+String(z):''); },
        trigger: function(event, element){
         var handler = MultiFile[event], value = $(element).attr('value');
         if(handler){
          var returnValue = handler(element, value, MultiFile);
          if( returnValue!=null ) return returnValue;
         }
         return true;
        }
       });
     
       //===
     
       // Setup dynamic regular expression for extension validation
       // - thanks to John-Paul Bader: http://smyck.de/2006/08/11/javascript-dynamic-regular-expresions/
       if(String(MultiFile.accept).length>1){
MultiFile.accept = MultiFile.accept.replace(/\W+/g,'|').replace(/^\W|\W$/g,'');
        MultiFile.rxAccept = new RegExp('\\.('+(MultiFile.accept?MultiFile.accept:'')+')$','gi');
       };
     
       //===
     
       // Create wrapper to hold our file list
       MultiFile.wrapID = MultiFile.instanceKey+'_wrap'; // Wrapper ID?
       MultiFile.E.wrap('<div class="MultiFile-wrap" id="'+MultiFile.wrapID+'"></div>');
       MultiFile.wrapper = $('#'+MultiFile.wrapID+'');
     
       //===
     
       // MultiFile MUST have a name - default: file1[], file2[], file3[]
       MultiFile.e.name = MultiFile.e.name || 'file'+ group_count +'[]';
     
       //===
     
if(!MultiFile.list){
// Create a wrapper for the list
// * OPERA BUG: NO_MODIFICATION_ALLOWED_ERR ('list' is a read-only property)
// this change allows us to keep the files in the order they were selected
MultiFile.wrapper.append( '<div class="MultiFile-list" id="'+MultiFile.wrapID+'_list"></div>' );
MultiFile.list = $('#'+MultiFile.wrapID+'_list');
};
       MultiFile.list = $(MultiFile.list);

       //===
     
       // Bind a new element
       MultiFile.addSlave = function( slave, slave_count ){
//if(window.console) console.log('MultiFile.addSlave',slave_count);

        // Keep track of how many elements have been displayed
        MultiFile.n++;
        // Add reference to master element
        slave.MultiFile = MultiFile;

// BUG FIX: http://plugins.jquery.com/node/1495
// Clear identifying properties from clones
if(slave_count>0) slave.id = slave.name = '';

        // Define element's ID and name (upload components need this!)
        //slave.id = slave.id || MultiFile.generateID(slave_count);
if(slave_count>0) slave.id = MultiFile.generateID(slave_count);
//FIX for: http://code.google.com/p/jquery-multifile-plugin/issues/detail?id=23
     
        // 2008-Apr-29: New customizable naming convention (see url below)
        // http://groups.google.com/group/jquery-dev/browse_frm/thread/765c73e41b34f924#
        slave.name = String(MultiFile.namePattern
         /*master name*/.replace(/\$name/gi,$(MultiFile.clone).attr('name'))
         /*master id  */.replace(/\$id/gi,  $(MultiFile.clone).attr('id'))
         /*group count*/.replace(/\$g/gi,   group_count)//(group_count>0?group_count:''))
         /*slave count*/.replace(/\$i/gi,   slave_count)//(slave_count>0?slave_count:''))
        );
     
        // If we've reached maximum number, disable input slave
        if( (MultiFile.max > 0) && ((MultiFile.n-1) > (MultiFile.max)) )//{ // MultiFile.n Starts at 1, so subtract 1 to find true count
         slave.disabled = true;
        //};
     
        // Remember most recent slave
        MultiFile.current = MultiFile.slaves[slave_count] = slave;
     
// We'll use jQuery from now on
slave = $(slave);
     
        // Clear value
        slave.val('').attr('value','')[0].value = '';
     
// Stop plugin initializing on slaves
slave.addClass('MultiFile-applied');

        // Triggered when a file is selected
        slave.change(function(){
          //if(window.console) console.log('MultiFile.slave.change',slave_count);
 
          // Lose focus to stop IE7 firing onchange again
          $(this).blur();
       
          //# Trigger Event! onFileSelect
          if(!MultiFile.trigger('onFileSelect', this, MultiFile)) return false;
          //# End Event!
       
          //# Retrive value of selected file from element
          var ERROR = '', v = String(this.value || ''/*.attr('value)*/);
       
          // check extension
          if(MultiFile.accept && v && !v.match(MultiFile.rxAccept))//{
            ERROR = MultiFile.STRING.denied.replace('$ext', String(v.match(/\.\w{1,4}$/gi)));
           //}
          //};
       
          // Disallow duplicates
for(var f in MultiFile.slaves)//{
           if(MultiFile.slaves[f] && MultiFile.slaves[f]!=this)//{
  //console.log(MultiFile.slaves[f],MultiFile.slaves[f].value);
            if(MultiFile.slaves[f].value==v)//{
             ERROR = MultiFile.STRING.duplicate.replace('$file', v.match(/[^\/\\]+$/gi));
            //};
           //};
          //};
       
          // Create a new file input element
          var newEle = $(MultiFile.clone).clone();// Copy parent attributes - Thanks to Jonas Wagner
          //# Let's remember which input we've generated so
          // we can disable the empty ones before submission
          // See: http://plugins.jquery.com/node/1495
          newEle.addClass('MultiFile');
       
          // Handle error
          if(ERROR!=''){
            // Handle error
            MultiFile.error(ERROR);

            // 2007-06-24: BUG FIX - Thanks to Adrian Wróbel <adrian [dot] wrobel [at] gmail.com>
            // Ditch the trouble maker and add a fresh new element
            MultiFile.n--;
            MultiFile.addSlave(newEle[0], slave_count);
            slave.parent().prepend(newEle);
            slave.remove();
            return false;
          };
       
          // Hide this element (NB: display:none is evil!)
          $(this).css({ position:'absolute', top: '-3000px' });
       
          // Add new element to the form
          slave.after(newEle);
       
          // Update list
          MultiFile.addToList( this, slave_count );
       
          // Bind functionality
          MultiFile.addSlave( newEle[0], slave_count+1 );
       
          //# Trigger Event! afterFileSelect
          if(!MultiFile.trigger('afterFileSelect', this, MultiFile)) return false;
          //# End Event!
       
        }); // slave.change()

// Save control to element
$(slave).data('MultiFile', MultiFile);

       };// MultiFile.addSlave
       // Bind a new element
     
     
     
       // Add a new file to the list
       MultiFile.addToList = function( slave, slave_count ){
        //if(window.console) console.log('MultiFile.addToList',slave_count);

        //# Trigger Event! onFileAppend
        if(!MultiFile.trigger('onFileAppend', slave, MultiFile)) return false;
        //# End Event!
     
        // Create label elements
        var
         r = $('<div class="MultiFile-label"></div>'),
         v = String(slave.value || ''/*.attr('value)*/),
         a = $('<span class="MultiFile-title" title="'+MultiFile.STRING.selected.replace('$file', v)+'">'+MultiFile.STRING.file.replace('$file', v.match(/[^\/\\]+$/gi)[0])+'</span>'),
         b = $('<a class="MultiFile-remove" href="#'+MultiFile.wrapID+'">'+MultiFile.STRING.remove+'</a>');
     
        // Insert label
        MultiFile.list.append(
         r.append(b, ' ', a)
        );
     
        b
.click(function(){
       
          //# Trigger Event! onFileRemove
          if(!MultiFile.trigger('onFileRemove', slave, MultiFile)) return false;
          //# End Event!
       
          MultiFile.n--;
          MultiFile.current.disabled = false;
       
          // Remove element, remove label, point to current
MultiFile.slaves[slave_count] = null;
$(slave).remove();
$(this).parent().remove();

          // Show most current element again (move into view) and clear selection
          $(MultiFile.current).css({ position:'', top: '' });
$(MultiFile.current).reset().val('').attr('value', '')[0].value = '';
       
          //# Trigger Event! afterFileRemove
          if(!MultiFile.trigger('afterFileRemove', slave, MultiFile)) return false;
          //# End Event!

          return false;
        });
     
        //# Trigger Event! afterFileAppend
        if(!MultiFile.trigger('afterFileAppend', slave, MultiFile)) return false;
        //# End Event!
     
       }; // MultiFile.addToList
       // Add element to selected files list
     
     
     
       // Bind functionality to the first element
       if(!MultiFile.MultiFile) MultiFile.addSlave(MultiFile.e, 0);
     
       // Increment control count
       //MultiFile.I++; // using window.MultiFile
       MultiFile.n++;

// Save control to element
MultiFile.E.data('MultiFile', MultiFile);


//#####################################################################
// MAIN PLUGIN FUNCTIONALITY - END
//#####################################################################
}); // each element
};

/*--------------------------------------------------------*/

/*
### Core functionality and API ###
*/
$.extend($.fn.MultiFile, {
  /**
   * This method removes all selected files
   *
   * Returns a jQuery collection of all affected elements.
   *
   * @name reset
   * @type jQuery
   * @cat Plugins/MultiFile
   * @author Diego A. (http://www.fyneworks.com/)
   *
   * @example $.fn.MultiFile.reset();
   */
  reset: function(){
var settings = $(this).data('MultiFile');
//if(settings) settings.wrapper.find('a.MultiFile-remove').click();
if(settings) settings.list.find('a.MultiFile-remove').click();
   return $(this);
  },


  /**
   * This utility makes it easy to disable all 'empty' file elements in the document before submitting a form.
   * It marks the affected elements so they can be easily re-enabled after the form submission or validation.
   *
   * Returns a jQuery collection of all affected elements.
   *
   * @name disableEmpty
   * @type jQuery
   * @cat Plugins/MultiFile
   * @author Diego A. (http://www.fyneworks.com/)
   *
   * @example $.fn.MultiFile.disableEmpty();
   * @param String class (optional) A string specifying a class to be applied to all affected elements - Default: 'mfD'.
   */
  disableEmpty: function(klass){ klass = (typeof(klass)=='string'?klass:'')||'mfD';
   var o = [];
   $('input:file.MultiFile').each(function(){ if($(this).val()=='') o[o.length] = this; });
   return $(o).each(function(){ this.disabled = true }).addClass(klass);
  },


/**
* This method re-enables 'empty' file elements that were disabled (and marked) with the $.fn.MultiFile.disableEmpty method.
*
* Returns a jQuery collection of all affected elements.
*
* @name reEnableEmpty
* @type jQuery
* @cat Plugins/MultiFile
* @author Diego A. (http://www.fyneworks.com/)
*
* @example $.fn.MultiFile.reEnableEmpty();
* @param String klass (optional) A string specifying the class that was used to mark affected elements - Default: 'mfD'.
*/
  reEnableEmpty: function(klass){ klass = (typeof(klass)=='string'?klass:'')||'mfD';
   return $('input:file.'+klass).removeClass(klass).each(function(){ this.disabled = false });
  },


/**
* This method will intercept other jQuery plugins and disable empty file input elements prior to form submission
*

* @name intercept
* @cat Plugins/MultiFile
* @author Diego A. (http://www.fyneworks.com/)
*
* @example $.fn.MultiFile.intercept();
* @param Array methods (optional) Array of method names to be intercepted
*/
  intercepted: {},
  intercept: function(methods, context, args){
   var method, value; args = args || [];
   if(args.constructor.toString().indexOf("Array")<0) args = [ args ];
   if(typeof(methods)=='function'){
    $.fn.MultiFile.disableEmpty();
    value = methods.apply(context || window, args);
//SEE-http://code.google.com/p/jquery-multifile-plugin/issues/detail?id=27
setTimeout(function(){ $.fn.MultiFile.reEnableEmpty() },1000);
    return value;
   };
   if(methods.constructor.toString().indexOf("Array")<0) methods = [methods];
   for(var i=0;i<methods.length;i++){
    method = methods[i]+''; // make sure that we have a STRING
    if(method) (function(method){ // make sure that method is ISOLATED for the interception
     $.fn.MultiFile.intercepted[method] = $.fn[method] || function(){};
     $.fn[method] = function(){
      $.fn.MultiFile.disableEmpty();
      value = $.fn.MultiFile.intercepted[method].apply(this, arguments);
//SEE http://code.google.com/p/jquery-multifile-plugin/issues/detail?id=27
      setTimeout(function(){ $.fn.MultiFile.reEnableEmpty() },1000);
      return value;
     }; // interception
    })(method); // MAKE SURE THAT method IS ISOLATED for the interception
   };// for each method
  } // $.fn.MultiFile.intercept

 });

/*--------------------------------------------------------*/

/*
### Default Settings ###
eg.: You can override default control like this:
$.fn.MultiFile.options.accept = 'gif|jpg';
*/
$.fn.MultiFile.options = { //$.extend($.fn.MultiFile, { options: {
accept: '', // accepted file extensions
max: -1,    // maximum number of selectable files

// name to use for newly created elements
namePattern: '$name', // same name by default (which creates an array)
         /*master name*/ // use $name
         /*master id  */ // use $id
         /*group count*/ // use $g
         /*slave count*/ // use $i
/*other      */ // use any combination of he above, eg.: $name_file$i

// STRING: collection lets you show messages in different languages
STRING: {
remove:'x',
denied:'You cannot select a $ext file.\nTry again...',
file:'$file',
selected:'File selected: $file',
duplicate:'This file has already been selected:\n$file'
},

// name of methods that should be automcatically intercepted so the plugin can disable
// extra file elements that are empty before execution and automatically re-enable them afterwards
  autoIntercept: [ 'submit', 'ajaxSubmit', 'ajaxForm', 'validate', 'valid' /* array of methods to intercept */ ],

// error handling function
error: function(s){
/*
ERROR! blockUI is not currently working in IE
if($.blockUI){
$.blockUI({
message: s.replace(/\n/gi,'<br/>'),
css: {
border:'none', padding:'15px', size:'12.0pt',
backgroundColor:'#900', color:'#fff',
opacity:'.8','-webkit-border-radius': '10px','-moz-border-radius': '10px'
}
});
window.setTimeout($.unblockUI, 2000);
}
else//{// save a byte!
*/
alert(s);
//}// save a byte!
}
 }; //} });

/*--------------------------------------------------------*/

/*
### Additional Methods ###
Required functionality outside the plugin's scope
*/

// Native input reset method - because this alone doesn't always work: $(element).val('').attr('value', '')[0].value = '';
$.fn.reset = function(){ return this.each(function(){ try{ this.reset(); }catch(e){} }); };

/*--------------------------------------------------------*/

/*
### Default implementation ###
The plugin will attach itself to file inputs
with the class 'multi' when the page loads
*/
$(function(){
  //$("input:file.multi").MultiFile();
  $("input[type=file].multi").MultiFile();
 });



/*# AVOID COLLISIONS #*/
})(jQuery);
/*# AVOID COLLISIONS #*/








Learn EventReciever in SharePoint 2010

Programmatically create a Attachment functionality in Custom Visual WebPart Sharepoint 2010

i modified it to suit my requirement

paste this code in the .aspx  page
<table> <tr><td>
<asp:HiddenField Value="hDeleteAttachs" ID="hHiddenFields"  runat="server" />
</td></tr>

<tr>
<td><span id="part1">
<SharePoint:AttachmentButton runat="server" ID="btnAttach" ControlMode="new" Text="Add Attachment">
</SharePoint:AttachmentButton> </span></td></tr>

<tr><td id='idAttachmentsRow'>
<SharePoint:AttachmentUpload runat="server" ID="uploadAttach" ControlMode="New">
</SharePoint:AttachmentUpload></td></tr>

<tr><td><SharePoint:AttachmentsField runat="server" ID="fieldAttach" ControlMode="new" FieldName="Attachments">
</SharePoint:AttachmentsField></td></tr>
</table>

 <asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="submitButton_Click" />

Below in the c# page

namespace ProjectName.VisualWebPartName
{
    public partial class VisualWebPartNameUserControl: UserControl
    {

        SPSite Osite = null;
        SPWeb Oweb = null;
        SPSite noprevsite = null;
        SPWeb noprevweb = null;
        SPList Olist = null;
        SPListItem Osplistitem = null;

 protected void Page_Load(object sender, EventArgs e)
        {
             noprevsite = SPContext.Current.Site;
             noprevweb = noprevsite.OpenWeb();

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                Osite = new SPSite(noprevsite.ID);
                Oweb = Osite.OpenWeb(noprevweb.ID);
                Oweb.AllowUnsafeUpdates = true;
                Olist = Oweb.Lists["ListName"];
                btnAttach.ListId = Olist.ID;
                uploadAttach.ListId = Olist.ID;
                fieldAttach.ListId = Olist.ID;
                Osplistitem = Olist.Items.Add();
});
}

 protected void submitButton_Click(object sender, EventArgs e)
        {
          AddAttachments(ref Osplistitem);
        }
   void AddAttachments(ref SPListItem Osplistitem)
        {
            try
            {
                for (int i = 0; i < (Page.Request.Files.Count - 1); i++)
                {
                    try
                    {
                        //Get the list of files for attachments
                        HttpPostedFile newAttach = Page.Request.Files[i];
                        byte[] fileContents = new byte[newAttach.ContentLength - 1];
                        newAttach.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
                        newAttach.InputStream.Read(fileContents, 0, newAttach.ContentLength - 1);
                        System.IO.FileInfo fInfo = new System.IO.FileInfo(newAttach.FileName);
                        Osplistitem.Attachments.Add(fInfo.Name, fileContents);
                    }
                    catch { continue; }
                }
            }
            catch
            {
            }

        }
}
}

List Operation

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

        public  int AddNewItemWithAttachment()
        {
             // Get name of the file 
            string _Str_File_Name = fuAttachment.PostedFile.FileName;
           //  Get stream data by following way
            byte[] contents = GetFileStream();

           // Declare variable which hold the newly created item id.
            int _Int_ID = 0;

           //  Follow the steps to insert new item into your sharepoint list with attachment. 
           //  Create new instance of site with your current application
            using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
            { 
            //  Create new web instance and open web for current application
                using (SPWeb web = site.OpenWeb())
                { 
                   //  Create new list object and get your list
                    SPList list = web.Lists["EmployeeList"]; 
                   //  Set allowunsafeupdates for web & site to true
                    site.AllowUnsafeUpdates = true;
                    web.AllowUnsafeUpdates = true; 
                   //  Create new list item object and add new item for newly created list object 
                    SPListItem item = list.Items.Add();
                    // Assign value for newly created item
                    item["Title"] = txtFirstName.Text.Trim();
                    item["FirstName"]  = txtFirstName.Text.Trim();
                    item["LastName"]  = txtLastName.Text.Trim();
                    item["Address"]  = txtAddress.Text.Trim();
                    item["Phone"]  = txtPhone.Text.Trim();
                    item["City"]  = txtCity.Text.Trim();
                    item["State"]  = txtState.Text.Trim();
                    item["Country"] = txtCountry.Text.Trim();     
                    //- Assign selected filename and stream data
                    if (!string.IsNullOrEmpty(_Str_File_Name) && (contents.Length > 0))
                    {
                    SPAttachmentCollection fileAttach = item.Attachments;
                    fileAttach.Add(_Str_File_Name, contents );
                    }
                   //  Update item which will add new item to list
                    item.Update();
                    // if you want to manipulate on newly created item then you can get id of the newly created item by following way
                  // return the ID
                   return  _Int_ID = item.ID;
                }
            }        

        }

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

        public void attachmentbylistid(int ItemId)
        {
            using (SPSite _site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb _web = _site.OpenWeb())
                {
                    // for testing Let's suppose your Item Id is 1
                  // int ItemId = 1;
                    SPList oList = _web.Lists["EmployeeList"];
                    SPListItem _item = oList.GetItemById(ItemId);
                    if (fileUpload.HasFile)
                    {
                        _web.AllowUnsafeUpdates = true;
                        Stream fs = fileUpload.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(fileUpload.PostedFile.FileName, _bytes);
                        _item.Update();
                        _web.AllowUnsafeUpdates = false;
                    }
                }
            }

        }


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

// pass ListItem ID , File name
        private void DeleteAttachment(int NodeID, String strFileName)
        {
            SPContext.Current.Web.AllowUnsafeUpdates = true;
            SPList List = SPContext.Current.Web.Lists["EmployeeList"];
            SPListItem delItem = List.GetItemById(NodeID);
            SPAttachmentCollection atCol = delItem.Attachments;
            foreach (string strDelfileName in atCol)
            {
                if (strDelfileName == strFileName)
                {
                    atCol.Delete(strDelfileName);
                    delItem.Update();
                    return;
                }
            }

        }


Add new Items into List programmatically in SharePoint 2010 using C#
        private void AddListItem()
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["EmployeeList"];
                    web.AllowUnsafeUpdates = true;
                    SPListItem item = list.Items.Add();
                    item["Title"] = txtFirstName.Text.Trim();
                    item["FirstName"] = txtFirstName.Text.Trim();
                    item["LastName"] = txtLastName.Text.Trim();
                    item["Address"] = txtAddress.Text.Trim();
                    item["Phone"] = txtPhone.Text.Trim();
                    item["City"] = txtCity.Text.Trim();
                    item["State"] = txtState.Text.Trim();
                    item["Country"] = txtCountry.Text.Trim();
                    item.Update();
                    web.AllowUnsafeUpdates = false;
                }
            }
        }


Update List Items By ListItemID  into List programmatically in SharePoint 2010 using C#

        public void UpdateListItem(int _ListItemID)
        {
            using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists["EmployeeList"];    
                    SPQuery _Query = new SPQuery();
                    _Query.Query = string.Format("<Where><Eq><FieldRef Name='ID' /><Value Type='Counter' >{0}</Value> </Eq> </Where>", _ListItemID); 
                    
                    //- Assign query to list and get item on the basis of the query build above
                    SPListItemCollection common = list.GetItems(_Query);
                    // Assign value for newly created item
                    if (common.Count > 0)
                    {
                        web.AllowUnsafeUpdates = true;
                        //Get first list item from the result list
                        SPListItem item = common[0];
                        item["Title"] = txtFirstName.Text.Trim();
                        item["FirstName"] = txtFirstName.Text.Trim();
                        item["LastName"] = txtLastName.Text.Trim();
                        item["Address"] = txtAddress.Text.Trim();
                        item["Phone"] = txtPhone.Text.Trim();
                        item["City"] = txtCity.Text.Trim();
                        item["State"] = txtState.Text.Trim();
                        item["Country"] = txtCountry.Text.Trim();
                        item.Update();
                        web.AllowUnsafeUpdates = false;
                    }                    
                }
            }
        }

Delete List Items By ListItemID into List programmatically in SharePoint 2010 using C#

        public void DeleteListItem(int _ListItemID)
        {
            using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists["EmployeeList"];
                    SPQuery _Query = new SPQuery();
                    _Query.Query = string.Format("<Where><Eq><FieldRef Name='ID' /><Value Type='Counter' >{0}</Value> </Eq> </Where>", _ListItemID);

                    //- Assign query to list and get item on the basis of the query build above
                    SPListItemCollection common = list.GetItems(_Query);
                    // Assign value for newly created item
                    if (common.Count > 0)
                    {
                        //- Set allowunsafeupdates for web to true
                        web.AllowUnsafeUpdates = true; 
                        //- Delete selected item from fetched list 
                        common[0].Delete(); 
                        //- Update item which will add new item to list
                        web.Update(); 
                    }
                }
            }

        }





      Get SharePoint List Item by ID

        private void GetListItembyID(int ItemID)
        {
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["EmployeeList"];
SPListItem Item = list .GetItemById(ItemID);
Response.Write("Item Title: " + Item["Title"].ToString());
               }
          }
      }

Deleting Multiple Versions of a List Item

When you delete multiple versions of a list item, use the DeleteByID method; do not use the Delete method. You will experience performance problems if you delete each SPListItemVersion object from an SPListItemVersionCollection object. The recommended practice is to create an array that contains the ID properties of each version and then delete each version by using the SPFileVersionCollection.DeleteByID method. The following code examples demonstrate both the approach that is not recommended and the recommended approach to deleting all versions of the first item of a custom list.
Good Coding Practice
Deleting each version of a list item by using the SPFileVersionCollection.DeleteByID method
SPSite site = new SPSite("site url");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["custom list name"];
SPListItem item = list.GetItemById(1);
SPFile file = web.GetFile(item.Url);
SPFileVersionCollection collection = file.Versions;
ArrayList idList = new ArrayList();
foreach (SPFileVersion ver in collection)
{  idList.Add(ver.ID);
}foreach (int verID in idList)
{try{
  collection.DeleteByID(verID);
}catch (Exception ex)
{  MessageBox.Show(ex.Message); 
}}
If you are deleting versions of items in a document library, you can use a similar approach by retrieving the SPListItem.File.Versions property.