Andrew Connell [MVP MOSS]
1350 Posts |  39 Articles |  3385 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
PDCBling
SharePoint Quick Links
Article Categories
Archives
Post Categories

View Andrew Connell's profile on LinkedIn

Add to Technorati Favorites

[11/12/2007] This article & the associated code download has been updated to attach the button's click event handler to be set in the code behind rather than in the ASPX page. Doing it in the ASPX page would constitute inline script which should never be used in a site page. My bad! [/]

One of the more common myths among both those who are just getting into and those seasoned in SharePoint development topics is that it is not possible to leverage the ASP.NET technique of leveraging code behind files in custom master pages or site pages. I call this a myth because that is just what it is... completely false. In a post on my blog, I touched on how this can be done, but I didn't provide any details. This article expands on this statement to explain not only how it can be done, but also to explain what is different between the native ASP.NET 2.0 and SharePoint development experience.

Overview of the SharePoint Site Architecture

SharePoint sites are structured in a very different way than native ASP.NET 2.0 sites are. The pages in ASP.NET 2.0 sites primarily live within folders on the file system. It is fairly easy to take a URL and map it to a file on the file system for the most part. However SharePoint sites are very different. The primary difference is that most files in SharePoint sites live exclusively in the database.

This virtualized file system facilitates vast numbers of sites within a farm as it is not uncommon for a enterprise SharePoint farm to host thousands if not tens or hundreds of thousands of sites. The files, pages as well as images and other files, that live in the database are called instances. Initially when a site is created, many of these instances are simply pointers to files on the file system, or templates. What makes SharePoint different from native ASP.NET 2.0 sites is that SharePoint promotes (and arguably encourages) customization of the pages on a site-by-site basis. In order to support this, customizations are saved in the SharePoint content database (a Microsoft SQL Server database). This allows sites to leverage underlying file templates until they are customized.

In native ASP.NET 2.0 sites, adding new pages involves dropping a new ASPX file into the desired folder in the site. In SharePoint, while it is possible to drop files within the webroot of the SharePoint site, it is not recommended. Rather, developers should create a template (just an ASP.NET 2.0 file) and then provision an instance of the file into the SharePoint site that points to the file on the file system. In both environments, the assembly containing the compiled code behind file of the ASP.NET 2.0 page is deployed to either the site's \BIN directory or the server's GAC.

For a deeper and more comprehensive explanation of the SharePoint architecture and how it interacts with ASP.NET 2.0, refer to Ted Pattison's & Dan Larson's book Inside Microsoft Windows SharePoint Services v3 book by MSPRESS... specifically chapter 2.

Creating Code Behind Files with Visual Studio 2005 in ASP.NET 2.0 Pages

Before jumping in to see how to create code behind files for ASP.NET 2.0 pages used in SharePoint sites, it makes sense to take a step back and review how it's done in native ASP.NET 2.0 pages within Visual Studio 2005. Visual Studio makes it real easy to add and leverage code behind files in ASP.NET 2.0 pages. A developer simply has to right-click the design surface of an ASPX / ASCX file or right-click the file in the Solution Explorer tool window and select View Code. This automatically creates (if it wasn't already created) a file with the name [Filename.aspx].cs with a class of the same name of the page that inherits from the System.Web.UI.Page class. It also adds a pointer to the class in the code behind file to the Inherits attribute of the Page directive: <% @Page Inherits="" %>.

In addition, when new Web controls are dropped on the design surface, Visual Studio adds a protected field for each Web control to the code behind.

Creating ASP.NET 2.0 Pages with Code Behind Files in SharePoint v3 Sites

Before walking through the steps necessary to leverage code behind files in ASP.NET 2.0 pages within a SharePoint site, take a look at it from the perspective of a native ASP.NET 2.0 site. In an ASP.NET 2.0 environment, Visual Studio knows how to do a lot of stuff for the developer. Unfortunately Visual Studio does not have the hooks into SharePoint to really understand what to do or how it works. This does not mean it isn't possible (if it did, the article would end here... duh!), it means that the process will be a little different. Specifically, creating and implementing code behind files with ASP.NET 2.0 pages used within a SharePoint site, the process will be completely manual. Not ideal, but at least we aren't left with a lack of functionality between the two platforms (ASP.NET 2.0 & SharePoint).

