SharePoint Pals
 | Sign In
How to Create a Basic WebApi which can be used by SharePoint 2013
by Sathish Nadarajan 2 Jul 2015
Author
SharePoint MVP
Visits   
Today  :   7     Total  :    12114

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.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation