Create Visual WebPart using Visual Studio 2013 in SharePoint 2013

Create Visual WebPart using Visual Studio 2013 in SharePoint 2013

In this post we will discuss how we can create a visual web part using Visual Studio 2013 in SharePoint 2013. Here we will try to create a visual web part by using the Visual web part template in the visual studio.

Open your visual studio 2013 and then go to File -> New Project. This will open the New Project dialog box. There from the left side installed templates, select “SharePoint Solutions” which is under Templates -> Visual C# -> Office/SharePoint. The from the right side choose the .Net Framework as 4.5 and also choose “SharePoint 2013 – Empty Project” template which looks like fig below:

Create Visual WebPart using Visual Studio 2013 in SharePoint 2013
Create Visual WebPart using Visual Studio 2013 in SharePoint 2013

Then it will open the SharePoint Customization Wizard. Here it will ask for the Site URL to debug. After giving the URL you can validate the URL by clicking on the Validate button. Then choose “Deploy as farm solution” option. The screen should look like below:

Create Visual WebPart using Visual Studio 2013 in SharePoint 2013
Create Visual WebPart using Visual Studio 2013 in SharePoint 2013

This will create the Empty SharePoint project. Now Right Click on the Project and then click on Add -> New Item as shown in the fig below:

Create Visual WebPart using Visual Studio 2013 in SharePoint 2013
Create Visual WebPart using Visual Studio 2013 in SharePoint 2013

Then from the Add New Item dialog box, from the Office/SharePoint templates, choose (Visual Web Part (Farm Solution Only)) as shown in the fig below:

Create Visual WebPart using Visual Studio 2013 in SharePoint 2013
Create Visual WebPart using Visual Studio 2013 in SharePoint 2013

Once it will add the Visual Web Part Project, you can Open the ascx file and write the code. Here I am simply adding a textbox control, a button control, and a label control. My idea is on button click we want to display a message. So the code will look like below:
<asp:TextBox ID=”txtName” runat=”server”></asp:TextBox>

<asp:Button ID=”btnClickHere” runat=”server” Text=”Click Here” OnClick=”btnClickHere_Click” /><br />

<asp:Label ID=”lblMessage” runat=”server” Text=””></asp:Label>

Create Visual WebPart using Visual Studio 2013 in SharePoint 2013
Create Visual WebPart using Visual Studio 2013 in SharePoint 2013

Now you can right click and go to the code behind file. And write the below code in the button click.
protected void btnClickHere_Click(object sender, EventArgs e)

{

lblMessage.Text = “Hello: ” + txtName.Text.Trim();

}

The code behind code looks like below:

Create Visual WebPart using Visual Studio 2013 in SharePoint 2013
Create Visual WebPart using Visual Studio 2013 in SharePoint 2013

Now Build the solution and if no errors are there then you can right click on the Solution explorer and can click on Deploy the solution.

Create Visual WebPart using Visual Studio 2013 in SharePoint 2013
Create Visual WebPart using Visual Studio 2013 in SharePoint 2013

This will deploy the solution in the site.

When you Build the solution it will generate a wsp file which you can take and deploy in different servers (Like testing or Production where visual studio is not installed usually).

If you want to deploy using PowerShell you can read below articles:
How to deploy farm wsp solution in SharePoint 2010 using PowerShell?

How to deploy a sandboxed solution in SharePoint 2010?

Depending on the mode you have selected the wsp will be available either in Debug folder or in Release folder like below:

Create Visual WebPart using Visual Studio 2015 in SharePoint 2016
Create Visual WebPart using Visual Studio 2015 in SharePoint 2016

Now you can open your SharePoint 2013 site and edit the page where you want to add your custom visual web part . When you click on Add web part, you will be able to see your custom web part under “Custom” web part categories like below:

Create Visual WebPart using Visual Studio 2013 in SharePoint 2013
Create Visual WebPart using Visual Studio 2013 in SharePoint 2013

When you will click on the button, you will be able to see the message in the label like below:

Create Visual WebPart using Visual Studio 2013 in SharePoint 2013
Create Visual WebPart using Visual Studio 2013 in SharePoint 2013

Read some SharePoint PowerShell tutorials:

Hope this will be helpful to you.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →