SharePoint 2013 web parts tutorial

In this SharePoint tutorial, we will discuss what are web parts in SharePoint 2013/2016/2019? In this SharePoint web parts examples, we will discuss how to create a web part using visual studio in SharePoint 2016/2013.

We will also see, what is a web part manager, and what is a web part zone in SharePoint?

In this SharePoint 2013 webpart tutorial, we will see how to create a visual web part in SharePoint.

What is a web part in SharePoint

What is a web part in SharePoint? Web Parts are one of the more important features of SharePoint, and without Web Parts, SharePoint might not have been as successful as it is. Web Parts are not unique to SharePoint, because every portal platform has similar concepts that are called different names, such as portlets or widgets.

A Web Part is an ASP.NET server control which is added to a Web Part Zone on Web Part Pages by users at runtime. The controls enable end-users to modify the content, appearance, and behavior of Web pages directly from a browser. It can be put into certain places on a web page by end-users, after developing by a programmer.

SharePoint 2013 web parts, they can display information, and they can ask for information from users. Users can add Web Parts to pages, selecting different Web Parts from a gallery, and creating their own unique pages and experiences.

The Web Parts themselves can have different configurations, even for different users. SharePoint uses Web Parts a lot; each list view and each list form or document library is displayed through a Web Part known as a List Viewer Web Part.

A Web Part is essentially a specific derivative of the System.Web.UI.WebControls.WebControl control. All custom Web Parts must derive from the abstract WebPart class, defined in the System.Web.UI.WebControls.WebParts namespace. The WebPart class also implements some specific interfaces that define the Web Part (IWebPart) and define the behavior of the Web Part (IWebEditable and IWebActionable).

What is a SharePoint Web Part Zone

Web part must exist in a Web Part zone. The Web Part zone is an ASP.NET control called WebPartZone. The Web Part zone defines a region on the page where Web Parts can be hosted, and the responsibility of the Web Part zone control is to layout the Web Parts.

<WebPartPages:WebPartZone runat="server" ID="zone" Title="The Zone" LayoutOrientation="Horizontal"/>

The Web Part zone determines how the Web Parts are rendered on the page. The ID property should be treated carefully.

After a web part zone is used on a page and a Web Part is added to that zone, the id of the zone is used when storing the Web Part state in SharePoint.

If you remove the zone or change its id without removing the Web Parts, the saved Web Part state will be orphaned in the SharePoint databases.

But after you change the id back to its original value or add another zone with the same id as the previous zone, the Web Parts are once again available.

What is a Web Part Manager in SharePoint

One of the most important components of the Web Part framework is the Web Part Manager. The SharePoint Web Part Manager is responsible for retrieving and storing the Web Part data in the SharePoint content databases.

A Web Part Manager object is required on all Web Part pages. SharePoint adds the SPWebPartManager to the default master pages.

You should also add this if you’re creating your own master page. When a page is loaded, the Web Part Manager loads the Web Part from the persisted storage, including its state and the shared or personal settings of the Web Part, and populates the Web Part zones.

SharePoint Out of box web parts

Microsoft provides various out of web parts that you can use inside a SharePoint web part page.

When you edit the SharePoint web part page and click on +Add a web part, you can see the out of box web parts.

The Apps web part category will display all your list and libraries presented in the site. You can select any list or library and the list view web part will be added.

There are also other web parts available for Blogs, Business Data, Community, Search, Content Rollup, Document Sets, Filters etc.

sharepoint out of box webparts
sharepoint out of box webparts

A few other out of box SharePoint web parts are:

Create a SharePoint web part using visual studio

Here in this section, we will discuss how to create a web part in SharePoint 2016 using visual studio 2015/2017/2019. The same approach we can follow to create a web part in SharePoint 2013 also.

We can easily create a web part using a visual studio for SharePoint 2016 site. Follow the below steps to do so. Open visual studio and then File -> New -> Project.

Then from the Installed templates expand to Office/SharePoint and then choose SharePoint Solutions. And then choose “SharePoint 2016 – Empty Project”. Give a name and click on OK.

sharepoint web parts examples
sharepoint web parts examples

Then give a local SharePoint site as debugging URL. And choose as a farm solution.

sharepoint 2013 webpart tutorial
webparts in sharepoint 2016 : Create web part using visual studio SharePoint 2016

Once you click on the finish button, it will create the empty SharePoint project. Then right-click on the project -> Add -> New Item… This will open the Add New Item dialog box. Here choose Web Part as shown in the fig below:

webparts in sharepoint 2013 : Create web part using visual studio 2015 SharePoint 2013
webparts in sharepoint 2013 : Create web part using visual studio SharePoint 2013

Once you will add it a web part will be added to the project. You can see there will be one <webpart name>.cs file. Here add the below code to the CreateChildControls() method. The WebPart class calls this method to create child server controls that the Web Part will later convert to HTM. It should look like below:

protected override void CreateChildControls()
{
HtmlGenericControl heading = new HtmlGenericControl("h1");
heading.InnerText = "Hello " + SPContext.Current.Web.CurrentUser.Name;
this.Controls.Add(heading);
}

Then right-click on the project and deploy the solution. Once it will be successfully added, open any web part page. Once you open your web part page, edit the page and click on Add a web part link. Then it will open the web part categories in the ribbon.

From the web part categories, select Custom and there you can see our demo web part like below. Select the web part and click on Add.

sharepoint web parts examples : Create web part using visual studio 2015 SharePoint 2013
sharepoint web parts examples : Create web part using visual studio 2015 SharePoint 2013

Once you have added the web part, the message will appear like below:

Create web part using visual studio 2015 SharePoint 2013
Create web part using visual studio SharePoint 2013

Create a Visual Web Part in SharePoint

We can also create a visual web part in SharePoint 2013/2016/2019 using visual studio. Visual web parts are like web parts, but they will have additional user control where you can drag and drop .Net controls.

I wrote a complete article on how to Create and Deploy Visual web part in SharePoint using Visual Studio.

Read some SharePoint web part tutorials:

This SharePoint tutorial, we learned:

  • What are web parts in SharePoint
  • What is a SharePoint Web Part Zone
  • What is a Web Part Manager in SharePoint
  • Create a SharePoint web part using visual studio
  • Create and Deploy Visual web part in SharePoint
>