Page class SP 2010 Sandbox Solution

We were trying some simple code in SharePoint 2010 and with a Sandboxed solution, the same code worked fine with Farm solution but didnt work with Sandbox solution. After doing some debugging we found that from the following code the following lines never worked:

           Page.ClientScript.RegisterStartupScript(GetType(), "MyuniqueGUID", xml, true);

 ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString()  , "alert('hello baby');", true); 



Code that is not working in SANDBOX:

protected void Page_Load(object sender, EventArgs e)


            _nodeSelection.EnableViewState = false;

            _publish.Click += HandlePublishClick;

            // TODO: Do validation here and write out error if no values for the key fields.

            int itemID = int.Parse(HttpContext.Current.Request.QueryString["ItemID"]);

            Guid listID = new Guid(HttpContext.Current.Request.QueryString["ListId"]);

            _document = new Document(Convert.ToInt32(itemID), listID);

            _documentName.Text = _document.Name;

            string xml = string.Format(@"var __xml = ""<root><item id='n1' parent_id='0' state='open'><content><name>1 TITLE PAGE </name></content></item><item id='n2' parent_id='0' state='open'><content><name>TRIAL DESIGN</name></content></item><item id='n2-1' parent_id='n2' state='closed'><content><name>Trial Design</name></content></item></root>"";");

           Page.ClientScript.RegisterStartupScript(GetType(), "MyuniqueGUID", xml, true);


So after some debugging and talking to some folks internally @ Microsoft we found following two things:

1. The Page class is not available in a sandbox solution.

2. System.Web.UI.ClientScriptManager is not supported In the sandbox.

So then Paul Stubb provided a sample on how this can be worked around and Sowmyan was kind enough to share it with us, following is the sample code:

namespace ClientOMProject.ListOperationsVisualWebPart {

    using System.Web;

    using System.Text.RegularExpressions;

    using Microsoft.SharePoint.WebPartPages;

    using Microsoft.SharePoint.WebControls;

    using System.Web.Security;

    using Microsoft.SharePoint.Utilities;

    using System;

    using System.Collections;

    using System.Collections.Specialized;

    using Microsoft.SharePoint;

    using System.Web.UI;

    using System.Web.Profile;

    using System.Text;

    using System.Web.Caching;

    using System.Web.UI.WebControls;

    using System.Configuration;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.SessionState;

    using System.Web.UI.HtmlControls;



    public partial class ListOperationsVisualWebPart {


        public static implicit operator global::System.Web.UI.TemplateControl(ListOperationsVisualWebPart target)


            return target == null ? null : target.TemplateControl;




        private void @__BuildControlTree(global::ClientOMProject.ListOperationsVisualWebPart.ListOperationsVisualWebPart @__ctrl) {

            System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl));

            @__parser.AddParsedSubObject(new System.Web.UI.LiteralControl(@"


<script src=""/SiteAssets/ListOperations.js"" type=""text/javascript""></script>


<script language=""javascript"" type=""text/javascript"">

// <![CDATA[


    function CreateButton_onclick() {




    function AddButton_onclick() {




    function ReadButton_onclick() {




    function UpdateButton_onclick() {




    function DeleteButton_onclick() {




    // ]]>




<p><input id=""CreateButton"" type=""button"" value=""Create List""

        style=""width: 150px"" onclick=""CreateButton_onclick()"" /></p>

<p><input id=""AddButton"" type=""button"" value=""Add List Item""

        style=""width: 150px"" onclick=""AddButton_onclick()"" /></p>

<p><input id=""ReadButton"" type=""button"" value=""Read List Item""

        style=""width: 150px"" onclick=""return ReadButton_onclick()"" /></p>

<p><input id=""UpdateButton"" type=""button"" value=""Update List Item""

        style=""width: 150px"" onclick=""return UpdateButton_onclick()"" /></p>

<p><input id=""DeleteButton"" type=""button"" value=""Delete List Item""

        style=""width: 150px"" onclick=""return DeleteButton_onclick()"" /></p>





        private void InitializeControl() {


            this.Load += new global::System.EventHandler(this.Page_Load);


       //CALL THE InitializeControl() above method in the onInit() of the webpart code


        protected virtual object Eval(string expression) {

            return global::System.Web.UI.DataBinder.Eval(this.Page.GetDataItem(), expression);




        protected virtual string Eval(string expression, string format) {

            return global::System.Web.UI.DataBinder.Eval(this.Page.GetDataItem(), expression, format);





Hope that helps someone save some time. 



Comments (3)
  1. Anurag Jain says:

    Could you please send us the working example of this approach?

  2. pavan says:

    hi sorry if i am missing something over here but i did not get the alternative to where exactly are u registering the startupscript

  3. Rujal Jariwala says:

    Would you please tell what did you write at the place of  "Page.ClientScript.RegisterStartupScript(GetType(), "MyuniqueGUID", xml, true);" in your back end codding ?

Comments are closed.

Skip to main content