There are two main differences in creating custom ASPX pages for use in a SharePoint site vs. an ASP.NET 2.0 site is that the file you create (ASPX) is used as a template for the real file (instance) within the SharePoint site. After creating the ASPX page, an instance needs to be provisioned into the SharePoint site. This is done using a SharePoint Feature.

I like to create all my Features that contain some sort of compiled code using a Class Library project template. Then I setup the project to be signed to generate a strong named assembly and create the folders necessary for a Feature. Next, add a reference to System.Web. Nope... no reference to Microsoft.SharePoint is necessary! See... SharePoint development IS ASP.NET 2.0 development at the core! Last setup bit is to add a custom MSBuild targets file and stuff necessary to automatically build and package the Feature into a WSS Solution Package (*.WSP) using the technique I wrote about here. Here's what my project looks like now:

SharePointCodeBehindPages01
Figure 1 - SharePointCodeBehindPages Visual Studio 2005 project

Next, create the ASPX page that will be added to the SharePoint site and put it in the Feature folder: SharePointCodeBehindPage. With the file created, add whatever is necessary to implement the page the way you want it. The one thing to point out is you should use one of the four SharePoint master page tokens as the value in the MasterPageUrl attribute in the Page directive. Here's a simple ASPX file that has a TextBox and Button for the user to enter some text and see it rewritten back to a Literal control on the page. It is a real simple example, but the simplicity makes it easy to see how to do code behind files in SharePoint:

1
Figure 2 - PageTemplate.aspx ASP.NET 2.0 page

Three things to note here:

  1. This page implements two ContentPlaceHolders defined in SharePoint master pages. I recommend you implement SharePoint ContentPlaceHolders or, your own that you've defined in your own custom master page... but you must be sure your master page is being used in order to use this page.
  2. The meta:progid attribute on the Page directive is necessary if the page will ever be opened in SharePoint Designer. You should include this as site pages are supposed to support customization and thus, be opened in SharePoint Designer. If you don't want to allow pages to be opened and customized in SharePoint Designer, then you should do that through permissions, not at the page level.
  3. The event handler for the button is not in the ASPX file. Putting it here would be adding inline script to the page which should never be done. Inline script will work when the page is uncustomized, but once it is customized, SharePoint's safe mode parser will block the page from being executed.

The next thing to do is to create the code file that will act as the code behind file. Create a new C# file using the same name as the ASPX page except tack on the .cs suffix to the end. There is no requirement for the name, but to stay with the native ASP.NET 2.0 model and to make it easy to figure out which one it goes with, this is just a good idea.

Within this file you should set the class to inherit from the System.Web.UI.Page class. Here's what mine looks like for the ASPX page shown in Figure 2 above:

2
Figure 3 - PageTemplate.aspx.cs code behind file for PageTemplate.aspx page

With the ASPX page and code behind created, now the two need to be linked together. Recall how Visual Studio does it... it adds an Inherits attribute to the Page directive in the ASPX automatically. So... we can do it manually! So what do we put in this attribute? The full name to the type, or class, of our page object. This is also known as the 5-part name. The five parts are: [full type name], [assembly name], [version], [culture], [public key token]. Everyone has different ways of getting this. One of the easiest is to build the project and then open the file using Lutz's Reflector. This gives you the last four parts of the name, also called the assembly full name. Note that the type name needs to also include the namespace. For the code in Figure 3 above, here's what my Inherits attribute is (note: don't include the line breaks in the Inherits attribute as shown in the image... they are present just for readability):

SharePointCodeBehindPages04
Figure 4 - Inherits attribute in the Page directive

We now have the page template and code behind created as well as wired together. The next step is to create the Feature that will provision the file into a SharePoint site as an uncustomized instance pointing to the template. First, create the Feature definition file. Nothing special here... just a site-scoped Feature. Note that I've added the ASPX file in the Feature definition... a technique I explain in this blog post.

SharePointCodeBehindPages05
Figure 5 - feature.xml

Now... the element manifest file(s) within a Feature is where all the work happens. In this case I do two things. First I provision the file using the <Module> and <File> site elements. The code below creates a new file (CodeBehindDemo.aspx) within a SharePoint site that is based off a template (PageTemplate.aspx) and puts this new file in the Samples subfolder in the site. Next I create a new menu item in the Site Actions menu on the site giving me an easy way to navigate to this sample page (and also to see if the Feature has been activated):

SharePointCodeBehindPages06
Figure 6 - elements.xml

That's it! Now package everything up into a WSS solution package file. I'll put the *.WSP's manifest.xml and generated assembly in the root of the *.WSP file. The Feature is added as a subfolder. Here's what the manifest.xml file looks like.

SharePointCodeBehindPages07
Figure 7 - manifext.xml

Notice the markup in Figure 7 is adding a safe control entry to the web.config for the destination Web application. This is because SharePoint must be aware that the types within the assembly we are deploying are safe and ok to execute. At this point, here's what my Visual Studio project looks like:

SharePointCodeBehindPages08
Figure 8 - Visual Studio project

Now, add the solution to the SharePoint farm's solution store and deploy. Go to your nearest WSS site and activate the Feature as shown in Figure 9. Notice how after activating the Feature (#1) a new item shows up in the Site Actions menu (#2).

SharePointCodeBehindPages09
Figure 9 - Site Features page

Click on the item in the Site Actions menu. You'll see a page that looks like the first image below. If you enter some text and click the button, poof... it works!

SharePointCodeBehindPages10

SharePointCodeBehindPages11

I hope this helps explain how to do ASPX pages with code behind files in SharePoint v3 sites. The same process works for mater pages and page layouts within Publishing sites, you just need to make sure you inherit from the proper class in your code behind. Same deal with user controls except these should be deployed to a folder within the [..]\12\TEMPLATE\CONTROLTEMPLATES directory, specifically a project subfolder within that folder. Before I wrap this up, let me address two points that I can already expect I'll see:

  1. How do you debug your code behind? You'll have to manually attach the debugger to the W3WP.EXE process that's running the application pool for your site. Go here for an explanation with screenshots as well as a little trick.
  2. Can't I just use inline script (ie: <script runat="server">) in the ASPX file instead of using a code behind? Yes and no... but I'll highly recommend against it. The "yes" part of the answer is that you can do it and it will work... for now. The "no" part is that inline script is blocked by SharePoint when the page becomes customized (edited in SharePoint Designer). Why? Because more than just developers use SharePoint Designer and Microsoft didn't want some admin assistant who picked up a C# dummies book over the weekend to have the ability to add C# code to pages on the company intranet... and I'm sure your server admins wouldn't care for that either! So... since site pages can be customized, you should never use inline script.

Here's the source for the project I created in this article. Note that the only dependency is you need to have MakeCab.exe in the specified path that's listed in the BuildSharePointPackage.targets file. You can get this file from the Microsoft Cabinet SDK.

ยป Download SharePointCodeBehindPages Visual Studio project

By the way, I understand that there is some extra work you need to do in order to get this to work. The goal here is to show you that SharePoint supports code behind files. Now... we just need better development tools!

posted on Sunday, September 30, 2007 11:25 PM

Feedback

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/1/2007 5:23 AM Muhanad Omar
Gravatar Thanks for this article AC! You really know how to tackle a subject :)


Regards,
Muhanad

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/1/2007 5:23 AM Aalok Kapoor
Gravatar Excellent and very useful article. Thanks.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/1/2007 10:15 AM Dan
Gravatar Another awesome article, Andrew. I'm sure that it feels like you are screaming into the wind sometimes, but hang in there. The problem with MOSS 2007 is not the product, it is the lack of product documentation, best practices, and blog articles like these. People do not know where to start, and do not always have the luxury of going to training to learn it. So, they just dive in, learning as they go, and miss key principles that make the product robust.

The more of these that get written, the sooner people will begin to understand the possibilities of the product.



# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/1/2007 11:21 AM Sahil Malik
Well - sure you can do codebehind. How else do you think webservices run?

Good article!

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/1/2007 12:20 PM sean k.
Gravatar Fantastic! Thanx for the great tip, Andrew!

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/1/2007 12:23 PM AC [MVP MOSS]
Gravatar Sahil-
There are Web services in SharePoint? ;)

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/1/2007 2:23 PM Guru Nagaraju
Gravatar Excellent Article!

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/1/2007 11:08 PM Clint Simon
Gravatar Hi Andrew, we haven't met but you taught a course a week weeks ago that my collegues at Ascentium attended (they all rave about you, BTW).

In this post I see that you are use your own wsp packaging utility. I would love it if you could take a look at a wsp package/deployment utility that I call SPDeploy.

It is an msbuild extension and visual studio project template and I think it takes what you have here to the next level. My teammates and me have been using it for quite a while and it has poven to be very valuable.

Please take a look, and I would LOVE to hear your feedback. You can find SPDeploy here:

http://www.spsimon.com/2007/09/introducing-spdeploy.html

Thanks!

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/2/2007 3:38 AM Sezai Komur
Gravatar Yes it does work quite well, been using this method for months now, it's powerful.

http://www.sharepointblogs.com/sezai/archive/2007/08/30/extend-the-publishinglayoutpage-class-create-your-own-to-run-code-in-the-page.aspx

It's funny how people jump to conclusions and state "YOU CANT DO IT" when you don't have built-in IDE support to quickly do it for you, just because something doesn't automatically do it for you, doesn't mean it can't be done !

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/2/2007 7:07 AM AC [MVP MOSS]
Gravatar Clint-
Just look at a look at your post... very interesting stuff... will definately download and poke around. Really eager to take a look at your custom tasks you've created & the tarets file.

However I have to admit I'm not much of a fan of custom project templates in Visual Studio. If you share a project with someone else who doesn't have the template installed, they won't be able to open it up. Then people think the code doesn't work or their VS is screwed up... I've been on the wrong end of this stick too many times. Therefore, I like creating all my projects using either the Class Library or Empty Project project templates and go from there. I am working on something at the moment though that will make this process even easier. It takes a different approach all together... nothing required just to work with the projects.

Don't get me wrong... what you've done is very impressive! Just a personal preference... may not be an issue for you and your team if you all have the project template installed. But for me, who shares code frequently with customers and students... doesn't work as well. Hope this isn't coming across as a negative reply :).

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/19/2007 8:05 AM Rohan
Gravatar thanks for the gr'8 post Andrew,
even for a newbie' like Me this post was quiet useful, I just have a small request if possible can u tell me which "schemas" are used to create the "feature.xml" and "element.xml" file.

And what "extra work" do we have to put-in to make this thing work ???

Thanks Once Again.....

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/19/2007 9:14 AM AC [MVP MOSS]
Gravatar Rohan-
SharePoint's XML schema is called the CAML schema for Collaborative Application Markup Language. Specifically you're looking for the Feature schema. The "extra work" is what I explain in the article... the part about using a Feature to provision the file.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/19/2007 9:57 AM Girish
Gravatar Good article But how to deploy a fetaure as a web part in sharepointy


# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/19/2007 1:30 PM AC [MVP MOSS]
Gravatar Girish-
Not possible... you use Features to add Web Part definitions to sites (look in the SDK for this). The deployment is handled by WSS Solution Packages (WSP's)... again, in the SDK.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/25/2007 1:51 PM Gopi
Gravatar I tried this to access the codebehind end up with this error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: The event handler 'OnClick' is not allowed in this page.

Source Error:

Line 8: <h3>Enter Some Text here and Press the Button</h3>
Line 9: &nbsp;&nbsp;<asp:TextBox Id="TextInput" runat="server"/>&nbsp;
Line 10: <asp:Button ID="Trigger" runat="server" Text="Submit" OnClick="Trigger_Onclick"/>
Line 11: <p>The text Enterd was <asp:Literal Id="TextOutPut" runat="server"/>
Line 12: And was submitted at <asp:Literal Id="TimeStamp" runat="server"/>.</p>

Do you have any idea!why I shudn't use the event handler here in sharepoint page!or whrer i went wrong!?


# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/25/2007 2:11 PM AC [MVP MOSS]
Gravatar Gopi-
Not sure because the code you've shown is exactly like what I showed.

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 10/30/2007 1:54 AM Mohammed Zayed
Gravatar Awesome Article Andrew ;)

Thanks a million.

Regards,
Mohammed Zayed
Information Worker Specialist

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 11/8/2007 9:09 AM Frode Magnussen
Gravatar Tested this way of doing it today. Works great!
Thanks Andrew...love your blog.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 11/8/2007 9:17 AM Gary
Gravatar Gopi-

I got the same error as you but only after altering CodeBehindDemo.aspx (minor text change) in Sharepoint Designer.

I deleted CodeBehindDemo.aspx and reapplied the feature and worked again.

A question for you Andrew should the aspx not be modified in SharePointDesigner?

Cheers
Gary

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 11/8/2007 11:10 AM Gary
Gravatar Gopi

Me again. After my last post I went into 'OnClick' hell. It turned out that all that was needed was
<PageParserPath VirtualPath="/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true" />

in the web.config.

CHeers


# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 11/12/2007 9:31 AM AC [MVP MOSS]
Gravatar Gary-
Actually that is a very bad way to do that. What happened was that putting the event handler in the page's ASPX was considered inline script (I didn't realize that). So when you customized it using SPD, SharePoint's safe mode parser blocked the page from executing. What you did was basically turn off the safe mode parser for the whole site... a VERY BAD idea.

The correct way was to set the event handler in the code behind. See figure #2 and #3 above as well as point #3 in the list after figure #2 for an update to the article.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 11/21/2007 12:36 AM John Creviston
Gravatar How would you get the page Title and Breadcrumbs to show for this page in the heading like the do for site pages created in the web browser?

I am successfully using this development approach, but I can't figure this one out.

Thanks.

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 11/21/2007 4:29 PM AC [MVP MOSS]
Gravatar John-
I haven't tested the breadcrumb, but I suspect you just add an entry to the navigation (SPWeb.Navigation.QuickLaunch|TopNavigationBar).

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 11/26/2007 3:22 AM Urko
Gravatar Hi!

Nice article!!!

I am trying to test it but i have a problem when i compile it, so can you tell me how to deploy it by hand?

thx!!!

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 11/26/2007 8:25 AM AC [MVP MOSS]
Gravatar Urko-
Just look at the manifest.xml file as that's the instructions to SHarePoint where to put everything.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/2/2007 7:15 PM AndyD
Gravatar Andrew, a great article and it has answered "my myths"! Keep it up to the great work.

AndyD.

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/3/2007 12:59 AM Suresh
Gravatar Hi !
I try to use it. But I got an Error.
Please solve it.


The base type 'AndrewConnell.SharePoint.Samples.PageTemplate' is not allowed for this page. The type is not registered as safe.



# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/3/2007 1:19 AM Suresh
Gravatar Hi ,
can any body help .
I want to create an aspx page with coding to do something.
and I want to display this page in a web part of a WSS site Page.


# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/3/2007 1:40 AM AC [MVP MOSS]
Gravatar Suresh-
That says you haven't listed the assembly as a <SafeControl /> in the web.config.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/3/2007 6:06 AM Gary
Gravatar I wonder if someone can point me in th ecorrect direction with the following problem.

I have been packaging my code behind up as above which has been working well. I have now attempted a migration from CMS 2002 which has also worked well.

My problem comes in trying to replace the dummy aspx's created in the migration. The solution says it has deployed correctly, I have then manually deactivated then activated the feature. Also tried a revert to site definition but no joy there. However hard I try the migrated aspx always stays put.

I suppose I could give my templates a new name then write some code to move all the pages to the new aspx. But that seems a sledgehammer approach, besides I like my names :)

I believe there must be a way to force this but have unsuccessfully googled this issue. Any help would be greatly appreciated.

Gary

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/3/2007 9:08 AM AC [MVP MOSS]
Gravatar Gary-
Deactivating Features doesn't remove the provisioned files, that is by design. If you want to remove the provisioned files you need to delete them manually or though code (FeatureDeactivated event).

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/3/2007 10:12 AM Gary
Gravatar Andrew

I would love to delete the dummy aspx's but Sharepoint referential kicks in as they are linked to numerous pages ( As you would expect).

So is the only solution to create another holding/dummy aspx move all postings to that one. Delete original dummy aspx and replace with the one from my feature. Move all pages back from the holding aspx to my new aspx? Repeat for each migrated aspx.

Gary

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/3/2007 10:22 AM AC [MVP MOSS]
Gravatar Gary-
This sounds like a development issue... I like to use a disposable site collection in my dev environment to design the ASPX files... creating them in a WYSIWYG environment then save them locally and provision using a Feature into my integration/build environment.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/20/2007 3:51 AM Kev
Gravatar Does this work for an application page - and by application page, I mean a page that exists only in the LAYOUTS FOLDER?

