简体   繁体   English

获取报告参数作为URL字符串

[英]Get report parameters as URL string

I have an application that serves SSRS reports. 我有一个提供SSRS报告的应用程序。 A given report is accessed in the following way: 可以通过以下方式访问给定的报告:

https://reportserver.com/reportname

Upon clicking View Report , a postback is submitted to the report server with the user-defined parameters. 单击“ 查看报告”后 ,将使用用户定义的参数将回发提交到报告服务器。 I need to grab these user-defined parameters and parse them as a URL string. 我需要获取这些用户定义的参数,并将其解析为URL字符串。

Desired result: https://reportserver.com/reportname?param1=foo&param2=bar 所需结果: https://reportserver.com/reportname?param1=foo&param2=bar https://reportserver.com/reportname?param1=foo&param2=bar param1 https://reportserver.com/reportname?param1=foo&param2=bar foo https://reportserver.com/reportname?param1=foo&param2=bar param2 https://reportserver.com/reportname?param1=foo&param2=bar

I found this doc that gets me close to what I need. 我发现此文档使我接近需要的文档 This method should allow me to grab all visible parameters and parse them myself, but I need hidden parameters as well. 这种方法应该允许我抓取所有可见参数并自己解析,但我也需要隐藏参数

How can I build this parameter string? 如何构建此参数字符串? We're using JavaScript/jQuery in the front end so it may be possible to grab this client-side before the POST, but I haven't found a way of doing this either. 我们在前端使用JavaScript / jQuery,因此有可能在POST之前抢占这个客户端,但是我也没有找到这样做的方法。

I've created URLs for reports with parameters 3 different ways. 我使用3种不同的方式为报告创建了URL。 A combination of the the first two may get you closer to solving your problem. 前两者的组合可能使您更接近解决问题的方法。

Use custom code in the report properties. 在报表属性中使用自定义代码。

Public Function ShowParameterValues(ByVal parameter As Parameter) As String  
Dim s as String = String.Empty 
    Try
       If parameter.IsMultiValue then  
          s = "Multivalue: "   
          For i as integer = 0 to parameter.Count-1  
             s = s + CStr(parameter.Value(i)) + " "   
          Next  
       Else  
          s = "Single value: " + CStr(parameter.Value)  
       End If  
       Return s  

    Catch ex As Exception
        Return "error"

    End Try

End Function  

OR 要么

Use a hyperlink in the report. 在报告中使用超链接。

=Globals!ReportServerUrl + "/ReportServer?" 
 + Replace(Globals!ReportFolder, " ", "+") + "%2f" 
 + Replace(Globals!ReportName, " ", "+") + "&rs:Command=Render"
 + "&boolean_value=" + CStr(Parameters!boolean_value.Value)
 + "&single_value_parameter=" + Parameters!single_value_parameter.Value 
 + "&multi_value_parameter=" + Join(Parameters!multi_value_parameter.Value, "&multi_value_parameter=") 
 + IIf(IsNothing(Parameters!week_date_start.Value), "&week_date_start:isnull=True", "&week_date_start=" & Format(Parameters!week_date_start.Value, Variables!FormatDate.Value))
 + IIf(IsNothing(Parameters!week_date_end.Value), "&week_date_end:isnull=True", "&week_date_end=" & Format(Parameters!week_date_end.Value, Variables!FormatDate.Value))

Also, I usually add this as a report variable and then you can have a standard textbox for the footer that doesn't have to change. 另外,我通常将此添加为报表变量,然后您可以为页脚添加一个标准文本框,而无需更改。

=Variables!UrlReportWithParameters.Value

OR 要么

Use the execution log. 使用执行日志。 Check out the column URL_Report_Filtered 查看列URL_Report_Filtered

--Purpose:  to search the reporting services execution log

DECLARE @all_value AS VARCHAR(10) = '<ALL>';
DECLARE @LogStatus AS VARCHAR(50) = '<ALL>';
DECLARE @ReportFolder AS VARCHAR(450) = 'Testing';
DECLARE @ReportName AS VARCHAR(450) = '<ALL>';
DECLARE @UserName AS VARCHAR(260) = '<ALL>';
DECLARE @GroupByColumn AS VARCHAR(50) = 'Report Folder';
DECLARE @StartDate AS DATETIME = NULL;
DECLARE @EndDate AS DATETIME = NULL;

