Search This Blog

Monday, January 17, 2011

Custom Webpart – Inserts items into Sharepoint Custom list

Here is the simple custom webpart which inserts items into (Title, Employee Name, Designation) Sharepoint custom list, before executing the webpart first of all you have to create custom list and required fields manually through UI as said below:

Create a custom list, name it as Custom List (you can also choose different name, but make sure to modify the same in the code too) then create columns as mentioned…..

Title [Single line text] (this is by default available, no need of re-creating)
Employee Name [Single line text]
Designation [Single line text]


Insert Into Custom List - Input Form

Insert Into Custom List - Record Inserted



List view displays records after inserting

Complete Source Code:

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

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

namespace InsertIntoList
{
    [Guid("4f70d2fa-e335-471d-94c6-5bec8d034032")]
    public class InsertIntoList : System.Web.UI.WebControls.WebParts.WebPart
    {
        TextBox oTextTitle;
        TextBox oTextName;
        TextBox oTextDesignation;
        Label oLabelMessage;
        Button oButtonSubmit;
        protected override void CreateChildControls()
        {
            base.CreateChildControls();

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

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

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

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

        void oButtonSubmit_Click(object sender, EventArgs e)
        {
            if (oTextTitle.Text.Trim() == "" || oTextName.Text.Trim() == "" || oTextDesignation.Text.Trim() == "")
            {
                oLabelMessage.Text = "You must specify a value for this required field";
            }
            else
            {
                SPSite mySite = SPContext.Current.Site;
                SPWeb myWeb = SPContext.Current.Web;
                SPList myList = myWeb.Lists["Custom List"];
                SPListItem myListItem = myList.Items.Add();
                myListItem["Title"] = oTextTitle.Text.ToString();
                myListItem["Employee Name"] = oTextName.Text.ToString();
                myListItem["Designation"] = oTextDesignation.Text.ToString();
                myWeb.AllowUnsafeUpdates = true;
                myListItem.Update();
                myWeb.AllowUnsafeUpdates = false;
                oLabelMessage.Text = "Recorded inserted";
            }
        }

        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'>");
            oTextName.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>Designation<span class='ms-formvalidation'> *</span></nobr></H3></TD><TD valign='top' class='ms-formbody' width='400px'><span dir='none'>");
            oTextDesignation.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>");
        }
    }
}

Enjoy!!!!!!

1 comment:

  1. Wow. Thank you so much for posting the complete code. Now it will be very easy for me to implement the same. I will do follow your blog regularly.
    electronic signature for sharepoint

    ReplyDelete