Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
Manish
Creator
Creator

Trigger Qlik Sense Task from .Net SDK

Hi 

I am trying to trigger to Qlik Sense task from c# .net code. Is there any sample code or any useful links which can help me get started?

 

Many thanks

Labels (2)
1 Solution

Accepted Solutions
marksouzacosta
Partner - Specialist
Partner - Specialist

Sorry about the delay @Manish .

You can use this code as a start point, see picture and code.

marksouzacosta_0-1715719276065.png

 

using Newtonsoft.Json.Linq;
using Qlik.Sense.RestClient;
using System.Net;
using System.Security;

string qlikSenseUrl = "";
string userName = "";
string userDomain = "";
string userPassword = "";

SecureString securePassword = new SecureString();
foreach (char c in userPassword)
{
securePassword.AppendChar(c);
}

try
{
Console.WriteLine("Starting Qlik Sense Connection");
var restClient = new RestClient(qlikSenseUrl);
restClient.AsNtlmUserViaProxy(new NetworkCredential(userName, securePassword, userDomain), certificateValidation: false);

string taskId = "b40bb9b1-a437-4554-9906-c21c8121a205";

var executeTask = $"/qrs/task/{taskId}/start";
var rsp = await restClient.PostAsync<JObject>(executeTask, string.Empty);

Console.WriteLine("Connection stabilished");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Read more at Data Voyagers - datavoyagers.net

View solution in original post

6 Replies
marksouzacosta
Partner - Specialist
Partner - Specialist

Hi @Manish, I believe you are talking about Qlik Sense on-prem, not Qlik Cloud.

If that is the case, you have to use the QRS API:

The End Point you are looking for is this one:
Open API specification for Repository Main API − post /task/{id}/start | Qlik Sense for developers H...

To call the QRS API End Points I highly recommend the following Nuget Package:
kolsrud/qlik_rest_sdk: SDK for accessing the Qlik Sense REST APIs (github.com)

The hardest part to figure out will be probably the Authentication because there are multiple ways to do that. 

Note: I think the .NET SDK does not support the QRS API, meaning, you will not be able to access Tasks from it.

Read more at Data Voyagers - datavoyagers.net
Manish
Creator
Creator
Author

Hi @marksouzacosta 

Thanks for coming back to me.

so my API endpoint will look like below is that correct ?

https://{servername}/qrs/task/{taskid}/start;

 

marksouzacosta
Partner - Specialist
Partner - Specialist

Sorry about the delay @Manish .

You can use this code as a start point, see picture and code.

marksouzacosta_0-1715719276065.png

 

using Newtonsoft.Json.Linq;
using Qlik.Sense.RestClient;
using System.Net;
using System.Security;

string qlikSenseUrl = "";
string userName = "";
string userDomain = "";
string userPassword = "";

SecureString securePassword = new SecureString();
foreach (char c in userPassword)
{
securePassword.AppendChar(c);
}

try
{
Console.WriteLine("Starting Qlik Sense Connection");
var restClient = new RestClient(qlikSenseUrl);
restClient.AsNtlmUserViaProxy(new NetworkCredential(userName, securePassword, userDomain), certificateValidation: false);

string taskId = "b40bb9b1-a437-4554-9906-c21c8121a205";

var executeTask = $"/qrs/task/{taskId}/start";
var rsp = await restClient.PostAsync<JObject>(executeTask, string.Empty);

Console.WriteLine("Connection stabilished");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Read more at Data Voyagers - datavoyagers.net
Manish
Creator
Creator
Author

Thanks @marksouzacosta . It worked. Sorry but can I be a pedant and ask you to please help me the authentication code as well? Actually I don't want to use password to connect probably something with XrefKey would help or any other way you think would be best.

Looking forward to your response.

marksouzacosta
Partner - Specialist
Partner - Specialist

In this case, you can use Certificate Authentication - that is actually my favorite one.

The first step is to create a Certificate File. It will look trick in a first glance, but it is not hard as it looks. I found a Community topic showing how to create a Certificate on QMC: How to configure Certificate Authentication for Ql... - Qlik Community - 1799913

Now to the code. I have created two procedures to populate the RestClient depending on the parameters I have received from my appsettings.json. The code is not complete but I think it will give you the idea how you can use the Certificate Files with or without passwords:

marksouzacosta_0-1715777949497.png

private void ConnectAsDirectConnection(RestClient restClient)
{
System.Security.Cryptography.X509Certificates.X509Certificate2Collection certs;
if (string.IsNullOrEmpty(CertificatePassword))
{
Log.Debug("Certificate without Password");
certs = RestClient.LoadCertificateFromDirectory(CertificatePath);
}
else
{
Log.Debug("Certificate with Password");
certs = RestClient.LoadCertificateFromDirectory(CertificatePath, SecureString(CertificatePassword));
}
restClient.AsDirectConnection(UserDomain,UserName,CertificatePort, CertificateValidation, certs);
}
private void ConnectAsNtlmUserViaProxy(RestClient restClient)
{
Log.Debug("User Credentials");
restClient.AsNtlmUserViaProxy(new NetworkCredential(UserName, SecurePassword, UserDomain), certificateValidation: false);
}
private SecureString SecureString(string str)
{
var securePassword = new SecureString();
foreach (var c in str.ToCharArray())
{
securePassword.AppendChar(c);
}
return securePassword;
}

This is my appsettings.json populated to connect using Certificate:

marksouzacosta_1-1715778166907.png

Note that the CertificatePath can be a local Windows folder or a network folder.

Please let me know if that covers what you are looking for.

Read more at Data Voyagers - datavoyagers.net
marksouzacosta
Partner - Specialist
Partner - Specialist

You can also find another example at Qlik Rest SDK documentation:
qlik_rest_sdk/Qlik.Sense.RestClient/Examples/BasicConnectionCertificateFromFile/Program.cs at master...

Read more at Data Voyagers - datavoyagers.net