Tuesday, October 16, 2012

Retrieve default and available values of Report Parameters using C# : ReportingService2010

I have been playing around with Reporting Services Web service ReportingService2010 for some time and I got really surprised from the things that we can do using this web service. Today I am going to write about retrieving the default and available values of a report parameter using C#. Sometimes back I wrote a post about Accessing Report Server using Report Server Web Service - Microsoft SQL Server 2008R2. So I am not going to write a more detailed post here.

Without much information, I will just paste the code down. Value retrieving part is in bold.
        /// <summary>
        /// pName is the parameter I want to get the default and available values for
        /// </summary>
        /// <param name="pName"></param>
        private void GetDefaultAndAvailableValues(string pName)
            //credentials to connec to the web service
            var userName = "username";
            var password = "password";

            //connecting to web service
            var clientCredentials = new NetworkCredential(userName, password);
            var rs = new ReportingService2010SoapClient();
            if (rs.ClientCredentials != null)
                rs.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
                rs.ClientCredentials.Windows.ClientCredential = clientCredentials;

            //open the connection

            var oTrustedUserHeader = new TrustedUserHeader();
            string historyId = null;
            ParameterValue[] values = null;
            DataSourceCredentials[] credentials = null;
            ItemParameter[] itemParameter = null;

            //all the parameters of the report will be filled to out parameter 'itemParameter'
            rs.GetItemParameters(oTrustedUserHeader, "/MyReports/Dev/MyReport", historyId, true, values, credentials, out itemParameter);

            //iterate through parameters
            foreach (ItemParameter ip in itemParameter)
                //check for the parameter I want to get default and available values for
                if (pName == ip.Name)
                    //get default values
                    string[] defValues = ip.DefaultValues;

                    //get valid/available values
                    ValidValue[] validValues = ip.ValidValues;
                    foreach (ValidValue vValue in validValues)
                        //ValidValue has Label property and Value property
                        //if you check the parameters in report designer, every parameter has Label field and Value field
                        ddlParamValues.Items.Add(new ListItem(vValue.Label,vValue.Value));
            //close the connection

And this is the output I am getting.
Hope this helps.

Happy Coding.