Archive

Archive for the ‘AJAX’ Category

The “Circular Reference” issue with Entity Framework, JSON and retaining relationship data

June 25th, 2017 Michael Bell No comments

I’ve got a WCF service that I was trying to return multiple data sets in the same call. You can read about that in the previous post. What I tried today for the first time, was to return objects along with other objects that shared a relationship. What I found was that you get circular reference issues when you try to do this. After about a day of googling, I found that you can use the JSON.net library to get around that issue. The example code follows:

[WebInvoke(Method = "POST",
 BodyStyle = WebMessageBodyStyle.WrappedRequest,
 ResponseFormat = WebMessageFormat.Json)]
 public dynamic getGalleryOutput(int GalleryID)
 {
 EasyGalleryDataContext data = new EasyGalleryDataContext();

 Folder folder = getAlbum(GalleryID);

 var includes = from gti in data.GalleryTemplateIncludes
 select new {
 gti.GalleryTemplateIncludeID,
 gti.IncludeID,
 gti.IncludeType,
 gti.IncludeOrder,
 gti.TemplateInclude
 };

 JsonSerializerSettings jsSettings = new JsonSerializerSettings();
 jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

 dynamic[] results = { folder, folder.Images, folder.GalleryTemplate, includes };
 return JsonConvert.SerializeObject(results, Formatting.None, jsSettings);
 }
Categories: AJAX, ASP.NET, Entity Framework, JSON, WCF Tags:

Configuring Microsoft AJAX to work in SharePoint 2007, MOSS

June 25th, 2017 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:

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

June 25th, 2017 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:

WCF Services, AJAX, and the new ‘dynamic’ keyword…

June 25th, 2017 Michael Bell 1 comment

Recently I had a project where I wanted to return multiple sets of data via AJAX with a single call, although the sets of data were different types (from different tables altogether). Anything to save some bandwidth/roundtrips, right? I made an attempt at using the new dynamic type to create my service method and to hold my sets of data, and was able to use the JavaScriptSerializer to return the JSON that I needed for my jQuery web service. Example below :)

[WebInvoke(Method = &amp;quot;POST&amp;quot;,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json)]
public dynamic getPriceRelatedData()
{
    EasyGalleryDataContext data = new EasyGalleryDataContext();

    var sizes = from szs in data.Sizes
        select new {
            szs.SizeID,
            szs.Height,
            szs.Width
        };

    var finishes = from fn in data.Finishes
        select new
        {
            fn.FinishID,
            fn.Title
        };

    dynamic pricesheets = from ps in data.PriceSheets
                        select new
                        {
                            ps.PriceSheetID,
                            ps.Title,
                            PriceSheetItems = (from psi in data.PriceSheetItems
                                                where psi.PriceSheetID == ps.PriceSheetID
                                                select new
                                                {
                                                    psi.PriceSheetItemID,
                                                    psi.PriceSheetID,
                                                    psi.SizeID,
                                                    psi.FinishID,
                                                    psi.Price
                                                })
                        };

    dynamic[] results = { sizes, finishes, pricesheets };

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    return serializer.Serialize(results);
}
Categories: AJAX, ASP.NET, WCF, c#, jQuery Tags: