Archive

Archive for the ‘AJAX’ Category

JavaScript, JSON, WCF, and passing dates, oh my!!!!

May 28th, 2010 Michael Bell No comments

I spent more time than I will EVER admit trying to figure out what the heck was wrong with my code, passing a date created by the jQuery UI datepicker into my .NET WCF service. I tried tick conversions (which would wind up sending a date WCF didn’t understand). The only things I COULD get WCF to understand, read around the year 1970. So, with a bit of research, I woudn up with the following code to convert a javascript date to a format that WCF wants to see.

$.ajax({
     type: "POST",
     contentType: "application/json; charset=utf-8",
     url: "/WebServices/WebServiceHost.svc/scorePhotographer",
     data: '{"datevisited": "\\\/Date(' + Date.UTC(x.getUTCFullYear(), x.getUTCMonth(), x.getUTCDate(), x.getUTCHours(), x.getUTCMinutes(), x.getUTCSeconds(), x.getUTCMilliseconds()) +
          (-x.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(x.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(x.getTimezoneOffset() / 60)) + '00' +
          ')\\\/" +
          '"}',
     dataType: "json",
     dataFilter: function(data, type) {
     var d = data.replace(/"\\\/(Date\(.*?\))\\\/"/gi, 'new $1');
     return d;
},
success: function (msg) {
     // blah
},
error: function (e) {
     //blah
}
});

Hope this helps someone!

Categories: AJAX, ASP.NET, WCF, jQuery Tags:

Configuring Microsoft AJAX to work in SharePoint 2007, MOSS

February 6th, 2009 Michael Bell 3 comments

I had the pleasure of recently converting a couple of webparts & controls to use Microsoft AJAX rather than AJAXPro.net. On top of all of my control changes, I had to do quite a few config file changes to SharePoint to ge tit to work correctly. Now, the instructions for this are out there on Microsofts site, and kudos to them for providing that. What they didn’t include, is sample code to implement these changes through managed code to your SharePoint farm. So that’s what this example does:

public override void SetConfigModifications(SPWebApplication WebApp, string Owner)
{
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("add[@path='*.asmx']",
"configuration/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'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("add[@path='*_AppService.axd']",
"configuration/system.web/httpHandlers", @"<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'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("add[@path='ScriptResource.axd']",
"configuration/system.web/httpHandlers", @"<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'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 // AJAX HttpModule
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("add[@name='ScriptModule'][@type='System.Web.Handlers.ScriptModule, " +
"System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']",
"configuration/system.web/httpModules", @"<add name='ScriptModule' type='System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 // Microsoft AJAX Tie-In
#region Sections and Groups
// SectionGroup for system.web.extensions
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("sectionGroup[@name='system.web.extensions']",
"configuration/configSections", @"<sectionGroup name='system.web.extensions'
type='System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' />",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 // SectionGroup for scripting
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("sectionGroup[@name='scripting']",
"configuration/configSections/sectionGroup[@name='system.web.extensions']", @"<sectionGroup name='scripting'
type='System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' />",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("section[@name='scriptResourceHandler']",
"configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']",
@"<section name='scriptResourceHandler'
type='System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' requirePermission='false' allowDefinition='MachineToApplication'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("sectionGroup[@name='webServices']",
"configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']",
@"<sectionGroup name='webServices'
type='System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' />",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("section[@name='jsonSerialization']",
"configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']/" +
"sectionGroup[@name='webServices']", @"<section name='jsonSerialization'
type='System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' requirePermission='false' allowDefinition='Everywhere' />",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("section[@name='profileService']",
"configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']/" +
"sectionGroup[@name='webServices']", @"<section name='profileService'
type='System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35' requirePermission='false' allowDefinition='MachineToApplication' />",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("section[@name='authenticationService']",
"configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']/" +
"sectionGroup[@name='webServices']", @"<section name='authenticationService'
type='System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' requirePermission='false' allowDefinition='MachineToApplication' />",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));
#endregion

 // AJAX Assembly
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification(
"add[@assembly='System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']",
"configuration/system.web/compilation/assemblies",
"<add assembly='System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 // Pages Section
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("controls",
"configuration/system.web/pages", "controls",
SPWebConfigModification.SPWebConfigModificationType.EnsureSection, Owner));

 // Pages entry for AJAX Assembly
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification(
"add[@tagPrefix='asp'][@assembly='System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']",
"configuration/system.web/pages/controls",
"<add tagPrefix='asp' namespace='System.Web.UI' assembly='System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 // AJAX SafeControls Entry
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification(
"SafeControl[@Assembly='System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']",
"configuration/SharePoint/SafeControls",
"<SafeControl Assembly='System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' Namespace='System.Web.UI' TypeName='*' Safe='True' />",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 // AJAX System.Web.Extensions Section
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("system.web.extensions",
"configuration", "system.web.extensions", SPWebConfigModification.SPWebConfigModificationType.EnsureSection, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("scripting",
"configuration/system.web.extensions", "scripting", SPWebConfigModification.SPWebConfigModificationType.EnsureSection, Owner));

WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("webServices",
"configuration/system.web.extensions/scripting", "webServices", SPWebConfigModification.SPWebConfigModificationType.EnsureSection, Owner));

 // AJAX System.webServer Section
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("system.webServer",
"configuration", "system.webServer", SPWebConfigModification.SPWebConfigModificationType.EnsureSection, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("validation[@validateIntegratedModeConfiguration='false']",
"configuration/system.webServer", "<validation validateIntegratedModeConfiguration='false'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 // AJAX System.webServer Modules Section
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("modules",
"configuration/system.webServer", "modules", SPWebConfigModification.SPWebConfigModificationType.EnsureSection, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("add[@name='ScriptModule'][@preCondition='integratedMode']",
"configuration/system.webServer/modules", @"<add name='ScriptModule' preCondition='integratedMode'
type='System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 // Web Services to system.web
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("webServices",
"configuration/system.web", "webServices", SPWebConfigModification.SPWebConfigModificationType.EnsureSection, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("protocols",
"configuration/system.web/webServices", "protocols", SPWebConfigModification.SPWebConfigModificationType.EnsureSection, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("add[@name='HttpGet']",
"configuration/system.web/webServices/protocols", @"<add name='HttpGet' />",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("add[@name='HttpPost']",
"configuration/system.web/webServices/protocols", @"<add name='HttpPost' />",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 // AJAX System.webServer Handlers Section
WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("handlers",
"configuration/system.webServer", "handlers", SPWebConfigModification.SPWebConfigModificationType.EnsureSection, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("remove[@name='WebServiceHandlerFactory-Integrated']",
"configuration/system.webServer/handlers", @"<remove name='WebServiceHandlerFactory-Integrated' />",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("add[@name='ScriptHandlerFactory']",
"configuration/system.webServer/handlers", @"<add name='ScriptHandlerFactory' verb='*' path='*.asmx'
preCondition='integratedMode'
type='System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("add[@name='ScriptHandlerFactoryAppServices']",
"configuration/system.webServer/handlers", @"<add name='ScriptHandlerFactoryAppServices' verb='*'
path='*_AppService.axd' preCondition='integratedMode'
type='System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

 WebApp.WebConfigModifications.Add(WebConfigManager.CreateModification("add[@name='ScriptResource'][@preCondition='integratedMode']",
"configuration/system.webServer/handlers", @"<add name='ScriptResource' preCondition='integratedMode'
verb='GET,HEAD' path='ScriptResource.axd'
type='System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'/>",
SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Owner));

WebApp.Update();
WebApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
Categories: AJAX, ASP.NET, MOSS, SharePoint 2007, c# Tags: