0

i am developing Sharepoint online on-premise app. Almost all my logic and SP objects manipulation is in the c# codebehind(not JS), but now i need to call my already implemented method from js. I created .asmx and called it via jquery AJAX. The service is hit with all needed parameters, but i cannot create the ClientContext object so i can actually run the method i need. What should i do to have the context created?

JS

$("#approveYesBtn").click(function () {
    var taskId = $("#<%= selectedTaskHf.ClientID%>").val();
    var note = $("#<%= noteApproveTb.ClientID%>").val();
    var parameters = '{"taskIdStr":"' + taskId + '", "note":"' + note + '"}';
    $.ajax({
        url: "/Services/DocumentApproval.asmx/ApproveTask",
        data: parameters,
        type: 'POST',
        contentType: "application/json",                        
        processData: false,
        success: function () {
                        documentApproveModal.close();
                        alert('task approved');
                    },
                    error: function (jxhr, msg, err) {
                        documentApproveModal.close();
                        alert('error');
                    }
    });
});

.asmx

[WebMethod]
public void ApproveTask(string taskIdStr, string note)
{
    //always null
    var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

    using (var context = spContext.CreateUserClientContextForSPAppWeb())
    {      
        //some code
    }
}
  • Do you need to generate the client context for the user who called the service or can you afford to create the clientcontext for a service account? – Unnie Aug 24 '15 at 8:26
  • I need the context to be app context(app web, not host web) in order to access lists that are part of the app. I am not sure about your question, i guess i need it for the user but not sure what the difference would be. The code creating context i provided is how i usually create the context anywhere in the code, when not in .asmx service. – Martin Aug 24 '15 at 8:54
  • Okay what i wanted to know is should the code run under the credentials of the logged in user in SharePoint who makes a call to webservice or can it run under the credentials of any service account ? – Unnie Aug 24 '15 at 8:56
  • I think it should run under the credentials of the app itself or credentials of the user using the app. – Martin Aug 24 '15 at 9:04
  • As long as i know i should just add some parameters to the header of the ajax request call in order to make it work, but i am not sure about that. – Martin Aug 24 '15 at 9:09
0

Try using the below code. Pass the site url as a parameter to your JSON call

Uri uri = new Uri(Url);
 string appOnlyAccessToken = TokenHelper.GetS2SAccessTokenWithWindowsIdentity(uri, null);
ClientContext ctx = TokenHelper.GetClientContextWithAccessToken(uri.ToString(), appOnlyAccessToken);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.