Using Visual Studio 2008, .NET 3.5 and Ajax with Windows SharePoint Services 3.0
With the recent release of Visual Studio 2008, I figured it would be a good idea to start using C# 3.0, .NET 3.5, Ajax and all the other goodies presented by that, when building SharePoint pages. My approach to get things up and running, was as follows.
1. Modify the web.config of the target web application
In configuration -> configSections: <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
</sectionGroup>
</sectionGroup>
</sectionGroup>
In SharePoint -> SafeControls: <SafeControl Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TypeName="*" Safe="True" />
In system.web -> httpHandlers: <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
In system.web -> httpModules: <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
In system.web -> compilation -> assemblies: <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
In system.web -> pages: <controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</controls>
2. Create an Ajax-enabled web page in Visual Studio 2008, and make sure to target .NET 3.5
To do this, I suggest you read up on Andrew Conells excellent article Using ASP.NET 2.0 Code Behind Files in SharePoint v3 Sites.
Once you've got the basic structure of the solution up and running, you can start adding the ASP.NET and C# code. I'll leave that part to you.
One piece of code you will need to stick in all web pages using Ajax, along with a script manager, is <script type='text/javascript'>_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;</script>
... otherwise all postbacks after the first will be surpressed. This is part of the a double-submit protection enforced by the default SharePoint form.
You will also have to make sure the page you build doesn't contain its own <form>. Failure to do so will cause your newly added page to crash for no apparent reason ("An unexpected error has occurred").
3. Install the .NET 3.5 runtime on your server
Download available at microsoft.com.
4. Deploy the solution
Use stsadm to install the solution you built in step 2, then open the SharePoint central administration to deploy it to the sites in your farm.
That's all there's to it. Well it was for me, in either case. If you run into trouble; feel free to ask.

4 comments:
It's nice to see a native C++ guy turned to into Web and Managed world.
Ack... Hehe. Well I've been poking around with .NET the last few years as well, but C++ has always seemed more interesting a subject for blog posts :)
Thank you! This saved us a lot of time.
Great article...
Post a Comment