WITH
report_users 
AS
(
    SELECT 
          [UserID]
        , [UserName]
        , [SimpleUserName] = UPPER(RIGHT([UserName], (LEN([UserName])-CHARINDEX('\',[UserName])))) 
    FROM 
        [dbo].[Users]
)
,
report_catalog
AS
(
    SELECT    
          rpt.[ItemID]
        , rpt.[CreatedById]
        , rpt.[ModifiedById]
        , rpt.[Type]
        , rpt.[Name] 
        , [ReportName] = rpt.[Name] 
        , rpt.[Description]
        , rpt.[Parameter]
        , [CreationDate] = CONVERT(DATETIME, CONVERT(VARCHAR(11), rpt.[CreationDate], 13))
        , [ModifiedDate] = CONVERT(DATETIME, CONVERT(VARCHAR(11), rpt.[ModifiedDate], 13))
        , [ReportFolder] = SUBSTRING(rpt.[Path], 2, LEN(rpt.[Path])-LEN(rpt.[Name])-2) 
        , rpt.[Path]
        , [URL_ReportFolder] = 'http://' + Host_Name() + '/Reports/Pages/Report.aspx?ItemPath=%2f'  + SUBSTRING(rpt.[Path], 2, LEN(rpt.[Path])-LEN(rpt.[Name])-2)  + '&ViewMode=List'
        , [URL_Report] = 'http://' + Host_Name() + '/Reports/Pages/Report.aspx?ItemPath=%2f'  + SUBSTRING(rpt.[Path], 2, LEN(rpt.[Path])-LEN(rpt.[Name])-2)  + '%2f' + rpt.[Name]
        , [ReportDefinition] = CONVERT(VARCHAR(MAX), CONVERT(VARBINARY(MAX), rpt.[Content]))  
        , [HostName] = Host_Name()
    FROM 
        [dbo].[Catalog] AS rpt
    WHERE 
        1=1
        AND rpt.[Type] = 2
)
SELECT 
    [GroupBy1] = 
        CASE  
            WHEN @GroupByColumn = 'Report Name' THEN rpt.[ReportName]
            WHEN @GroupByColumn = 'Report Folder' THEN rpt.[ReportFolder]
            WHEN @GroupByColumn = 'User Id' THEN usr.[SimpleUserName]
            ELSE '<N/A>' 
        END
    , rpt.[Path]
    , rpt.[ReportFolder]
    , rpt.[Name]
    , rpt.[URL_ReportFolder]
    , rpt.[URL_Report] 
    , [URL_Report_Filtered] = rpt.[URL_Report] + '&rs:Command=Render&' + CONVERT(VARCHAR(max), el.[Parameters])
    , [UserName] = usr.[SimpleUserName]
    , el.[Status]
    , el.[TimeStart]
    , el.[RowCount]
    , el.[ByteCount]
    , el.[Format]
    , el.[Parameters]
    , [TotalSeconds] = CONVERT(CHAR(8),DATEADD(ms,(el.[TimeDataRetrieval] + el.[TimeProcessing] + el.[TimeRendering]),0),108)
    , [TimeDataRetrieval] = CONVERT(CHAR(8),DATEADD(ms,el.[TimeDataRetrieval],0),108) 
    , [TimeProcessing] = CONVERT(CHAR(8),DATEADD(ms,el.[TimeProcessing],0),108)  
    , [TimeRendering] = CONVERT(CHAR(8),DATEADD(ms,el.[TimeRendering],0),108) 
    , [OrderbyDate] = CAST([TimeStart] AS DATETIME) 
FROM 
    report_catalog AS rpt 
    LEFT JOIN [dbo].[ExecutionLog] AS el ON el.[ReportID] = rpt.[ItemID]
    LEFT JOIN report_users AS usr ON el.[UserName] = usr.[UserName]
WHERE 
    1=1
    AND (@all_value IN(@LogStatus) OR el.[Status] IN(@LogStatus))
    AND (@all_value IN (@ReportFolder) OR rpt.[ReportFolder] IN(@ReportFolder))
    AND (@all_value IN(@ReportName) OR rpt.[ReportName] IN(@ReportName))
    AND (@all_value IN(@UserName) OR usr.[SimpleUserName] IN(@UserName))
    AND (@StartDate IS NULL OR CONVERT(DATETIME, CONVERT(VARCHAR(11), el.[TimeStart], 13)) >= @StartDate)
    AND (@EndDate IS NULL OR CONVERT(DATETIME, CONVERT(VARCHAR(11), el.[TimeStart], 13)) <= @EndDate)

I got it working. 我知道了 Fair warning: I'm new to ASP.NET so this is likely not an ideal solution. 合理的警告:我是ASP.NET的新手,所以这可能不是理想的解决方案。

I added an event handler to the report viewer control's code behind. 我在后面的报表查看器控件的代码中添加了一个事件处理程序。 This queries the execution log, grabbing the parameters selected most recently by the user. 这将查询执行日志,获取用户最近选择的参数。 It is meant to be triggered when a button called "Save Report" is clicked. 单击“保存报告”按钮时将触发该事件。 If you try to handle this with a Load or PreRender event handler it will fire before the row has a chance to insert into the database, giving you the result of the user's second most recent execution parameters. 如果尝试使用LoadPreRender事件处理程序来处理此问题,它将在该行有机会插入数据库之前PreRender ,从而为您提供用户第二次执行参数的结果。

Define the Button (.ascx file) 定义按钮(.ascx文件)

  <asp:LinkButton ID="SaveReportButton" runat="server" title="Save this Report"></asp:LinkButton>

Add event handler to code behind (.ascx.vb file) 将事件处理程序添加到后面的代码中(.ascx.vb文件)

Protected Sub SaveReportButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveReportButton.Click
  Dim conn As New SqlConnection(<connection string here>)
  Dim cmd As New SqlCommand("SELECT TOP 1 Parameters FROM [ReportServer].[dbo].[ExecutionLogStorage] WHERE <qualify on user, timestamp, etc. here>", conn)
  cmd.Parameters.AddWithValue(<query parameter here>)
  conn.Open()
  Dim result = cmd.ExecuteScalar()

  ' Prevents NullReferenceException from result.ToString() in case no result is found
  If (result IsNot Nothing)

    ' Redirect based on parameter string retrieved from log
    Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri & "?" & result.ToString())

  End If
  conn.Close()
End Sub

Call postback from JavaScript on button click 单击按钮从JavaScript调用回发

<li>
  <a href=\'javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(<reference SaveReportButton with appropriate arguments>)\' id="SaveReportButton" title="Save Report">
  Save Report
  </a>
</li>

Documentation on WebForm_DoPostBackWithOptions() and WebForm_PostBackOptions() is sparse, but a colleague has already done it this way so I followed suit for consistency's sake because it works. WebForm_DoPostBackWithOptions()WebForm_PostBackOptions()上的文档很稀疏,但是同事已经以这种方式完成了此操作,因此出于一致性的考虑 ,我遵循了它,因为它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM