Search This Blog

Monday, February 13, 2012

Insert Data Into Sharepoint List Using C# Code


using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;


using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;


namespace insertdataintocustomlist
{
    [Guid("8470d1da-6e24-479c-b0e8-102f82a41a68")]
    public class insertdatademo : System.Web.UI.WebControls.WebParts.WebPart
    {


        public TextBox oTextTitle;
        public TextBox oTextEmployeeName;
      //public TextBox oTextEmployeeID;
        public TextBox oTextEmployeeLocation;
        public TextBox oTextEmployeeDesignation;
        public TextBox oTextEmployeeDateOfJoin;
        public Label oLabelMessage;
        public Button oButtonSubmit;
        public Calendar my;
        
        
        public insertdatademo()
        {
            my = new Calendar();
          
        }


        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            
            
            oTextTitle = new TextBox();
            this.Controls.Add(oTextTitle);


            oTextEmployeeName = new TextBox();
            this.Controls.Add(oTextEmployeeName);


            oTextEmployeeDesignation = new TextBox();
            this.Controls.Add(oTextEmployeeDesignation);


             oTextEmployeeDateOfJoin = new TextBox();
             my = new Calendar();
             my.DataBind();
             my.SelectionChanged += new EventHandler(my_SelectionChanged);
             this.Controls.Add(oTextEmployeeDateOfJoin);
             this.Controls.Add(my);


            //oTextEmployeeID = new TextBox();
            //this.Controls.Add(oTextEmployeeID);


            oTextEmployeeLocation = new TextBox();
            this.Controls.Add(oTextEmployeeLocation);


            oLabelMessage = new Label();
            this.Controls.Add(oLabelMessage);


            oButtonSubmit = new Button();
            oButtonSubmit.Text = "Submit";
            oButtonSubmit.CssClass = "ms-ButtonHeightWidth";
            this.Controls.Add(oButtonSubmit);


            oButtonSubmit.Click += new EventHandler(oButtonSubmit_Click);




            // TODO: add custom rendering code here.
            //Label label = new Label();
            //label.Text = "Hello World";
            //this.Controls.Add(label);
        }


        void my_SelectionChanged(object sender, EventArgs e)
        {
            string strDate = my.SelectedDate.Year + "-" + my.SelectedDate.Month + "-" + my.SelectedDate.Day;
            oTextEmployeeDateOfJoin.Text = strDate;
        }


        void oButtonSubmit_Click(object sender, EventArgs e)
        {
            if (oTextTitle.Text.Trim() == "" || oTextEmployeeName.Text.Trim() == "" || oTextEmployeeDesignation.Text.Trim() == "" || oTextEmployeeLocation.Text.Trim() == "")
            {
                oLabelMessage.Text = "You must specify a value for this required field";
            }
            else
            {
                try
                    {
                SPSite mySite = SPContext.Current.Site;
                SPWeb myWeb = SPContext.Current.Web;
                myWeb.AllowUnsafeUpdates = true;
                
                DateTime dateTimeValue = new DateTime();
                dateTimeValue = Convert.ToDateTime(oTextEmployeeDateOfJoin.Text);
                //dateTimeValue = my.SelectedDates;
                string strDate = dateTimeValue.Year + "-" + dateTimeValue.Month + "-" + dateTimeValue.Day;


                
                SPList myList = myWeb.Lists["Employee's Register"];
                SPListItem myListItem = myList.Items.Add();
                myListItem["Title"] = oTextTitle.Text.ToString();
                myListItem["Employee Name"] = oTextEmployeeName.Text.ToString();
                myListItem["Employee Designation"] = oTextEmployeeDesignation.Text.ToString();
                myListItem["Employee Location"] = oTextEmployeeLocation.Text.ToString();
                myListItem["Employee DteOfJoin"] = strDate;
             //  myListItem["Employee ID"] = oTextEmployeeID.Text.ToString();


                
                myListItem.Update();
                myWeb.AllowUnsafeUpdates = false;
                oLabelMessage.Text = "Recorded inserted";
                ClearChildControlState = true;
                    }
                    catch(Exception ex)
                {
                    oTextEmployeeDateOfJoin.Text = ex.Message;
                    }
            }
        }
       
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<table class='ms-toolbar' cellpadding='2' cellspacing='0' border='0' width='100%'><tr><td class='ms-toolbar' width='100%'><span class='ms-formvalidation'>");
            oLabelMessage.RenderControl(writer);


            writer.Write("</span</td><td width='100%' class='ms-toolbar' nowrap><IMG SRC='/_layouts/images/blank.gif' width='1' height='18' alt=''></td><td class='ms-toolbar' nowrap='true'><span style='white-space: nowrap;padding-right: 3px;' class='ms-descriptiontext'><span class='ms-formvalidation'>*</span> indicates a required field</span></td></tr></table>");
            writer.Write("<Table class='ms-formtable' style='margin-top: 8px;' border=0 cellpadding=0 cellspacing=0 width=100%>");


            writer.Write("<TR><TD nowrap='true' valign='top' width='190px' class='ms-formlabel'><H3 class='ms-standardheader'><nobr>Title<span class='ms-formvalidation'> *</span></nobr></H3></TD><TD valign='top' class='ms-formbody' width='400px'><span dir='none'>");
            oTextTitle.RenderControl(writer);
            writer.Write("<br></span></TD></TR>");


            writer.Write("<TR><TD nowrap='true' valign='top' width='190px' class='ms-formlabel'><H3 class='ms-standardheader'><nobr>Employee Name<span class='ms-formvalidation'> *</span></nobr></H3></TD><TD valign='top' class='ms-formbody' width='400px'><span dir='none'>");
            oTextEmployeeName.RenderControl(writer);
            writer.Write("<br></span></TD></TR>");


            writer.Write("<TR><TD nowrap='true' valign='top' width='190px' class='ms-formlabel'><H3 class='ms-standardheader'><nobr>Employee Designation<span class='ms-formvalidation'> *</span></nobr></H3></TD><TD valign='top' class='ms-formbody' width='400px'><span dir='none'>");
            oTextEmployeeDesignation.RenderControl(writer);
            writer.Write("<br></span></TD></TR>");


            writer.Write("<TR><TD nowrap='true' valign='top' width='190px' class='ms-formlabel'><H3 class='ms-standardheader'><nobr>Employee Location<span class='ms-formvalidation'> *</span></nobr></H3></TD><TD valign='top' class='ms-formbody' width='400px'><span dir='none'>");
            oTextEmployeeLocation.RenderControl(writer);
            writer.Write("<br></span></TD></TR>");


            writer.Write("<TR><TD nowrap='true' valign='top' width='190px' class='ms-formlabel'><H3 class='ms-standardheader'><nobr>Employee DateOfJoin<span class='ms-formvalidation'> *</span></nobr></H3></TD><TD valign='top' class='ms-formbody' width='400px'><span dir='none'>");
            oTextEmployeeDateOfJoin.RenderControl(writer);
            my.RenderControl(writer);
            writer.Write("<br></span></TD></TR>");


            /*writer.Write("<TR><TD nowrap='true' valign='top' width='190px' class='ms-formlabel'><H3 class='ms-standardheader'><nobr>Employee ID<span class='ms-formvalidation'> *</span></nobr></H3></TD><TD valign='top' class='ms-formbody' width='400px'><span dir='none'>");
            oTextEmployeeID.RenderControl(writer);
            writer.Write("<br></span></TD></TR>");*/




            writer.Write("<table class='ms-formtoolbar' cellpadding='2' cellspacing='0' border='0' width='100%'><tr><td width='99%' class='ms-toolbar' nowrap><IMG SRC='/_layouts/images/blank.gif' width='1' height='18' alt=''></td><td class='ms-toolbar' nowrap='true'><TABLE cellpadding='0' cellspacing='0' width='100%'><TR><TD align='right' width='100%' nowrap>");
            oButtonSubmit.RenderControl(writer);
            writer.Write("</TD></TR></TABLE></td></tr></table>");
        }






    }
}

No comments:

Post a Comment