Appframe Data API


Overview

The Appframe Data API can be used to perform CRUD operations on all views and to execute procedures available to the af_user role in the database.

  • AppframeR4 developers must add views and procedures to be available as resources using the Data Resources under General settings in SiteSetup article.

  • Appframe365 developers have access to all the views and procedures on the database via the api.


Overriding users current domain

It is possible to override the users current domain when using domain based security by passing in "current-domain=Domain" in the request header.
This way you can have a current domain stored locally on your device instead of using the users default domain.


Examples using javascript


List all routes

This route lists all routes available for the below operations (for developers only)

    var dataApi = new af.data.Api("api/data");
    dataApi({ "operation": "listapi" }, function(error, data) {
        if (error) {
            alert(error);
            return;
        }
        console.log(data);
    });

Retrieve

    var dataApi = new af.data.Api("api/data");
    dataApi({
        "operation": "retrieve",
        "resourceName": "atbv_Testing_Issu11627",
        "maxRecords": -1, //optional
        "distinctRows": false, //optional
        "filterString": "", //optional (use either filterString or filterObject)
        "filterObject": null, //optional
        "whereClause": "", //optional (use either whereClause or whereObject)
        "whereObject": null, //optional
        "sortOrder": [{"testColumn": "ASC" }], //optional
        "excludeFieldNames": false, //optional
        "groupBy": ["testColumn", "testColumn2"], //optional
        "fields": [
            { "name": "testColumn" },
            { "name": "testColumn2" },
            { "name": "testColumn3", "aggregate": "COUNT" } // optional aggregate function
        ]
    }, function(error, data) {
        if (error) {
            alert(error);
            return;
        }

        console.log(data);
    });
    //response (excludeFieldNames:false)
    {"success":[{"testColumn":1,"testColumn3":null},{"testColumn":2,"testColumn3":null},
    //response (excludeFieldNames:true)
    {"success":[[1,null],[2,null],[2,null],[2,null],[3,null]]}
    {"testColumn":2,"testColumn3":null},{"testColumn":2,"testColumn3":null},{"testColumn":3,"testColumn3":null}]}

Create

var dataApi = new af.data.Api("api/data");
    dataApi({
        "operation": "create",
        "resourceName": "atbv_Testing_Issu11627",
        "uniqueName": "atbl_Testing_Issu11627",
        "TestColumn": 2, //value to be inserted
        "TestColumn3": 2, //value to be inserted
        "excludeFieldNames": false,
        "fields": [ //fields to retrieve
            { "name": "PrimKey" },
            { "name": "TestColumn" }
        ]
    }, function(error, data) {
        if (error) {
            alert(error);
            return;
        }
        console.log(data);
    });
    //response (excludeFieldNames:false)
    {"success":[{"PrimKey":"9fc1bd40-8bf7-4ced-af77-64010678b2f2","TestColumn":2}]}
    //response (excludeFieldNames:true)
    {"success":["597d708e-99be-4c90-8fb2-b5ec6b4d5512",2]}

Update

    var dataApi = new af.data.Api("api/data");
    dataApi({
        "operation": "update",
        "resourceName": "atbv_Testing_Issu11627",
        "uniqueName": "atbl_Testing_Issu11627",
        "fields": [ //fields to retrieve
            { "name": "PrimKey" },
            { "name": "TestColumn" },
            { "name": "TestColumn3" }
        ],
        "excludeFieldNames": false,
        "PrimKey": "f67abde2-dc70-4cec-85e8-361cf63b888d", //record to update (must also be present in fields list)
        //Fields to update
        "TestColumn": 3, //value to be updated (must also be present in fields list)
        "TestColumn3": 3 //value to be updated (must also be present in fields list)
    }, function(error, data) {
        if (error) {
            alert(error);
            return;
        }
        console.log(data);
    });
    //response (excludeFieldNames:false)
    {"success":[{"PrimKey":"f67abde2-dc70-4cec-85e8-361cf63b888d","TestColumn":3}]}
    //response (excludeFieldNames:true)
    {"success":["f67abde2-dc70-4cec-85e8-361cf63b888d",3]}

Destroy

    var dataApi = new af.data.Api("api/data");
    dataApi({
        "operation": "destroy",
        "resourceName": "atbv_Testing_Issu11627",
        "uniqueName": "atbl_Testing_Issu11627",
        "PrimKey": "cc1ae92f-9157-49ce-9781-baeef08e56f9" //record to delete
    }, function(error, data) {
        if (error) {
            alert(error);
            return;
        }
        console.log(data);
    });
    //response
    {"success":true}

Execute

    var dataApi = new af.data.Api("api/data");
    dataApi({
        "operation": "execute",
        "resourceName": "sstp_System_IsRoleMember",
        "timeout": 30, //optional
        "excludeFieldNames": false, //optional    
        "RoleName": "af_developer" //Parameter
    }, function(error, data) {
        if (error) {
            alert(error);
            return;
        }
        console.log(data);
    });    
    //response (excludeFieldNames:false)
    {"success":{"Table":[{"Column1":true}]}}
    //response (excludeFieldNames:true)
    {"success":[[{"Column1":true}]]}

.NET Example

    
    
    class Program
    {
        static void Main(string[] args)
        {
            MainAsync().GetAwaiter().GetResult();
        }

        static async Task MainAsync()
        {
            var baseAddress = new Uri("https://appframe-test.pims365.no/");
            var cookieContainer = new CookieContainer();

            using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
            using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
            {
                //Authenticate
                var content = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair<string, string>("username", ConfigurationManager.AppSettings.Get("username")),
                    new KeyValuePair<string, string>("password", ConfigurationManager.AppSettings.Get("password")),
                });
                                
                var authResponse = await client.PostAsync("/login", content);
                authResponse.EnsureSuccessStatusCode();

                //Retrieve data
                ApiRequestData requestData = new ApiRequestData()
                {
                    operation = "retrieve",
                    resourceName = "sviw_System_Persons",
                    maxRecords = 10,
                    fields = new List<Field>() {
                        new Field() { name = "ID" },
                        new Field() { name = "Name" }
                    }
                };

                var dataResponse = await client.PostAsync(baseAddress + "api/data", new StringContent(JsonConvert.SerializeObject(requestData), Encoding.UTF8, "application/json"));
                dataResponse.EnsureSuccessStatusCode();

                var responseData = dataResponse.Content.ReadAsStringAsync().Result;                               
            }
        }

        public class MasterChildCriteria
        {
        }

        public class Field
        {
            public string name { get; set; }
            public string type { get; set; }
            public bool nullable { get; set; }
            public int maxLength { get; set; }
            public bool computed { get; set; }
            public bool identity { get; set; }
            public bool hasDefault { get; set; }
            public string caption { get; set; }
            public object description { get; set; }
            public object aliasName { get; set; }
            public bool joined { get; set; }
            public bool ignored { get; set; }
            public bool excludeFromFilter { get; set; }
        }

        public class ApiRequestData
        {
            public string filterString { get; set; }
            public string whereClause { get; set; }
            public object filterObject { get; set; }
            public object whereObject { get; set; }
            public List<object> sortOrder { get; set; }
            public int maxRecords { get; set; }
            public bool distinctRows { get; set; }
            public MasterChildCriteria masterChildCriteria { get; set; }
            public int? skip { get; set; }
            public string operation { get; set; }
            public string resourceName { get; set; }
            public List<Field> fields { get; set; }
        }
    }
    

Related articles

Placeholder "LocalizeWeb2016" failed