Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint 2013 and ASP.NET WebApi

I would like to use ASP.NET WebApi inside a SharePoint 2013 farm solution.

I know it is not supported out-of-the-box, but I found SignalR can be run by means of a simple HttpModule, so I was wondering whether a similar appoach could be used.

Thanks in advance, Rich

UPDATE June 2013

Made it working by reworking the HTTP Module shown in the mentioned post:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "kms2013/api/{controller}/{action}",
                defaults: new { }
            );
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
config.Services.Replace(typeof(IAssembliesResolver), new SPAssemblyResolver());

HostingEnvironment.RegisterVirtualPathProvider(new WebAPIVirtualPathProvider());

SPAssemblyResolver

public class SPAssemblyResolver : IAssembliesResolver
{
    public ICollection<Assembly> GetAssemblies()
    {
        return new List<Assembly> { Assembly.GetExecutingAssembly() };
    }
}

WebAPIVirtualPathProvider

Same as SignalRVirtualPathProvider shown in the post.

NEW ISSUE

The only problem with this approach is ScriptResource.axd and WebResource.axd now break when SP references them in a page. I tried to add an ignore route:

RouteTable.Routes.Add(new Route("{resource}.axd", new StopRoutingHandler()));

But I keep getting 401 Unauthorized. Removing the module clears the error, so I guess we're still lacking one last piece of the puzzle.

like image 854
user2363245 Avatar asked May 08 '13 18:05

user2363245


People also ask

Can SharePoint connect to API?

SharePoint offers a rich set of APIs that can be consumed in various ways. This article outlines what options you have, how they work and what their advantages and disadvantages are.

Does SharePoint support REST API?

SharePoint includes a Representational State Transfer (REST) service that is comparable to the existing SharePoint client object models. Now, developers can interact remotely with SharePoint data by using any technology that supports REST web requests.


1 Answers

Yes, the same approach should work.

Create a web api project and check the app init part - then follow my blog post you referenced already.

Btw: Ask at sharepoint.stackexchange.com - maybe someone has a better solution.

like image 188
Max Melcher Avatar answered Oct 11 '22 21:10

Max Melcher