Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a custom aspx page in sharepoint sandboxed solution with the content as shown below?

<%@ Page Language="C#"  MasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderMain" runat="server">
    Hi Doug.
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</asp:Content>

and the code behind:

namespace Microsoft.SharePoint.WebPartPages
{
    public class SamplePage: System.Web.UI.Page
    {
        protected global::System.Web.UI.WebControls.Label Label1;

        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = "I work just fine.";
        }
    }
}

Here I have changed the namespace to Microsoft.SharePoint.WebPartPages because sandbox solution is not accepting the project assembly "MyAssembly". I deployed the solution. but i cannot see the code behind SamplePage is not executing. An anyone please help me?

Note: I also perform the below action in javascript using which i want to step into the code behind samplepage.cs. But it never calls it.

 $ajax
    ({ type: 'POST', dataType: "html", url: "http://server09/Style%20Library/sample/SamplePage .aspx",

        beforeSend: function (xhr) {
            xhr.setRequestHeader('command', 'ListsNames');
        },
        success: function (content) {
            alert('sharmila');
        }
    });
share|improve this question
    
Changing the namespace doesn't put it in the same assembly (dll file). You shouldn't be using any Microsoft namespace for your projects. –  lgaud Oct 22 '12 at 1:23
add comment

3 Answers

up vote 4 down vote accepted

You have to be a bit tricky to include codebehind for your custom pages in sandbox solutions. Here's a little tutorial on how to do it:

http://www.wictorwilen.se/Post/Custom-application-pages-in-the-SharePoint-2010-Sandbox.aspx

For example, here's my custom ASPX page:

<%@ Page language="C#" 
  MasterPageFile="~masterurl/default.master"  
  Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c"%>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" 
  Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

  <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <WebPartPages:SPUserCodeWebPart
        runat="server" 
        Description="Admin" 
        Title="Admin" 
        AssemblyFullName="$SharePoint.Project.AssemblyFullName$" 
        SolutionId="b72e73fe-2ee4-4b0d-9849-d53a3d911fbf" 
        ID="adminwp"     TypeFullName="Example.Settings.Settings" >
    </WebPartPages:SPUserCodeWebPart>
</asp:Content>

And here's how I add controls to it through my .cs codebehind file.

namespace Example.Settings
{
    [ToolboxItemAttribute(false)]
    public class Settings : WebPart
    {

        protected override void CreateChildControls()
        {
        Controls.Add(new LiteralControl("<img WIDTH=320px src='../ElementsModule/Logo.jpg'></img></br>"));
        }
    }
}

make sure you specify the Solution GUID and the path the codebehind.

share|improve this answer
    
Thanks for the link :) Bu in the above link i dont find any hint about code behind aspx page. Its is about deploying custom aspx page to sharepoint which i have done successfully. I need to know how to add code behind page for the custom aspx page and how to call it. –  sharmila Oct 19 '12 at 10:36
1  
I have edited my answer to include a simple example, hope it's clear now. In general you have to be aware that code blocks in sandbox solutions are heavily restricted. –  Rafał Saltarski Oct 19 '12 at 10:48
    
You can't have code behind an aspx page in a sandboxed solutions, so the workaround that Wictor is implementing it the blog post at the link is to include a web part in the page which can the have the code you want –  Per Jakobsen Oct 19 '12 at 11:32
    
@Rafal,Thanks for your reply.. I have followed the method you have suggested. I have added sandboxed visual webpart. when added it inherits from "System.Web.UI.WebControls.WebParts.WebPart". and I have added SPUserCodeWebPart. during ajax call the request comes to this visual webpart class file. But i need to request to seperate class. which is not happening. any ideas? I mean to say using your code can we make request to any other class files other then "Settings" class. ex: "Settings1" class and having the SPUserCodeWebPart for it ? what will be the SPUserCodeWebPart code for it? –  sharmila Oct 22 '12 at 9:22
add comment

I think something needs to be explained here: having codebehind for aspx pages in sandbox solutions is, as far as I know, impossible. System.Web.UI.Page is not available in sandbox. In fact, when you are referring to it in your code, you're really getting a mockup object. Try using for example - Page.ClientScript.RegisterStartupScript - code will compile, won't throw any exception, but also you won't get any result.

Second issue in your code is that in .aspx file you have line Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>

but in your code behind file - you're declaring new class SamplePage which has no relation to above aspx.

Now, as for a workaround, which Rafal pointed out - in your solution you should include standard SharePoint page (as you already have), but instead of any custom labels or such - you should create sandbobx webpart (this webpart shouldn't have any ascx markup though all child controls should be created from code behind), add it in WebPartPages:SPUserCodeWebPart markup and run all managed code from within this webpart.

Please also remember, that referencing to this.Page may not work as expected (for example, Page.FindControl("Label1") might return null), that's why all required controls should be created inside WebPart.

share|improve this answer
    
My main requirement is to pass data from client to server side using ajax post call. Now I have custom aspx page with the SPUserCodeWebPart and i need to call the aspx page using ajax method. which should step into the code behind file.I could easily do it in farm solution but i am getting no wherr while trying the same in sandboxed solution. Can you please tell me how can i accomplish this in sandboxed solution? –  sharmila Oct 19 '12 at 12:15
add comment

My main requirement is to pass data from client to server side using ajax post call. Now I have custom aspx page with the SPUserCodeWebPart and i need to call the aspx page using ajax method. which should step into the code behind file.I could easily do it in farm solution but i am getting no wherr while trying the same in sandboxed solution. Can you please tell me how can i accomplish this in sandboxed solution?

If you already have your page with SPUserCodeWebPart, then this webpart code behind should trigger normally after your ajax call. You should be able to get your value from Request property. If nothing happens, then maybe showing more code would help. If you have a problem with vs debugger, maybe you're already aware of this, but just in case, please attach to SPUCWorkerProcess instead of w3wp

share|improve this answer
    
Can you please post code that you have right now and add comment what exact behavior you expect? –  Marek Kembrowski Oct 22 '12 at 9:20
    
I have added sandboxed visual webpart. when added it inherits from "System.Web.UI.WebControls.WebParts.WebPart". and I have added SPUserCodeWebPart. during ajax call the request comes to this visual webpart class file. But i need to request to seperate class. which is not happening. any ideas? using Rafals code can we make request to any other class files other then "Settings" class. ex: "Settings1" class and having the SPUserCodeWebPart for it ? what will be the SPUserCodeWebPart code for it? –  sharmila Oct 22 '12 at 9:23
    
@ Marek, since the code part is bit large..i cannot send it using comment..any alternative way? –  sharmila Oct 22 '12 at 9:25
    
I don't need whole code, just a proof of concept, cause i'm not 100% sure if i understand what exactly you want to achieve. If by 'other class' you mean embedded ascx file, than i think, it's not possible. –  Marek Kembrowski Oct 22 '12 at 9:30
    
let me explain you. if we check Rafals code the SPUserCodeWebPart targets to the webpart.i e. settings webpart. if i create the settings.aspx file and use ajax post call method $({ type: 'POST', dataType: "html", url: "servername/Style%20Library/Module/Settings.aspx"; then the request sent to the code part of visual webpart.i.e. public class Settings : WebPart{ protected void Page_Load(object sender, EventArgs e) {//write code here}} But if i create my own page like webservice.aspx also adding the SPUserCodeWebPart targetting to webservice class --see the next comment –  sharmila Oct 22 '12 at 9:35
show 3 more comments

Your Answer

 
discard

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.