Generic Sense/Net OData action

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.3
  • Enterprise
  • Community
  • Planned

Overview

Generic Sense/Net OData action

Sense/Net ECMS has a powerful and always evolving OData REST API that you can use to invoke actions easily from client side code. There are a number of Built-in OData actions and functions that you can call and you are also able to create your own custom OData action. This article describes an even easier way to call custom code using our OData Rest API.

Details

In most cases when you need to perform an action in a client-side environment, you use the following pattern:

  • choose an action
  • collect the parameters it needs
  • send an OData request to that action

If you have a custom task that you need to perform and there is no built-in OData action for that, you could create your own custom OData action. However the steps above are quite similar to simply calling a method. This is exactly what you can achieve by using the Generic Sense/Net OData action feature of Sense/Net ECMS: you can call a custom method through OData without having to create a new action. For security reasons there are a couple of restrictions on the methods you are able to call this way:

  • the method must be public
  • the method must be static
  • the first parameter must be a Content (this will be the OData resource)
  • other parameters must be serializable (because the values are traveling in the request)
  • the method must have an ODataAction or ODataFunction attribute (calling a method that doesn’t have one of these attributes causes MethodAccessException)

The difference between an action and a function is that a function must not make any changes to the system. Actions (e.g. SetPermissions or Delete action) do make changes therefore they can be called only using the POST HTTP method. If your method affects the repository, please mark it with the ODataAction attribute.

Custom method

You can place a static method into any of your custom classes. The following example shows which methods can be called through the generic Sense/Net OData action and which not:

namespace MyCompany.MyFeature
{
    public static class MyTools
    {
        // not allowed (the attribute is missing)
        public static string Method0(Content content)
        {
            return "ok";
        }
 
        // not allowed (the method is not public)
        [ODataAction]
        internal static string Method1(Content content)
        {
            return "ok";
        }
 
        // ALLOWED
        [ODataFunction]
        public static string Method2(Content content, string customName, int value)
        {
            return string.Concat(customName, ": ", value);
        }
    }
}

Generic OData application

To be able to reach the method you created above, you need to place an application of type GenericODataApplication into one of the Application folders of the Content Repository. You are also able to set permissions on this application content to restrict the access of the method to certain users. For example you can put this application to the following place by the name MyOperation:

  • /Root/(apps)/GenericContent/MyOperation
GenericODataApplication

You have to set the following fields of the application:

  • Class Name: MyCompany.MyFeature.MyTools (fully qualified class name)
  • Method Name: Method2
  • Parameters: string name, int value (comma separated ordered list of types and names, similar to the method declaration)

Please note: parameter names can be different than the ones on the method - only the types and order of parameters count. In the request you should use the names given here, on the application.

The value of the Parameters field will be used to select the appropriate method overload. The given names determine the JSON object format (see the request example below). Parameter types must be fully qualified type names except the following built-in short cuts:

  • string: System.String
  • string[]: Array of System.String
  • int: System.Int32
  • int[]: Array of System.Int32
  • bool: System.Boolean
  • long: System.Int64
  • decimal: System.Decimal
  • double: System.Double
  • object: System.Object
  • byte: System.Byte
  • sbyte: System.SByte
  • char: System.Char
  • float: System.Single
  • uint: System.UInt32
  • ulong: System.UInt64
  • short: System.Int16
  • ushort: System.UInt16
  • DateTime: System.DateTime
  • Node: SenseNet.ContentRepository.Storage.Node
  • Content: SenseNet.ContentRepository.Content

OData request

After completing the steps above you can send an OData request to invoke the method. Use the following values:

The response will be:

test: 42

Related links

References

There are no related external references for this article.