Step by step create a web api and deploy in Microsoft Azure for SharePoint online using visual studio 2015

create web api in sharepoint online visual studio 2015
Check out Best Alternative to InfoPath


Here we will discuss how we can create a web api and how we can deploy in Microsoft Azure for SharePoint online site using visual studio 2015. Later we will discuss how we can use it inside a visual studio 2015 workflow.


We were working in a visual studio 2015 workflow for SharePoint online (Download PDF for FREE) site. In that workflow there were some complex logic was there. So we decided, instead of checking everything inside the workflow, we will develop some web apis and we will call the web api using httpsend activitity inside our workflow.

Follow below steps to Create Web Api for SharePoint:

Open visual studio 2015, File -> New Project…

In the New Project dialog box, expan Installed -> Templates -> Visual C# -> Web and then choose ASP.NET Web Application like below. Give a name for the project as well as make sure .NET Framework 4.5.2 is selected like below:


web api azure
web api azure

Then in the New ASP.NET Project dialog box, choose Empty project template. Then select the “Web Api” check box. And in the hosting section, check the “Host in the cloud” Microsoft Azure check box And select the Web Api option from drop down list like below:

azure sharepoint api
azure sharepoint api

Then in the Configure Microsoft Azure Web App dialog box, enter the Microsoft Azure details. First click on Add an account and then enter the Azure cresdentials.



Then give a Azure web app name. Then you can choose to create a new App service plan or create a new service plan. Similarly choose an existing Resource group or create a new group. Then choose the Region. The whole dialog box looks like below:

azure sharepoint development environment
azure sharepoint development environment

Now the solution looks like below:

azure web api app visual studio 2015
azure web api app visual studio 2015

Then we need to add controller class. Right click on the Controller folder Add -> Controller like below:

azure web api apps tools for visual studio 2015
azure web api apps tools for visual studio 2015

Then choose “Web API 2 Controller – Empty” and click on Add like below:

azure web api visual studio 2015 sharepoint
azure web api visual studio 2015 sharepoint


Then you can give a name for the Controller or you can use existing name (DefaultController) for the Controller.

azure web api visual studio 2015 sharepoint online
azure web api visual studio 2015 sharepoint online

Here when accessing the URL, we have to use “Default” for the Controller.

The DefaultController class will look like below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MyDemoWebAPI.Controllers
{
public class DefaultController : ApiController
{
}
}

Here in this class we are going to add one simple method which will accept one parameter and return something.

I have added one action known as MyAction like below:

[HttpGet]
public string MyAction(string testParameter)
{
return testParameter;
}

The controller class looks like below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MyDemoWebAPI.Controllers
{
public class DefaultController : ApiController
{
[HttpGet]
public string MyAction(string testParameter)
{
return testParameter;
}
}
}

Then we need to add the MapHttpRoute attribute inside the “WebApiConfig” which is presented inside the App_Start folder.

config.Routes.MapHttpRoute(
name: “MyAction”,

routeTemplate: “api/{controller}/{action}/{testParameter}”,

defaults: new { controller = “DefaultController”, action = “MyAction”, testParameter = “”}

);

Here name:”MyAction” — Name of the Action that we have added in the DemoController class.
routeTemplate: “api/{controller}/{action}/{testParameter}”,

While accessing the url, we have to pass our controller name in the place of {controller}, our action name in place of {action} and our input parameter name in place of {testParameter}.

defaults: new { controller = “DefaultController”, action = “MyAction”, testParameter = “”}

And in the above line we are passing the default values.

It looks like below:

calling web api from sharepoint
calling web api from sharepoint

Now it is time to deploy the web api. Here we will use the Import option to publish to Microsoft Azure.

Before proceeding further we need to make sure we have created an Azure web app and also we have downloaded the published file.

Right click on the Project and then click on Publish…

deploy web api Azure sharepoint online
deploy web api Azure sharepoint online

Then click on “Import” options like below:

deploy web api Azure sharepoint
deploy web api Azure sharepoint

Now select the PublishSettings file which you have downloaded by following the above article.

deploy web api Microsoft Azure
deploy web api Microsoft Azure

Then make sure to give https in the destination url and also you can click on Validate Connection button to validate the connection like below:

create web api in sharepoint 2016 visual studio 2015
create web api in sharepoint 2016 visual studio 2015

Then in the next screen click on Next button like below:

create web api in sharepoint 2013 visual studio 2015
create web api in sharepoint 2013 visual studio 2015

Then in the next window click on preview button to see the preview or else you can directly click on the Publish button to publish to Azure.

create web api in sharepoint online visual studio 2015
create web api in sharepoint online visual studio 2015

Now once the deployment successful, we can access the web api url which will return the value. We can access like below:

https://mysharepointwebapi.azurewebsites.net/api/Default/MyAction?testParameter=Bhawana

Here we are passing the parameter like ?testParameter=Bhawana

Read some sharepoint online tutorials:

And it will return like below:

create web api in sharepoint online
create web api in sharepoint online

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

View all posts by Bhawana Rathore →