Using user context data(from HttpContext) in the custom functions

Stimulsoft Reports.Net discussion
tayar
Posts: 30
Joined: Tue Sep 26, 2017 9:22 am

Re: Using user context data(from HttpContext) in the custom functions

Post by tayar » Mon Jun 17, 2019 11:44 am

Hi,
HighAley wrote:
Mon Jun 17, 2019 11:40 am
You could create the variable with your code and it will not be visible to the users.
I think this what I am missing. Can you please provide an example of this?

Thanks
HighAley
Posts: 8065
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Using user context data(from HttpContext) in the custom functions

Post by HighAley » Mon Jun 17, 2019 8:28 pm

Hello.

Our product is very complicated. One class could have many constructors.
As it it with the StiVariable class.
VariableConstructors.png
VariableConstructors.png (29.74 KiB) Viewed 391 times
You could use any of them.

Usually we suggest to create a variable that you need in the Designer and look at the code tab.
You will find necessary code there. Just add a necessary variable to the Dictionary.

Thank you.
tayar
Posts: 30
Joined: Tue Sep 26, 2017 9:22 am

Re: Using user context data(from HttpContext) in the custom functions

Post by tayar » Tue Jun 18, 2019 4:03 pm

HI,

Thanks. This is valuable. And now last thing. How do I get the value of this variable within a function which is registered using

Code: Select all

StiFunctions.AddFunction
Can you please give me an example?

Thanks
Vitaly
HighAley
Posts: 8065
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Using user context data(from HttpContext) in the custom functions

Post by HighAley » Tue Jun 18, 2019 7:27 pm

Hello.

You could call the variable from anywhere inside the report by its name.
We can't give you more detailed information because we don't see your function and the variable.

Thank you.
tayar
Posts: 30
Joined: Tue Sep 26, 2017 9:22 am

Re: Using user context data(from HttpContext) in the custom functions

Post by tayar » Wed Jun 19, 2019 7:12 am

Hi,

But, as explained, I can not use this variable in the report. I need it in a function.

Let's say I register a variable StiVariable("MyCustomVariable", "MyValue")

Can you please provide an example of a function which simply returns the value of this variable.

Thanks
tayar
Posts: 30
Joined: Tue Sep 26, 2017 9:22 am

Re: Using user context data(from HttpContext) in the custom functions

Post by tayar » Tue Jul 02, 2019 3:29 pm

Hi,

Would it be possible to get an example of a function as described in a message above?

Thanks
HighAley
Posts: 8065
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Using user context data(from HttpContext) in the custom functions

Post by HighAley » Tue Jul 02, 2019 5:39 pm

Hello.

Here is the custom function that returns the variable MyCustomVariable.
CustomFunction.png
CustomFunction.png (13.01 KiB) Viewed 316 times
Please, look at the attached report template.
CustomFunctionReport.mrt
(3.37 KiB) Downloaded 16 times

Thank you.
bra
Posts: 7
Joined: Tue Jul 09, 2019 11:23 am

Re: Using user context data(from HttpContext) in the custom functions

Post by bra » Mon Jul 15, 2019 1:31 pm

HighAley, I have copied your code in Visual Studio and it cannot find InitializeComponent method. Am I missing some using statement with reference to some namespace of extension methods?
Or you meant we need to derive StiReport class in every single report as a script?
Lech Kulikowski
Posts: 1871
Joined: Tue Mar 20, 2018 5:34 am

Re: Using user context data(from HttpContext) in the custom functions

Post by Lech Kulikowski » Mon Jul 15, 2019 8:47 pm

Hello,

Could you explain your issue in more details?

Thank you.
bra
Posts: 7
Joined: Tue Jul 09, 2019 11:23 am

Re: Using user context data(from HttpContext) in the custom functions

Post by bra » Tue Jul 16, 2019 12:35 pm

I need to implement function that must be available from a report and is able to receive a string parameter. Inside it I must have access to current report's object instance variables, get a variable of dictionary type use provided string parameter as an index of value in the dictionary and return the value.

Code: Select all

const string DIC_VAR_NAME = "custom_dictionary";

public string GetDictionaryValue(string key)
{
   // get current report instance
   var currentReportObject = ??? (don't know how to get the object, must be of type StiReport or probably it's descendant)

   // get dictionary from report
   if (!dictionaryObject.IsVariableExist(DIC_VAR_NAME)) return "";
   var dictionaryObject = currentReportObject.Variables[DIC_VAR_NAME];
   var dictionary = dictionaryObject as IDictionary<string, string>();
   if (dictionary == null) return "";

   return dictionary[key];
}
As you can see I cannot use static function because I need instance members (need to get current StiReport instance somehow).

So two questions:
1) how to register such a function in Report from code?
2) how to get current report object instance to be able to operate with variables that were assigned from code?
Post Reply