Wednesday, January 16, 2013

Custom Webpart – Inserts items into sharepoint custom list usng CreateChildControls() method

Custom Webpart – Inserts items into sharepoint custom list

10 October, 2008 (14:37) | MOSS - Object Model | By: GVK
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 - Input Form
Download complete source code
Insert Into Custom List - Record Inserted
Insert Into Custom List - Record Inserted
List view displays records after inserting
List view displays records after inserting

  1. using System;  
  2. using System.Runtime.InteropServices;  
  3. using System.Web.UI;  
  4. using System.Web.UI.WebControls.WebParts;  
  5. using System.Xml.Serialization;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. using Microsoft.SharePoint;  
  9. using Microsoft.SharePoint.WebControls;  
  10. using Microsoft.SharePoint.WebPartPages;  
  11.   
  12. namespace InsertIntoList  
  13. {  
  14.     [Guid("4f70d2fa-e335-471d-94c6-5bec8d034032")]  
  15.     public class InsertIntoList : System.Web.UI.WebControls.WebParts.WebPart  
  16.     {  
  17.         TextBox oTextTitle;  
  18.         TextBox oTextName;  
  19.         TextBox oTextDesignation;  
  20.         Label oLabelMessage;  
  21.         Button oButtonSubmit;  
  22.         protected override void CreateChildControls()  
  23.         {  
  24.             base.CreateChildControls();  
  25.   
  26.             oTextTitle = new TextBox();  
  27.             this.Controls.Add(oTextTitle);  
  28.   
  29.             oTextName = new TextBox();  
  30.             this.Controls.Add(oTextName);  
  31.   
  32.             oTextDesignation = new TextBox();  
  33.             this.Controls.Add(oTextDesignation);  
  34.   
  35.             oLabelMessage = new Label();  
  36.             this.Controls.Add(oLabelMessage);  
  37.   
  38.             oButtonSubmit = new Button();  
  39.             oButtonSubmit.Text = "Submit";  
  40.             oButtonSubmit.CssClass = "ms-ButtonHeightWidth";  
  41.             this.Controls.Add(oButtonSubmit);  
  42.   
  43.             oButtonSubmit.Click += new EventHandler(oButtonSubmit_Click);  
  44.         }  
  45.   
  46.         void oButtonSubmit_Click(object sender, EventArgs e)  
  47.         {  
  48.             if (oTextTitle.Text.Trim() == "" || oTextName.Text.Trim() == "" || oTextDesignation.Text.Trim() == "")  
  49.             {  
  50.                 oLabelMessage.Text = "You must specify a value for this required field";  
  51.             }  
  52.             else  
  53.             {  
  54.                 SPSite mySite = SPContext.Current.Site;  
  55.                 SPWeb myWeb = SPContext.Current.Web;  
  56.                 SPList myList = myWeb.Lists["Custom List"];  
  57.                 SPListItem myListItem = myList.Items.Add();  
  58.                 myListItem["Title"] = oTextTitle.Text.ToString();  
  59.                 myListItem["Employee Name"] = oTextName.Text.ToString();  
  60.                 myListItem["Designation"] = oTextDesignation.Text.ToString();  
  61.                 myWeb.AllowUnsafeUpdates = true;  
  62.                 myListItem.Update();  
  63.                 myWeb.AllowUnsafeUpdates = false;  
  64.                 oLabelMessage.Text = "Recorded inserted";  
  65.             }  
  66.         } 

No comments:

Post a Comment