Qlik Sense .Net SDK in compliance with the client (Part 1)

    Qlik Sense .Net SDK in compliance with the client (Part 1)

     


    Many .Net developers run into problems when creating Qlik Sense Apps with the .Net SDK. This document is intended to ease these problems. 

     

    Script

     

    An application created with the .Net SDK will not be able to show its content until it has data.
    To create and set a script and loading data see online help “
    Loading data into Qlik Sense”.

     

    Sheet

     

    A sheet without title will not appear in the client, it will also hide other sheets. The title can be set at creation :

     

    ISheet mySheet = application.CreateSheet("TestCreateSheet", new SheetProperties
    {

     

    MetaDef = new SheetMetaDataDef{Title = "My sheet title"}

     

    });

     

    Or after creation :

     

    using (mySheet.SuspendedLayout)

     

    {

     

    mySheet.Properties.MetaDef.Title = "My sheet title";

     

    }

     

    Note “mySheet.Properties.Rank” indicates the order in the sheetlist.

     

    Table

     

    The table has a hypercube object which must be set in order to work with the client. There are different scenarios for how to create a table.

     

    1. Empty, (setting dimensions and measures in the client or just setting them later).
      mySheet.CreateTable ("MyTable", new TableProperties

     

           {

     

                HyperCubeDef = new TableHyperCubeDef

     

                {

     

                  Dimensions = new List<HyperCubeDimensionDef>(),

     

                  Measures = new List<HyperCubeMeasureDef>()

     

                }

     

           });

     

     

     

    1. Using inline dimensions and measures
      mySheet.CreateTable ("MyTable", new TableProperties

     

           {

     

                HyperCubeDef = new TableHyperCubeDef

     

                {

     

                  Dimensions = new List<HyperCubeDimensionDef>

     

                  {

     

                    new HyperCubeDimensionDef

     

        {

     

          Def = new HyperCubeDimensionqDef {FieldDefs = new [] {"Dim1"} }

     

        }

     

                  },

     

                  Measures = new List<HyperCubeMeasureDef>

     

                  {

     

                    new HyperCubeMeasureDef

     

        {

     

          Def = new HyperCubeMeasureqDef { Def = "=Sum(Dim1)" }

     

        }

     

                  }

     

    }

     

           });


     

     

     

     

     

    1. Using library dimensions and measures
      var myDimension = myApp.CreateDimension(null, new DimensionProperties
      {

     

             MetaDef = new NxMetaDef(),
             Dim =
    new NxLibraryDimensionDef {FieldDefs = new[] { "Dim1" }},

     

             Name = "Dimension1",

     

             Title = "The Dimension1"

     

           });

     

           var myMeasure = myApp.CreateMeasure(null, new MeasureProperties

     

           {

     

             Measure = new NxLibraryMeasureDef {Def = "Sum(Dim1)", Label = "Sum Dim1"},

     

             Title = "Measure1"

     

           });

     


    mySheet.CreateTable (
    "MyTable", new TableProperties

     

           {

     

                HyperCubeDef = new TableHyperCubeDef

     

                {

     

                  Dimensions = new List<HyperCubeDimensionDef>

     

                  {

     

                    new HyperCubeDimensionDef { LibraryId = myDimension.Info.Id }

     

                  },                                                                         Measures = new List<HyperCubeMeasureDef>

     

      {

     

        new HyperCubeMeasureDef { LibraryId = myMeasure.Info.Id }

     

      }

     

    }

     

    });

     

     

     

    1. Or a combination of inline and library
      mySheet.CreateTable ("MyTable", new TableProperties

     

           {

     

                HyperCubeDef = new TableHyperCubeDef

     

    {

     

                  Dimensions = new List<HyperCubeDimensionDef>

     

                  {

     

                    new HyperCubeDimensionDef { LibraryId = myDimension.Info.Id },

     

                    new HyperCubeDimensionDef

     

                    {

     

                      Def = new HyperCubeDimensionqDef { FieldDefs = new [] {"Dim1"} }

     

                    }

     

                  },

     

                  Measures = new List<HyperCubeMeasureDef>

     

                  {

     

                    new HyperCubeMeasureDef { LibraryId = myMeasure.Info.Id },

     

                    new HyperCubeMeasureDef

     

                    {

     

                      Def = new HyperCubeMeasureqDef { Def = "=Sum(Dim1)" }

     

                    }

     

                  }

     

    }

     

           });

     

     

     

    Besides dimensions and measures there are some other properties that must be set these are SortCriterias, SortBy, InterColumnSortOrder, OtherTotalSpec, SuppressMissing (see Hypercube initialization) and columnOrder (see below):

     

    ColumnOrder, the column order indicates in which order they should be placed in the client.

     

    Here is a basic way of initializing ColumnOrder, note that def is a HypercubeDef.

     

    def.ColumnOrder = Enumerable.Range(0, def.Dimensions.Count() + def.Measures.Count());

     

     


     

     

     

    Hypercube initialization

     

     

     

    SortCriterias, defines the sorting criteria for the field in the dimension. A generic way of initializing SortCriterias:

     

    foreach (var dimension in hyperCubeDef.Dimensions)

     

    {

     

      if (!string.IsNullOrEmpty(dimension.LibraryId)) // is it a library

     

      {

     

        var dim = this.Session.App.GetDimension(dimension.LibraryId);

     

                   

     

        if (dim.DimInfos.First().Tags.Contains("$text") || dim.Tags.Contains("$ascii"))

     

          dimension.Def.SortCriterias.SortByAscii = SortDirection.Ascending;

     

        else if (dim.DimInfos.First().Tags.Contains("$numeric"))

     

          dimension.Def.SortCriterias.SortByNumeric = SortDirection.Ascending;

     

      }

     

      else

     

      {

     

        if (!dimension.Def.FieldDefs.First().Equals("null")) // is it inline

     

        {

     

          var f = this.Session.App.GetFieldDescription(nxDimension.Def.FieldDefs.First());

     

     

     

          if (f.Tags.Contains("$text") || f.Tags.Contains("$ascii"))

     

            dimension.Def.SortCriterias.SortByAscii = SortDirection.Ascending;

     

          else if (f.Tags.Contains("$numeric"))

     

            dimension.Def.SortCriterias.SortByNumeric = SortDirection.Ascending;

     

        }

     

      }
      if (
    dimension.Def.SortCriterias == null) // if not set a default sort criteria
      {
        dimension.Def.SortCriterias =
    new List<SortCriteria>
        {
         
    new SortCriteria { SortByLoadOrder = SortDirection.Ascending };
        };
      }
    }

     


    SortBy, define the sort criteria for the measure. A basic way of initializing “SortBy”:

     

     

     

    foreach (var measure in hyperCubeDef.Measures)

     

    {

     

      measure.SortBy = new SortCriteria

     

                {

     

                    SortByLoadOrder = SortDirection.Ascending,

     

                    SortByNumeric = SortDirection.Descending

     

                };

     

    }

     

     

     

    InterColumnSortOrder, order of the columns in the hypercube should be sorted. Example [1, 0, 2] means that the first column to be sorted should be column 1 followed by column 0 and last column 2. A generic way of initializing InterColumnSortOrder (def is a HypercubeDef):

     

    def.InterColumnSortOrder = Enumerable.Range(0, def.Dimensions.Count() + def.Measures.Count());

     

     

     

    OtherTotalSpec, defines if some values (grouped as others) should be grouped together in the visualization.  A basic way of initializing OtherTotalSpec (def is a HypercubeDef):

     

    foreach (var dimension in def.Dimensions)

     

    {

     

      dimension.OtherTotalSpec = new OtherTotalSpecProp()

     

      {

     

        OtherMode = OtherMode.OTHER_OFF,

     

        OtherCounted = new ValueExpr() { v = 10 },

     

        OtherLimit = new ValueExpr() { v = 0 },

     

        OtherLimitMode = OtherLimitMode.OTHER_LE_LIMIT

     

      };

     

    }

     

     

     

    SuppressMissing, set this to true which means no null values are returned.

    def.SuppressMissing = true;