QlikView Management API - #2 Export / Add / Delete Document CALs

    Introduction

     

    This document is part of a series of documents that discuss how to use the QlikView Management API. You can find the index for these documents Here.

    Please review the getting started document before continuing.

     

    In this example we will review ways that the API can be used to work with document CALs with a couple of examples attached.  Document CALs are a cost effective way to provide limited access to users to just one or two documents on a QlikView server, in larger deployments these can be time consuming to manage and so the Management API can help with this.

     

    Note - Under the terms of the QlikView Licence Agreement, Document CALs are intended to be persistant for a user  i.e. they should not be shared or deleted unless that user leaves an organisation or role.

     

    Working with Document CALs

    Document CALs are allocated for each QVW file and the allocation is stored in Meta data on the QlikView sever.  Using the API this meta data can be consumed and edited to export/add/delete document CALs.  In these examples we will mainly be working with the DocumentNode, DocumentMetaData and ,AssignedNamedCAL objects.

     

    First off all you need to connect to the management API and handle the Service Key process:

     

    QMSClient Client;

    string QMS = "http://localhost:4799/QMS/Service";

    Client = new QMSClient("BasicHttpBinding_IQMS", QMS);

    string key = Client.GetTimeLimitedServiceKey();

    ServiceKeyClientMessageInspector.ServiceKey = key;

     

    Exporting Document CAL Data

     

    To begin we need to reference the QlikView server we want to connect to so we can read its settings.  ServiceInfo is an array of all the services of a given type, so if you had more than one QV Server you would need to use the right one, for now we will assume its just one.

     

    ServiceInfo[] MyQVS = Client.GetServices(ServiceTypes.QlikViewServer);


    Now we need to find a list of all of the QlikView documents on the server, the DocumentNode object contains and array of all the documents on the server. As we want to get thier meta data soon we can create somewhere for that to go.

     

    DocumentNode[] allDocs = Client.GetUserDocuments(MyQVS[0].ID);

    DocumentMetaData Meta;

     

    Once we have a list of documents we can loop through each document and extract the document Meta data. When doing this we can decide the scope of the data and in this instance we want licence data.   Now the meta data is extracted it is possible to extract the AssignedNamedCAL data from it.  This is an array of all the current CALs for a particular document containing information like the username and the last time they used the CAL.  This is the data we can now extract for each document, in this simple example the code prints the data to screen but it is can be changed to export to a file or keep the data for further processing.

     

     

    for (int i = 0; i < allDocs.Length; i++)

    {

    Meta = Client.GetDocumentMetaData(allDocs[i], DocumentMetaDataScope.Licensing);

    List<AssignedNamedCAL> currentCALs = Meta.Licensing.AssignedCALs.ToList();

    for (int a = 0; a < currentCALs.Count; a++)

    {

    Console.WriteLine(Meta.UserDocument.Name + ", " + currentCALs[a].UserName + ", " + currentCALs[a].LastUsed);

    }

    }

     

     

    Adding a Document CAL

     

    To add a document CAL we use the same approach as above to obtain the existing meta data but this time we modify it and save it back to the server.  So start the same way.

     

    ServiceInfo[] MyQVS = Client.GetServices(ServiceTypes.QlikViewServer);

    DocumentNode[] allDocs = Client.GetUserDocuments(MyQVS[0].ID);

    DocumentMetaData Meta;

     

    Create a couple of strings to hold the name of the QVW we want to work with and the name of the user to add a CAL for.

     

    string document = "Movies Database.qvw";

    string user = "Fred";

     

    There is not a "Find document" function in the API and so we need to loop through the DocumentNode array to find the file we want to use.  Once we find it, we extract its meta data and its list of assigned CALs as before.  Now we create a new AssignedNamedCAL and add the name of the user to it.  This new object is added to the existing list, pushed back into the meta data and finally the new data is saved back to the server.

     

    for (int i = 0; i < allDocs.Length; i++)

    {

    if (allDocs[i].Name == document)  //If the document matches the one im looking for then apply the changes

    {

    //Get the meta data for the current document

    Meta = Client.GetDocumentMetaData(allDocs[i], DocumentMetaDataScope.Licensing);

     

    //Extract the current list of Document CALs

    List<AssignedNamedCAL> currentCALs = Meta.Licensing.AssignedCALs.ToList();

     

    //Create a new CAL object and set the username for it

    AssignedNamedCAL newCAL = new AssignedNamedCAL();

    newCAL.UserName = user;

     

    //Add the new cal to the list of CALs object

    currentCALs.Add(newCAL);

     

    //Add the updated CALs list back to the meta data object

    Meta.Licensing.AssignedCALs = currentCALs.ToArray();

     

    //Save the metadata back to the server

    Client.SaveDocumentMetaData(Meta);

    }

    }

     

     

    Deleteing a Document CALs

    To remove a document CAL we use the same approach as above

     

    ServiceInfo[] MyQVS = Client.GetServices(ServiceTypes.QlikViewServer);

    DocumentNode[] allDocs = Client.GetUserDocuments(MyQVS[0].ID);

    DocumentMetaData Meta;

     

    Create a couple of strings to hold the name of the QVW we want to work with and the name of the user to remove a CAL for.

     

    string document = "Movies Database.qvw";

    string user = "Fred";

     

    This time we loop through the document array to find the document and then loop through the CALs to find the person to remove.  Once we remove the entry, we save the meta data back to the server as before.

     

    for (int i = 0; i < allDocs.Length; i++)  //Loop through each document

    {

    if (allDocs[i].Name == document)

    {

    //Get the meta data for the current document

    Meta = Client.GetDocumentMetaData(allDocs[i], DocumentMetaDataScope.Licensing);

     

    //Extract the current list of Document CALs

    List<AssignedNamedCAL> currentCALs = Meta.Licensing.AssignedCALs.ToList();

     

    for (int x = 0; x < currentCALs.Count; x++)

    {

    //If the user matches the name then remove it from the list

    if(currentCALs[x].UserName == user)

    currentCALs.Remove(currentCALs[x]);

    }


    //Add the updated CALs list back to the meta data object

    Meta.Licensing.AssignedCALs = currentCALs.ToArray();

    //Save the metadata back to the server

    Client.SaveDocumentMetaData(Meta);

    }

    }

     

     

    Other Items

    The Document Meta Data object contains a range of information for the document, the licencing component of this can also be used to view/change the the total number of document CALs in use or allocated to a specific document.