Follow in Twitter & Facebook


Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools


Sunday, July 30, 2017

Create a web api and deploy in Microsoft Azure for SharePoint online using visual studio 2015

We can create a web api using visual studio 2015 and we can deploy in Microsoft Azure for SharePoint online site using visual studio 2015.

This article I have already published in EnjoySharePoint.com site also.

Follow below steps to Create Web Api for SharePoint Online:

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:


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:


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 credentials.


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:


Now the solution looks like below:


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


Then choose "Web API 2 Controller - Empty" and click on Add like below:


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


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:


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

Before proceding 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...


Then click on "Import" options like below:


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


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:


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


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.


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

And it will return like below:


Hope this will be helpful.




0 on: "Create a web api and deploy in Microsoft Azure for SharePoint online using visual studio 2015"