Question

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.

TARUN


Sponsored



Replies

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


Hi,

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btn').click(function () {                
                jQuery.ajax({
                    url: 'default.aspx/GetData',
                    type: "POST",
                    dataType: "json",                    
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        alert(JSON.stringify(data));
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="button" name="btn1" id="btn" value="ClickMe" />
        </div>
    </form>
</body>
</html> 

Back-end:

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:
                    return;
                case RedirectionStatus.ShouldRedirect:
                    Response.Redirect(redirectUrl.AbsoluteUri, endResponse: true);
                    break;
                case RedirectionStatus.CanNotRedirect:
                    Response.Write("An error occurred while processing your request.");
                    Response.End();
                    break;
            }
        }

        protected static SharePointContext _Context = null;

        [WebMethod()]
        public static string GetData()
        {
            if (_Context == null)
            {
                return "Context not initilized";
            }
            else
            {
                using (var clientContext = _Context.CreateUserClientContextForSPHost())
                {
                    clientContext.Load(clientContext.Web, web => web.Title);
                    clientContext.ExecuteQuery();
                    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,

Lee


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