Skip to main content
Announcements
Live today at 11 AM ET. Get your questions about Qlik Connect answered, or just listen in. SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Qlik Sense: Update a variable via API

Hi,

I'm trying to update a variable Definition using the NET SDK.

I loaded all the variables using the client as follows (I don't know if there's a better way):

Qlik.Sense.Client.IVariableList variables = Qlik.Sense.Client.AppExtensions.GetVariableList(qsApp);

I then try to change the values using:

qsVariable.Set<string>("Definition", kpv.Value.Definition);

But this does not work.

I am lost as to which API has the information and the actions I would need to do to achieve what I want.

Thanks in advance for your help.

8 Replies
petter
Partner - Champion III
Partner - Champion III

You don't show in your snippets above how you define and set the reference in the qsVariable. Furthermore I can see that I find a Method called Set - have a look at this list of methods:

https://help.qlik.com/sense/2.1/en-US/developer/#../Subsystems/APIs/Content/MashupAPI/qlik-variable-...

Not applicable
Author

I'm using a dictionary as follows:

if (pVariables != null && pVariables.Count > 0) {

                            foreach (KeyValuePair<string, NxVariableListItem> kpv in pVariables) {

                                NxVariableListItem qsVariable = kpv.Value;

                                  .......

I have tried the following:

qsVariable.Set<string>("Definition", kpv.Value.Definition);

Or

GenericVariable tmp = qsApp.GetVariableByName(kpv.Value.Name);

GenericVariableProperties tmpp = tmp.GetProperties();

tmpp.Set<string>("Definition", kpv.Value.Definition);

Neither works...

petter
Partner - Champion III
Partner - Champion III

When are you trying to update the definition of the variable? You can only do that before the application is published? Are you doing this on Qlik Sense Desktop?

Have a look at the documentation here to see how to create variables:

https://help.qlik.com/sense/2.1/en-US/developer/Subsystems/NetSDKAPI/Content/WorkWith/Net-Sdk-Work-W...

Not applicable
Author

I'm (trying to) updating the variables in the qvf file locally before publishing the file in QS Server via the API.

I have looked at the examples provided. Does that mean that I should destroy the variables I want to update and create again? Seems odd!

FYI, I am able to update Connections correctly before publish.

Øystein_Kolsrud
Employee
Employee

I would assume you would need to do something like this:

GenericVariable tmp = qsApp.GetVariableByName(kpv.Value.Name);

GenericVariableProperties tmpp = tmp.GetProperties();

tmpp.Definition = kpv.Value.Definition;

tmp.SetProperties(tmpp);

Notice that I do not use the generic Set method for the properties structure, but instead rely on the C# class property. Also, note that your have to do a "SetProperties" after you have modified the properties structure, otherwise the engine will never be informed that the properties has changed.

Øystein_Kolsrud
Employee
Employee

And a small note on the line with the generic "Set" method for setting the variable definition: The engine will actually look for a property named "qDefinition", not "Definition" when evaluating a variable. The C# property "Definition" of the "GenericVariableProperties" class maps to that engine property. Actually, these two operations should be equivalent in your case:

1) tmpp.Definition = kpv.Value.Definition;

2) tmpp.Set<string>("qDefinition", kpv.Value.Definition);

Note that the property name starts with a 'q' in version 2)! The engine uses that prefix to identify properties that are to be evaluated by the engine. (Other typical examples you will encounter are "qHyperCubeDef" and "qListObjectDef".)

Not applicable
Author

Still KO!

Maybe i'm doing something wrong?

In fact, I have a local file.

1. I connect to QS and upload the file using the REST api. (qrs/app/upload?name={0})

2. I get the AppId and change the variables as per your previous message.

3. I publish/replace the file (qrs/app/{0}/publish?stream={1} or qrs/app/{0}/replace?app={1})

When I check on the published app, the variable definition is unchanged...

Any ideas??

Not applicable
Author

It's now working in 2.2.

Cheers!