I get this error when I try to add code-behind to the bin folder for the page:

Request failed. at System.Reflection.Assembly._GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at System.Web.UI.TemplateParser.GetType(String typeName, Boolean ignoreCase, Boolean throwOnError)
at System.Web.UI.TemplateParser.ProcessInheritsAttribute(String baseTypeName, String codeFileBaseTypeName, String src, Assembly assembly)
at System.Web.UI.TemplateParser.PostProcessMainDirectiveAttributes(IDictionary parseData)

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/20/2007 6:41 AM AC [MVP MOSS]
Gravatar Kev-
Yup... application pages can also have code behinds. You'll need to put the assembly in the GAC though.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/20/2007 10:56 AM Kev
Gravatar AC -

Thanks for the reply. To be clear, does this mean that the <PageParserPath> and <SafeControl> entries doesn't apply to assemblies in the BIN folder ?

Because when I try this example, with the code-behind class inheriting from LayoutsPageBase, I get the exception above. I believe it's a permissions issue and no matter how I change the <PageParserPath> and <SafeControl>, I can't get past it.

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 12/20/2007 12:58 PM AC [MVP MOSS]
Gravatar Kev-
Are you deploying it to the GAC? If it is inheriting from LayoutsPageBase, the assembly MUST be in the GAC. And yes, the PageParserPath applies to everything... as does SafeControls.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/12/2008 11:58 AM Srinivas
Gravatar Hi AC,

Thanks for your article.

I am new to MOSS,Please provide me some info. to create Element.xml.Manifest.xml,Feature.xml and .ddf.I am unable to create this files,without this I wont be able to proceed further in that.


Regards
N.Srinivas

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/12/2008 1:30 PM AC [MVP MOSS]
Gravatar Srinivas-
Please check the SharePoint SDK which has a ton of information on all of these tasks. You need to create a Feature and Solution... all of this is demonstrated in the download assocaited with this article, linked above, as well in numerous Visual How To's on MSDN (http://msdn2.microsoft.com/en-us/library/bb892193.aspx) and in every SharePoint development book.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/15/2008 11:57 AM Glenn J. Melton
Gravatar I get this error after i've installed and deployed the feature when i try to add it to a site: "An error occurred during the processing of . Could not load the assembly 'Version=1.0.0.0, Culture=neutral, PublicKeyToken=8b71068d79515ff9'. Make sure that it is compiled before accessing the page." I built the project and then tweaked the csprog file like you discussed in the article.

This is what's in my page directive: <%@ Page MasterPageFile="~masterurl/default.master" Debug="true" Inherits="EVBankForms.SharePoint.RequestInfoTemplate, EVB_RequestInfo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8b71068d79515ff9" Language="C#" %>

I use'd reflector to get the pubkeytoken. What could be the problem?

Glenn...

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/15/2008 12:27 PM AC [MVP MOSS]
Gravatar Glenn-
It can't find your assembly. Make sure it is either in the site's BIN directory or the GAC depending on where you depoyed it. If you see it, make sure the strong name is correct. Looks like it should be named EVB_RequestInfo.dll.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/17/2008 1:33 PM Glenn J. Melton
Gravatar I want to thank you for your help. The assembly is being loaded into the bin directory of the site now. What's happening to me now is that I get a 404 error when i try to add the feature to the site when it tries to load it the browser. The file RequestInfoForm.aspx is not being copied into the sharepoint web site for some reason. I'm including my feature and element files if you can look at them for me I'd very much appreciate it and any help you could give. Does it have anything to do with the <module url...> tag/attribute in the elements file?

------------------------------------------------------
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="A0399470-846C-48BD-AAC3-D963936142D1"
Title="EvergreenBank Request Information Form"
Description="Adds Request Information form to site"
Scope="Web"
Hidden="False"
AlwaysForceInstall="True"
Version="1.0.0.0">

<ElementManifests>
<ElementManifest Location="Elements.xml" />
<ElementFile Location="RequestInfoTemplate.aspx" />
</ElementManifests>

</Feature>

