in

SharePoint University

Clean slate. Nothing but SharePoint.
Go, SharePoint!

how to integrate ASP.NET codes inside SharePoint

Last post 07-08-2008 3:42 PM by menerva. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 11-01-2007 5:48 PM

    • allan
    • Not Ranked
    • Joined on 11-01-2007

    how to integrate ASP.NET codes inside SharePoint

    i have some shopping cart codes which run good in asp.net 2.0. i want to reuse these codes inside SharePoint but i don't know how? when you create a new page you can't write codes to it like you do with <script runat=server> or create a codefile with name of xxx.aspx.cs. i have many useful codes, if i can reuse them in SharePoint that will save me lot of time. i know it's big topic so really appreciate the advice. thanks in advance.

  • 11-01-2007 6:44 PM In reply to

    • mongeau
    • Top 50 Contributor
    • Joined on 08-07-2007
    • Fargo ND

    Re: how to integrate ASP.NET codes inside SharePoint

     You definitely won't be able to do code-behind in SharePoint. However, here is a workaround to get inline code to work:

    - "An error occurred during the processing of xxx.aspx. Code blocks are not allowed in this file."
    - "This page has encountered a critical error. contact your system administrator if this problem persist."

    Example inline code
    ==================================
    Open up SharePoint Designer and the masterpage you want to edit. Switch to the code view and anywhere from the first <head> tag down you can add some inline c# code:

    <script runat="server">
    string myVar = "hello world!";
    void Page_Load(object sender, System.EventArgs e)
    {
    Response.Write(myVar);
    SPSite siteCollection = new SPSite("http://[sharepoint]");
    SPWeb site = siteCollection.OpenWeb("/[subsite]/");

    SPList list = site.Lists["Announcements"];
    SPListItemCollection items = list.Items;

    foreach(SPListItem item in items)
    {
    Response.Write(item["Title"].ToString());
    Response.Write("<br/>");
    }

    }
    </script>

    This shows how you can use the SharePoint object model in your inline code. If you saved the master page now and opened the site in your browser you would get an error about not being able to run code blocks (listed above). There’s an extra line you need to put into your web.config file first to allow this inline c# to execute.

    Modify the web.config file
    ==================================
    1. Open the web.config file and make the following change:

    <PageParserPaths>
        <PageParserPath VirtualPath="/_catalogs/masterpage/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true" />
    </PageParserPaths>

    The PageParserPaths xml tags will be there already you just need to add the line in between.

    2. Save web.config .
    3. Start -> Run -> iisreset /noforce , click OK.

    Note: I would like to point out that making this change is possibly not good practice. The fact that CompilationMode being set to "Always" points out that there may be a performance hit and caching won’t exactly work well.

    Sources
    ==================================
    http://weblog.vb-tech.com/nick/archive/2006/08/03/1710.aspx

    http://www.wssdemo.com/blog/Lists/Posts/Post.aspx?List=d5813c18%2D934f%2D4fd6%2D9068%2D5cdd59ce56ba&ID=232

     

  • 11-15-2007 1:30 AM In reply to

    • Navid
    • Not Ranked
    • Joined on 08-19-2007

    Re: how to integrate ASP.NET codes inside SharePoint

    I thinks this only works when you DO NOT use any DataView in your page design and only use other ASP controls.

    Am I right ?

  • 07-08-2008 3:42 PM In reply to

    • menerva
    • Not Ranked
    • Joined on 05-22-2008
    • Salé

    Re: how to integrate ASP.NET codes inside SharePoint

    really helpful article, but i've a question

     how to insert a script c# in a simple webpage (not the masterpage) ?

    and how can i call the function or method in the script?

    and if possible, you can bring us a sample code who contain all steps to integrate script (in preference c#) in a simple page of sharepoint ?

     

    and thanx for all

    hello there, im new here, i hope i can be a welcome there with all community and you're welcome 2 :)
Page 1 of 1 (4 items)

Need SharePoint Training? Attend a SharePoint Bootcamp!
Forum content (c) original posters. Everything else (c) 2008 SharePoint Experts, Inc.