Skip to main content

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!!!!!!

Comments

  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

Post a Comment

Popular posts from this blog

Tab Control in Asp.Net

Scenerio: I need your help in designing tab control in   asp.net .My requirement is I need a tab control in   asp.net (C#) like  for example goto my computer ,right click c drive and select properties.we get tabs like general,security etc....... like that i need to design one tab control(no need of any rightclick) in my web page and the database table columns should come as tabs and inseide the tab data of that particular column should come.   Example:Employee master tab1:Employee name.........his name in the tab tab2:Age.............his age tab3:Address........his address   Solution:   You can do this using a simple div <style type="text/css"> .tabs         {             position: relative;             height: 20px;             margin: 0;   ...

AI and Microsoft: Revolutionizing Efficiency in Nonprofit Organizations

  How AI and Microsoft Enhance Efficiency in Nonprofit Organizations In today’s fast-paced world, nonprofit organizations face unique challenges—limited resources, increasing demands, and the constant need to do more with less. But what if technology could be the game-changer they need? In my latest research paper,  "How AI and Microsoft Enhance Efficiency in Nonprofit Organizations" , I explore how cutting-edge technologies like Artificial Intelligence (AI) and Microsoft’s innovative tools are revolutionizing the way nonprofits operate. From streamlining administrative tasks to enhancing donor engagement and optimizing resource allocation, AI and Microsoft’s solutions are empowering nonprofits to focus on what truly matters—their mission. This paper dives deep into real-world examples, practical applications, and the transformative potential of these technologies. Whether you’re a nonprofit professional, a tech enthusiast, or simply curious about the intersection of technolo...

Social tagging overview in Sharepoint 2010

A tag is a word or phrase that identifies an individual piece of information according to a set of attributes or criteria. Tags make it easy to find and share information about a specific subject or task. Social tagging helps users categorize information in ways that are meaningful to them. Social tagging can improve the quality of search results by filtering against specific tags, and it can also connect individuals who want to share information with other users who have like interests. This article describes the social tagging features in Microsoft SharePoint Server 2010. This article does not describe how to configure social tagging features. It also does not discuss how to implement social tagging features as part of an overall social media strategy for an enterprise. About using social tagging features Social tagging features help users to share information and to retrieve relevant, high-quality content more efficiently. Such sharing encourages collaboration and b...