Search code examples
jsonodatasensenet

Using JSON arrays in SenseNet settings


If JSON arrays are used in a SenseNet settings object, they are not accessible via the OData API.

For example, consider the following SenseNet settings object, which comes installed at Root/System/Settings/Portal.settings by default:

{
    ClientCacheHeaders: [
        { ContentType: "PreviewImage", MaxAge: 1 },
        { Extension: "jpeg", MaxAge: 604800 },
        { Extension: "gif", MaxAge: 604800 },
        { Extension: "jpg", MaxAge: 604800 },
        { Extension: "png", MaxAge: 604800 },
        { Extension: "swf", MaxAge: 604800 },
        { Extension: "css", MaxAge: 600 },
        { Extension: "js", MaxAge: 600 }
    ],
    UploadFileExtensions: {
        "jpg": "Image",
        "jpeg": "Image",
        "gif": "Image",
        "png": "Image",
        "bmp": "Image",
        "svg": "Image",
        "svgz": "Image",
        "tif": "Image",
        "tiff": "Image",
        "xaml": "WorkflowDefinition",
        "DefaultContentType": "File"
    },
    BinaryHandlerClientCacheMaxAge: 600,
    PermittedAppsWithoutOpenPermission: "Details"
}

When viewing this object through the OData API, the ClientCacheHeaders field is not included:

{
    "d": {
        "UploadFileExtensions.jpg": "Image",
        "UploadFileExtensions.jpeg": "Image",
        "UploadFileExtensions.gif": "Image",
        "UploadFileExtensions.png": "Image",
        "UploadFileExtensions.bmp": "Image",
        "UploadFileExtensions.svg": "Image",
        "UploadFileExtensions.svgz": "Image",
        "UploadFileExtensions.tif": "Image",
        "UploadFileExtensions.tiff": "Image",
        "UploadFileExtensions.xaml": "WorkflowDefinition",
        "UploadFileExtensions.DefaultContentType": "File",
        "BinaryHandlerClientCacheMaxAge": 600,
        "PermittedAppsWithoutOpenPermission": "Details",
    }
}

If you search specifically for the ClientCacheHeaders field using the following query:

Odata.svc/Root/System/Settings('Portal.settings')?&metadata=no&$select=ClientCacheHeaders

the API returns null:

{
    "d": {
        "ClientCacheHeaders": null
    }
}

I know that JSON arrays are allowed in settings files because the above example is referenced in the SenseNet wiki page describing settings usage.

Am I performing my OData query incorrectly, or is this some sort of parsing bug in the SenseNet API?


Solution

  • Here's an implementation of the custom OData function suggested by Miklos. Once this is done, you have to register the OData call as described here.

    public static class OData
    {
        [ODataFunction]
        public static string GetMySettings(Content content)
        {
            var retstr = "";
            try
            {
                var settingsFile = Settings.GetSettingsByName<Settings>("MySettings", content.Path);
                var node = Node.LoadNode(settingsFile.Path) as Settings;
                var bindata = node.GetBinary("Binary");
    
                using (var sr = bindata.GetStream())
                using (var tr = new System.IO.StreamReader(sr))
                    retstr = tr.ReadToEnd();
            }
            catch (Exception e)
            {
                SnLog.WriteException(e);
            }
    
            return retstr; 
        }
    }