You are on page 1of 8

33_SharePoint_Server_Object_Model

Create a New Project ServerObjectModel_List_Web


Rt Click---Add---New Item----Select VisualWebPart
Name-----Emp_Registration
Copy and paste the following code in design
<link href="../_layouts/15/1033/STYLES/Server_Object/Styles.css" rel="stylesheet" />
<table class="tbltopstyle">
<tr>
<th colspan="2">Registration Form</th>
</tr>
<tr>
<td>Employee Name</td>
<td>
<asp:TextBox ID="txtEName" runat="server"
CssClass="txtstyle"></asp:TextBox></td>
</tr>
<tr>
<td>Father Name</td>
<td>
<asp:TextBox ID="txtFName" runat="server"
CssClass="txtstyle"></asp:TextBox></td>
</tr>
<tr>
<td>Gender</td>
<td>
<asp:DropDownList ID="ddlGender" runat="server" CssClass="txtstyle">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td>Salary</td>
<td>
<asp:TextBox ID="txtSalary" runat="server"
CssClass="txtstyle"></asp:TextBox></td>
</tr>
<tr><td colspan="2">
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"
/></td></tr>
</table>
<asp:Label ID="lblMsg" runat="server" Text="" ForeColor="Green"></asp:Label>

Style.css
.tbltopstyle
{
width: 600px;
}
.tbltopstyle th
{
font-family: Arial;
font-size: 23px;
background-color: #0e83cd;
color:#333;
}
.tbltopstyle td
{
font-family: Arial;
font-size: 18px;
color:#333;
}
.txtstyle
{
border: 1px solid #0e83cd;
width: 300px;
}
.spansuccess
{
font-family: Arial;
font-size: 18px;
color:#333;
font-weight:bold;
}

The Above code output is as follows

Go to CS File and write the following code.


public string listname = "Employee_Corner";

protected void Page_Load(object sender, EventArgs e)


{
if(!Page.IsPostBack==false)
{
if (CheckList() == false)
{
//Creating Emplist
SPWeb web = SPContext.Current.Web;
web.Lists.Add(listname,"Employee
Tracking",SPListTemplateType.GenericList);
SPList list = web.Lists[listname];
list.Fields.Add("Father Name", SPFieldType.Text, true);
StringCollection sgenders=new StringCollection ();
sgenders.Add("Male");
sgenders.Add("Female");
list.Fields.Add("Gender", SPFieldType.Choice, false, false,
sgenders);
list.Fields.Add("Salary", SPFieldType.Number, true);
list.Fields.GetFieldByInternalName("Title").Title = "Emp Name";
list.Update();
}
}
}
public bool CheckList()
{
bool listavailable=true;
try
{
SPList list = SPContext.Current.Web.Lists[listname];
if (list.Title == listname)
{
listavailable = true;
}
}
catch(SPException exc)
{
listavailable = false;
}
catch (Exception exc)
{
listavailable = false;

}
return listavailable;
}
protected void btnSave_Click(object sender, EventArgs e)
{
SPSite site = new SPSite(SPContext.Current.Site.Url);
SPWeb web = site.OpenWeb();
SPList list = web.Lists[listname];
SPListItem newitem = list.AddItem();
newitem["Title"] = txtEName.Text;
newitem["Father_x0020_Name"] = txtFName.Text;
newitem["Gender"] = ddlGender.SelectedItem.Text;
newitem["Salary"] = txtSalary.Text;
newitem.Update();
lblMsg.Text = "A new Employee with Employee ID:<span
class='spansuccess'>" + newitem.ID + "</span> is created Successfully";
}

Deploy

After deploying successfully, create new page and add corresponding webpart.
By that time, we can see new list called Employee_Corner.
Go and check in SiteContents.
After clicking on Employee_Corner.
The following output will be displayed.

How to Create Sub Site by using Server Object Model


Rt click on Project---add new---select Visual WebPart---------Name=BlogCreation.

Go to BlogCreation design file and paste the following


<link href="../_layouts/15/1033/STYLES/Server_Object/Styles.css" rel="stylesheet" />
<table class="tbltopstyle">
<tr>
<th colspan="2">Blogs Administration</th>
</tr>
<tr>
<td>Blog Title</td>
<td>
<asp:TextBox ID="txtBName" runat="server"
CssClass="txtstyle"></asp:TextBox></td>
</tr>
<tr>
<td>Blog URL</td>
<td>
<asp:TextBox ID="txtburl" runat="server"
CssClass="txtstyle"></asp:TextBox></td>
</tr>
<tr>
<td>Description</td>
<td>
<asp:TextBox ID="txtDesc" runat="server"
CssClass="txtstyle"></asp:TextBox></td>
</tr>
<tr><td colspan="2">
<asp:Button ID="btnCreate" runat="server" Text="Create"
OnClick="btnCreate_Click" /></td></tr>
</table>
<asp:Label ID="lblMsg" runat="server" Text="" ForeColor="Green"></asp:Label>

The Above code out put looks like the following

In CS File, write the following.


protected void btnCreate_Click(object sender, EventArgs e)
{
SPSite site = new SPSite(SPContext.Current.Site.Url);

SPWeb newweb=site.AllWebs.Add(txtburl.Text, txtBName.Text, txtDesc.Text,


1033, "BLOG#0", false, false);
lblMsg.Text = "A New Blog is Created Successfully.<br/>Please click here
to navigate to <a href='" + newweb .ServerRelativeUrl+ "'>blog</a>";
}//Get-SPWebTemplate

Using the following command we can find templates.

After deploying, create new page and test it.


Like the following should be displayed as out put.