Can we Use Web Methods in Provider hosted app

Category: apps for sharepoint 2013


TARUN00197 on Wed, 13 Sep 2017 04:55:30

I am developing a provider hosted app (Web Forms) for SharePoint Online hosted in Azure.

I am trying to implement the Web Method in the provider hosted app, but cant proceed further because all the web Methods are static. How can i get the Context in the Web Method.

If I create a Web Method without using the Context (normal function with static json object) its working fine.

Can we use Web Methods in provider Hosted Apps.? If yes, can any one have reference example or sample code

Anyone suggest a way to move further or other approach to meet my requirement.



Lee__li on Thu, 14 Sep 2017 07:07:27


You may try this:

aspx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PHWebMethodWeb.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btn').click(function () {                
                    url: 'default.aspx/GetData',
                    type: "POST",
                    dataType: "json",                    
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
    <form id="form1" runat="server">
            <input type="button" name="btn1" id="btn" value="ClickMe" />


public partial class Default : System.Web.UI.Page
        protected void Page_PreInit(object sender, EventArgs e)
            Uri redirectUrl;
            switch (SharePointContextProvider.CheckRedirectionStatus(Context, out redirectUrl))
                case RedirectionStatus.Ok:
                case RedirectionStatus.ShouldRedirect:
                    Response.Redirect(redirectUrl.AbsoluteUri, endResponse: true);
                case RedirectionStatus.CanNotRedirect:
                    Response.Write("An error occurred while processing your request.");

        protected static SharePointContext _Context = null;

        public static string GetData()
            if (_Context == null)
                return "Context not initilized";
                using (var clientContext = _Context.CreateUserClientContextForSPHost())
                    clientContext.Load(clientContext.Web, web => web.Title);
                    return clientContext.Web.Title;

        protected void Page_Load(object sender, EventArgs e)
            // The following code gets the client context and Title property by using TokenHelper.
            // To access other properties, the app may need to request permissions on the host web.
            var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
            _Context = spContext;            


Best Regards,


Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact