I have Javascript (Client Object Model) running in a Content Editor web part that controls other web parts on the page. The JS COM is limited in what it can do, so I want to be able to call managed code (c#) at times to fill in the gaps. But I can't figure out how to pass control from js to c# (and back).

This is SharePoint 2010 sandboxed shared/hosted environment (not O365 SharePoint Online). So I believe the managed code must be in a WSP web part on the same page. The site has anonymous access.

Can (how can) the JS call the c# code?

  • 1
    Why not create a custom webpart which has your javascript code and can just POST data to normal codebehind?
    – Jasper
    Sep 13, 2012 at 6:43

1 Answer 1


One simple (and hackish) solution is to simply look for normal POST request parameters in your Web Part and do some logic based on this.
Example Web Part code:

var incomingMessage = Context.Request["message"];
var message = string.Format("Hello {0}", incomingMessage);
Controls.Add(new Label { Text = message, CssClass = "hack" });

Example call/read with jQuery:

$.post(window.location.href, {message: 'world!'}, function(data) {
    var $data = $(data);
    var message = $data.find('.hack').text();
  • You cannot create custom webservices in a sandboxed environment.
    – Jasper
    Sep 13, 2012 at 6:42
  • 1
    Good call @jasper, removed it from the answer. Thanks
    – eirikb
    Sep 13, 2012 at 6:46
  • Nice hack! One improvement: '.' (dot as url) should be replaced with window.location.href so that the same page is requested, not the url "folder" (SitePages, Pages) in some web templates. Sep 13, 2012 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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