Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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
Sorry about the delay @Manish .
You can use this code as a start point, see picture and code.
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);
}
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.
Thanks for coming back to me.
so my API endpoint will look like below is that correct ?
https://{servername}/qrs/task/{taskid}/start;
Sorry about the delay @Manish .
You can use this code as a start point, see picture and code.
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);
}
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.
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:
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:
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.
You can also find another example at Qlik Rest SDK documentation:
qlik_rest_sdk/Qlik.Sense.RestClient/Examples/BasicConnectionCertificateFromFile/Program.cs at master...