------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- create a new page 'RequestInfoForm' in a WSS site based off RequestInfoTemplate.aspx -->
<Module Url="Forms">
<File Url="RequestInfoTemplate.aspx"
Name="RequestInfoForm.aspx"
Type="Ghostable">
</File>
</Module>

<!-- add a link to the bottom of the SiteActions menu to make it easy to jump to demo page-->
<CustomAction Id="6A30C15D-6757-4A7A-BEA9-C0AD84C833BB"
Location="Microsoft.SharePoint.StandardMenu"
GroupId="SiteActions"
Sequence="99"
Title="Request Information Form"
Description="This is the Request Information form.">
<UrlAction Url="~site/RequestInfoForm.aspx" />
</CustomAction>

</Elements>

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/17/2008 2:06 PM AC [MVP MOSS]
Gravatar Glenn-
In your <MODULE> element you are provisioning all the files (just one in your case) to the "Forms" subfolder off the root of the site. Open the site up in SharePoint Designer and you'll see your ASPX page in there... or to test, just add a "/Forms" after the ~site in the <UrlAction> element in the <CustomAction>.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/17/2008 3:03 PM Glenn J. Melton
Gravatar I created a forms folder in sp designer, modified the elements file to load the file into ~site/Forms/ and when I select the feature in site actions, i get a 404 error still. Next I went to the physical directory on the web server and created a Forms directory under the root. I ran site actions again to load the feature to the site and got a this:

Server Error in '/' Application.
-----------------------------------------------------------------------

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Forms/requestinfoform.aspx

------------------------------

The dll for the page in in the physical bin directory on the server. The server's event log has no entries relative to what I'm trying to accomplish like audit failures or errors.

Any ideas???? Is the Type="Ghostable" potentially causing this?

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/17/2008 3:35 PM AC [MVP MOSS]
Gravatar Glenn-
You should not create anything in the webroot on the server... totally unnecessary. When you provisioned the file, you said it activated just fine. Therefore the file was provisioned. You need to look to see where it was provisioned. In your original post that points to /Forms/RequestInfoForm.aspx. Use SharePoint Designer to try to find the file...

Have you tried typing the URL directly to the file? if you activated it in the http://litware.com/subsiteA site, then it would be http://litware.com/subsite/forms/requestinfoform.aspx.

Ghostable is not the problem... that provisions files into the site. The other option, GhostableInLibrary, provisions files into document libraries which is not what you want here.

Please refer to the sample solution I provide to see how this should work.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/17/2008 6:15 PM Glenn J. Melton
Gravatar I must tell you that you have been of exceptional assistance with the issues I've been experiencing. I am able to get through the whole process successfully now. The last issue I found with the process (this may be amusing) was that my UI designer removed the <form runat="server" ...> tag from the default.master and each time I deployed the feature to the site, and it tried to bring it up, I got the really useful error message from the admin site in SP Designer that an "unexpected error had occured".

Thanx a million AC - you rock!!

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/18/2008 7:33 AM AC [MVP MOSS]
Gravatar Glenn-
Good to hear... what's your address? I'll send an invoice :) JUST KIDDING of course... glad I could help!

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/22/2008 3:45 PM Viraj Deshpande
Gravatar Hi,

I have followed the exact steps, but in OnInit when i am associating any event handler for any control i can see that control is set to NULL, due to which it is failing.

Am i missing anything?

Regards
-- VirajD

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/22/2008 4:54 PM AC [MVP MOSS]
Gravatar VirajD-
Then the names of your controls, both in the ASPX & as the protected field are not identical, thus ASP.NET can't perform the auto wire-up. The other process is for you to use the FindControl() method to get a reference to the field that you want to create an event for. In the code sample above, I used the identical name for the ID and the protected field so ASP.NET would wire them up for me (as shown in figures 2 & 3).

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/23/2008 6:18 PM Viraj Deshpande
Gravatar Hi AC!

I am using the Identical names in both ASPX and .CS as well. It somehow worked for me now. But whenever i add a new control the issue jumps again. Then after redeploying the feature it goes off.

Few Qs

1.When we add a new control then is it necessary to reinstall the feature?
2. If i change the theme for my site it doesn't get applied to my custom pages added in this fashion. What can be done for that?

Thanks for your help.

