How to Create a Basic WebApi which can be used by SharePoint 2013


Sathish Nadarajan
SharePoint MVP
Published On :   02 Jul 2015
Visit Count
Today :  3    Total :   13074



In this article, let us see how to create a Basic WebApi which can be used by SharePoint 2013. Now, in most of the development environment, the customers were not happy to deploy the WSPs, either it can be a Farm Solution as well as SandBox Solution. Then the solution would be going to Client Object Model and using the Javascript, achieve our functionality.

But in that case also, writing the more complex logics in the JavaScript will also lead to some threats. End user can view the source or inject some other malicious code. To avoid that, we can create some WebApi’s which will be hosted on some other Server. The javascripts will make a call to the API.

The WebAPI which I am referring here is something different from our WCF Service. Let us see how to create a Basic WebAPI step by step.

1. Open the Visual Studio 2013.

clip_image002

2. On the new Project, select the template as shown above.

3. On the Template screen, select the options as shown below.

clip_image003

4. The solution will be as shown below.

clip_image005

5. Add a Controller Class

clip_image007

6. Select the Web API 2 Controller Empty.

clip_image009

7. Give the Name as DemoController.

8. Create a Sample Method as shown below.

 [HttpGet]
         public string MyAction(string testParameter)
         {
 
 
             //Do the Action Here
             return “My Test”;
 
         }
 

10. By default the WebApiConfig file will have the below code.

 public static void Register(HttpConfiguration config)
         {
             // Web API configuration and services
 
             // Web API routes
             config.MapHttpAttributeRoutes();
 
             config.Routes.MapHttpRoute(
                 name: "DefaultApi",
                 routeTemplate: "api/{controller}/{id}",
                 defaults: new { id = RouteParameter.Optional }
             );
 
              
         }
 

11. Along with that add the below Routes as well. Hence the code will looks like

 public static void Register(HttpConfiguration config)
         {
             // Web API configuration and services
 
             // Web API routes
             config.MapHttpAttributeRoutes();
 
             config.Routes.MapHttpRoute(
                 name: "DefaultApi",
                 routeTemplate: "api/{controller}/{id}",
                 defaults: new { id = RouteParameter.Optional }
             );
 
             config.Routes.MapHttpRoute(
                 name: "SearchApi",
                 routeTemplate: "api/{controller}/{action}/{testParameter}",
                 defaults: new { action = "Get" }
             );
         }
 
 

12. Now, our API is ready to execute. Click F5 from the Visual Studio. It will be loaded on the local virtual IIS.

13. Now, on the browser, type the URL like, http://<<localhost>>/api/demo/MyAction/test

14. Our Controller method will be called and we will get back a String as JSON Object.

15. Even, if we want to return as a Parsed JSON Object, we can use the below code.

 [HttpGet]
         public IHttpActionResult MyAction (string docTopic,string termID)
         {
              
                     return Json(“My Test”);
                     
  
         }
 

Download the Sample Config Files Here

Happy Coding,

Sathish Nadarajan.

Categories