SharePoint 2016 Develop and deploy custom feature using Visual Studio 2015

develop custom feature sharepoint 2015 using visual studio 2015

In this post, we will discuss how we can develop and deploy a custom feature using visual studio 2015 for SharePoint 2016. Follow below steps to do so:

Open Visual Studio 2015 and then File -> New -> Project

Then in the New Project dialog box, expand Visual C# tab then expand Office/SharePoint and then select SharePoint Solutions. Then from the right side choose SharePoint 2016 – Empty Project like below:

Develop and deploy custom feature using Visual Studio 2015
Develop and deploy custom feature using Visual Studio 2015

Then in the Site Customization wizard, first give a local site for debugging. You can always validate the site by clicking on the Validate button.
Then choose which type of solution you want to create, farm solution or sandboxed solution. Here choose the farm solution.
Then click on the Finish button.

develop custom feature sharepoint 2015 using visual studio 2015
develop custom feature sharepoint 2015 using visual studio 2015

Also, read:
SharePoint 2016 How to create list instance and list definition using Visual Studio 2015

Create List Definition and List Instance in SharePoint 2013 using Visual Studio 2013

Create web part page using visual studio 2013 in SharePoint 2013

It will take some time to configure the solution.
Once the solution got create you can see there will be one folder name as “Features”. Here we will add a feature to the project. Before proceeding further let’s understand what a feature is:

Features are packages of functionality that you can activate and deactivate in SharePoint farm. Features are the recommended method for deploying pieces of functionality, customizations, or configuration changes to front-end Web servers.

The feature is a server side functional component which can be installed and activated at various scopes throughout a SharePoint instance.

Feature Scope:

A feature can be activated in various scopes like below:
Farm: Activate a Feature for an entire server farm. Ex-web template which user can use to create sites.

WebApplication: Activate a Feature for all websites in a web application. Ex Timer Job

Site: Activate a Feature for all websites in a site collection. Ex Content type or site column

Web: Activate a Feature for a specific website. Ex List or Document library.

1. Farm: To manage features at Farm level: Central Administration->System Settings -> Manage Farm Features (Farm Management). Here we can activate or deactivate farm level features

2. Application: To manage features at the Application level: Central Administration->Web Applications Management->Manage Web Application Features

3. Site: To manage features at site level: Open Top Level Site->Site Actions-> Site Settings->Site Collection Administration->Site Collection Features

4. Web: To manage features at Page level: Open Page-> Site Actions-> Site Settings ->[under Site Administration] Site Features

Now back to the visual studio 2015 and right click on the Features folder and then click on Add Feature.

SharePoint 2016 Develop and deploy custom feature using Visual Studio 2015
SharePoint 2016 Develop and deploy custom feature using Visual Studio 2015

By default it will add a name as Feature1, you can rename it to a meaningful name.

Then in the Feature page, Give a Title, Description and select a scope. Here we will choose Web scope. The feature will be available on the particular site.

develop custom feature sharepoint 2015 using visual studio 2015
develop custom feature sharepoint 2015 using visual studio 2015

The solution now looks like below:

develop custom feature sharepoint 2015 using visual studio 2015
develop custom feature SharePoint 2015 using visual studio 2015

Add Event Receiver:
Now we will see how we can add an event receiver to the feature. Right click on the Feature name and then click on Add Event Receiver like below:

develop custom feature sharepoint 2015 using visual studio 2015
develop custom feature sharepoint 2015 using visual studio 2015

This will add a .cs file where we can write our code. By default, it put add various event handlers with the commented line.

Then uncomment the FeatureActivated and FeatureDeactivating event handlers and write the below code.

Now write the below code like below:

// Uncomment the method below to handle the event raised after a feature has been activated.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = new SPSite(“http://mypc:29024/sites/SPTraining/”).OpenWeb();
try
{
SPList targetList = web.Lists[“MyListFromFeatureDemo”];
}
catch (ArgumentException)
{
Guid listId = web.Lists.Add(“MyListFromFeatureDemo”,
“List created through feature”, SPListTemplateType.Contacts);
SPList list = web.Lists[listId];
list.OnQuickLaunch = true;
list.Update();
}
}

// Uncomment the method below to handle the event raised before a feature is deactivated.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWeb web = new SPSite(“http://mypc:29024/sites/SPTraining/”).OpenWeb();
try
{
SPList list = web.Lists[“MyListFromFeatureDemo”];
list.Delete();
}
catch (ArgumentException)
{

}
}

Once you write the code, right click on the solution and then click on Deploy like below:

develop custom feature sharepoint 2015 using visual studio 2015
develop custom feature SharePoint 2015 using visual studio 2015

Once the deployment successfully over. Open the particular site where you have deployed the feature, you can see the feature like below:

SharePoint 2016 Develop and deploy custom feature using Visual Studio 2015
SharePoint 2016 Develop and deploy custom feature using Visual Studio 2015

In the site content also if you will check it will create a list as the name we have given.

Develop and deploy custom feature using Visual Studio 2015
Develop and deploy custom feature using Visual Studio 2015

If you will deactivate the feature, the list will also be get deleted as we have written the code in FeatureDeactivating event.

Hope this will be helpful.


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 →