Create a new team site in SharePoint.
Create a new list named "Employee Registration"
After the list has been created you need to create columns for saving employee details.
Now the columns have been created successfully.
Next we need to create a new “Empty SharePoint project” in Microsoft Visual Studio named “EmployeeReg”. Click OK.
Validate the site. After the validation successful, Deploy as a Farm solution.
Click Finish.
Now right-click on the solution, click Add and select new item. Create a “New Visual Webpart” named “EmployeeRegistration”.
Now Add the Fields and labels into Employee Registration form.
Double-click on the register button to add the code.
Use this namespace.
- using Microsoft.SharePoint;
Full Code
- using Microsoft.SharePoint;
- using System;
- using System.ComponentModel;
- using System.Web.UI.WebControls.WebParts;
-
- namespace EmployeeReg.EmployeeRegistration
- {
- [ToolboxItemAttribute(false)]
- public partial class EmployeeRegistration : WebPart
- {
-
-
-
-
-
- public EmployeeRegistration()
- {
- }
-
- protected override void OnInit(EventArgs e)
- {
- base.OnInit(e);
- InitializeControl();
- }
-
- protected void Page_Load(object sender, EventArgs e)
- {
- }
-
- protected void btnregister_Click(object sender, EventArgs e)
- {
- try
- {
- using(SPSite site = new SPSite(SPContext.Current.Web.Url))
- {
- using(SPWeb web = site.OpenWeb())
- {
- SPList list = web.Lists.TryGetList("Employee Registration");
- if(list!=null)
- {
- SPListItem NewItem = list.Items.Add();
- {
- web.AllowUnsafeUpdates = true;
- NewItem["Employee Name"] = txtempname.Text;
- NewItem["Designation"] =drpdesg.SelectedItem.ToString();
- NewItem["Address"] =txtaddr.Text;
- NewItem["Email"] =txtemail.Text;
- NewItem["Contact No"] = txtcontact.Text;
- NewItem.Update();
- Alert.Text = "Registration Successful";
-
- }
- }
- else
- {
- Alert.Text = "List not found";
- }
- }
- }
-
-
- }
- catch(Exception ex)
- {
- Alert.Text = ex.Message.ToString();
- }
- }
-
- protected void btnclear_Click(object sender, EventArgs e)
- {
- txtempname.Text = "";
- txtemail.Text = "";
- drpdesg.SelectedIndex = -1;
- txtcontact.Text = "";
- txtaddr.Text = "";
- Alert.Text = "";
- }
- }
- }
Now build the project and then
Deploy solution.
Go to the site and edit the page, then add a Webpart from the
custom tab.Now
Register with some details.
The data has been added into the list.
Now click Clear.
All the fields are cleared successfully.
I hope this will help SharePoint beginners to start developing custom webparts.