Regards
-- VirajD

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/23/2008 7:28 PM Viraj Deshpande
Gravatar Hi AC!

One more question. Whichever the controls that i am using inside the custom page they are not getting the SP style sheet applied. I need to set the style for each and every control seperately.

In case of webparts we just need tp build the webpart and style will automatically gets applied.

Is there anything that we can do for our custom pages to avoid applying style seperately for each control?

Regards
-- VirajD

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/23/2008 8:30 PM AC [MVP MOSS]
Gravatar Viraj-
1) If the file is uncustomized, then you can just modify the file in the Feature or, ideally, update it with a WSP. Since it is uncustomized, the source of file on the file system is that one that is used.
2) Your custom pages aren't using the styles that MSFT has defined in their style sheets. A theme is just CSS files.

Don't understand your last question about CSS.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/24/2008 8:20 AM Geoff
Gravatar Any idea why this doesn't work too well in VS2008? It's possible to build the code but the design view of the pages doesn't render because VS can't load the master page using the URL ~/_layouts/applicaiton.master

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/24/2008 11:45 AM AC [MVP MOSS]
Gravatar Geoff-
It does work in VS2008... I'm doing that today. You can't design the pages because Visual Studio has no hooks into SP or doesn't understand SharePoint, so you can only work in the HTML view.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/24/2008 12:16 PM Geoff
Gravatar AC - Thanks for your response.

In VS2005 the design view does work but in 2008 all you get is an error saying that the master page cannot be loaded.

I've come up with a work around but it's not ideal :(

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/24/2008 12:49 PM Viraj Deshpande
Gravatar Hi AC!

I have used the way which this article is describing to create custom page. My custom page has a Data Grid control on it. To make a look and feel i.e. styles of this Data Grid similar as that of Doc Lib in the SP site, i have used the CSS given in core.css file i.e. "ms-xxxx" for header of Data Grid and other controls on the page.

Now when i am changing the theme of my site, all the other pages except the custom pages get this new theme applied. My custom pages still has default theme i.e. light blue color even for Data Grid header, Page Headers, Tabs, Quick Launch etc... on the page which are part of master page.

How can i make sure that updated theme gets applied to my custom page too.

Thanks for your help.

Regards
-- VirajD

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/25/2008 12:43 AM AC [MVP MOSS]
Gravatar Viraj-
Instead of using the GridView, use the SPGridView in the SharePoint assemblies. It is what MSFT uses in the list views... thus you don't need to set any CSS classes. See if that helps... if not, I'm not sure as I don't work with themes very much.

# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/25/2008 12:45 AM AC [MVP MOSS]
Gravatar Geoff-
My point was that Visual Studio can't load the master page because the paths to the masters are dynamic tokens. I can't speak for why one version works in Design mode and the other doesn't... but that's beside the point: what you are trying to do is not supported and I don't recommend it because of issues like this. Just stay in the HTML view.

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/28/2008 4:26 PM Viraj Deshpande
Gravatar Hi AC!,

Instead of inheriting from System.Web.UI.Page i have inherited my page from Microsoft.SharePoint.WebPartPages.WebPartPage. This way i can integrate any webpart if needed in future.

Now how can i add a SPGridView control on this ASPX page. I am not able to get how can i add the SPGridView control in this page? I mean do i need to create it as a child control and build a logic for rendering OR simply i can use any TAG like <SPGridView>?

Thanks for your help.

Regards
-- VirajD

 re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites 1/28/2008 4:26 PM Ed.
Gravatar I'm using this technique on several pages where I need code behind functionality. My problem is trying to use localization to access a local resource. I can access global resources in the .ASPX by using either:
<%=GetGlobalResourceObject( "WebResourcesGlobal", "Key" ).ToString()%>
or:
<asp:Literal ID="Literal1" runat="server" Text="<%$Resources:WebResourcesGlobal,Key%>" />
But, the same techniques do not seem to work for local resources, e.g.:
<%=GetLocalResourceObject("LocalKey").ToString()%>

It can't seem to find the local resource file, either if I include it as part of the feature, or if I compile the resource into the DLL.

Would you please explain why this doesn't work and recommend the proper method of accessing local resources in a SharePoint ASPX with a code behind?

Thank you for your time.
- Ed.


# re: Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Si