SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Why not create a custom webpart which has your javascript code and can just POST data to normal codebehind? – Jasper Sep 13 '12 at 6:43
up vote 1 down vote accepted

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();
share|improve this answer
You cannot create custom webservices in a sandboxed environment. – Jasper Sep 13 '12 at 6:42
Good call @jasper, removed it from the answer. Thanks – eirikb Sep 13 '12 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. – Anatoly Mironov Sep 13 '12 at 10:25
Updated, @anatoly-mironov. Thanks :) – eirikb Sep 13 '12 at 10:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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