Create visual web part in SharePoint 2013/2016 using visual studio

This SharePoint tutorial, we will discuss, how to 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. We will also see, how to add custom web part properties to visual webpart in sharepoint 2013 or SharePoint 2016.

Create visual web part in SharePoint using 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:

sharepoint visual web part

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 the “Deploy as farm solution” option. The screen should look like below:

sharepoint 2013 visual web part

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

sharepoint 2016 visual web part

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 web part in sharepoint

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 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 web part 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 web part in sharepoint 2016

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 web part in sharepoint 2013 using visual studio

This will deploy the solution to the SharePoint site.

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

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

create visual web part in sharepoint using visual studio

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:

how to create visual web part in sharepoint

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

how to create visual web part in sharepoint 2013

This way, we can create a visual web part in SharePoint 2013 using visual studio.

Add Custom Web Part Properties in SharePoint 2013 Visual Web Part

This SharePoint tutorial, we will discuss, how to add a custom property to the SharePoint 2013 visual web part as well as how we can set default value also.

In that web part, I have a label control and I want to set a default value for the label. Also, users should able to change the value from web part properties.

Here we will deal with three files, one the Visual web part .aspx file as well as the visual web part code file. And the web part file (MyTestVisualWP.cs).

Open your Web part file (MyTestVisualWP.cs) and here we need to add properties to the web part like below:

const string const_Name = "EnjoySharePoint";
private string _yourName = const_Name;

[Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [Category("My Category")] [WebDisplayName("Enter Your Name")] [WebDescription("Provide your name.")] [DefaultValue(const_Name)]
public string YourName
{
get
{
return _yourName;
}
set
{
_yourName = value;
}
}

Now your web part file should look like below:

add custom web part properties to visual webpart in sharepoint 2013

Now Open the web user control file and write like below:

public MyTestVisualWP WebPart { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
this.WebPart = this.Parent as MyTestVisualWP;
lblMessage.Text = "Hello: " + this.WebPart.YourName;
}

Your code should look like below:

add custom web part properties to visual webpart in sharepoint 2016

Now deploy your visual web part to the site. Then add the web part to the page and it will show the default value like below:

how to create custom properties for visual webpart in sharepoint 2013

Now edit the page and then edit the web part. You will be able to see the web part properties like below and you change the value like below:

how to create custom properties for visual webpart in sharepoint 2016

Now the changed value will appear like below:

add custom web part properties to visual webpart in sharepoint 2013

Error occurred in deployment step Recycle IIS Application Pool in SharePoint

Now, let use see how to fix the error, which usually comes when we try to deploy a SharePoint solution built using Visual Studio.

The full error message is: “Error occurred in deployment step ‘Recycle IIS Application Pool’: The local SharePoint server is not available. Check that the server is running and connected to the SharePoint farm.”

Error occurred in deployment step Recycle IIS Application Pool in SharePoint

The error comes if your sql server is not running. So to solve the issue you can try folling things:

Make sure you are able to connect to database server. Try to connect the SQL server using SQL Server Management Studio.

To deploy these types of things you need admin privileges. So make sure your Visual Studio is running as an Administrator.

Also, make sure you have db_owner rights to the below databases:

  • SharePoint_Config
  • SharePoint_AdminContent_GUID
  • Your current web application that needs to deploy the Web part.
  • Make sure your SQL Server (MSSQLSERVER) service is running. You can check this in the Services (Start -> Run -> services.msc)

You may like following SharePoint tutorials:

Here, we learned how to create a visual web part in SharePoint 2013 using visual studio. Also, we learned, how to add custom web part properties to visual webpart in sharepoint 2013 or SharePoint 2016.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

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 SPGuides.com

>