Wednesday, 8 July 2015

Add download button in Add form which download the file

Blackbaud CRM 
I had a scenario in which download functionality needs to be added on Add form. This download button takes File Name as input from the user. The same functionality like we have in normal windows application in which user open a save file dialogue box and then user put the name of file and then save the file. So we were facing issues to actually map the same sort of functionality in BlackBaud. So what I did to make a View form instead of Add form, reason being to actually no need for Save button and in Add form we don’t have any functionality to hide the OOB save button. So, I made a view form like below.

In above picture, File name is an input field, normally we have view only fields in view form so I changed this in HTML page in UI Model and replace the span with input tag. In VB class of UI Model, I needed to actually fetch the data from some data form through DataFormLoadRequest() and put that data into the text file. So, in UI Model, I coded the below lines behind the Export button event

Private Sub _export_InvokeAction(sender As Object, e As InvokeActionEventArgs) Handles _export.InvokeAction
        If String.IsNullOrEmpty(Me.MYEXPORTFILE.Value) Then 'Checking if MYEXPORTFILE Field is empty
            Dim prompt As New UIPrompt()
            prompt.ButtonStyle = UIPromptButtonStyle.Ok
            prompt.Text = "Empyt File Name is not allowed."
            prompt.ImageStyle = UIPromptImageStyle.Information
            Exit Sub
        End If

        If Not Me.MYEXPORTFILE.Value.Contains(".") Then
            Dim prompt As New UIPrompt()
            prompt.ButtonStyle = UIPromptButtonStyle.Ok
            prompt.Text = "File Name without Extension not allowed."
            prompt.ImageStyle = UIPromptImageStyle.Information
            Exit Sub
        End If

        Dim Filename_Array As String() = Me.MYEXPORTFILE.Value.Trim.Split(New Char() {"."c}) 'spliting MYEXPORTFILE Field value & creating Array of string
        Dim counter As Int32 = 1 'Counter to skip first part of Array in loop

        For Each fern As String In Filename_Array 'Loop on Array to get Extension part in Array
            If Not counter = 1 Then 'skiping first part of Array
                If Not (fern = "ext" Or fern = "txt") Then 'Checing extension of File
                    Dim prompt As New UIPrompt()
                    prompt.ButtonStyle = UIPromptButtonStyle.Ok
                    prompt.Text = "File Extension '" + fern + "' is not Allowed."
                    prompt.ImageStyle = UIPromptImageStyle.Information
                    Me.DownloadCustomPrompt("MYEXPORTKEY", Me.MYEXPORTFILE.Value)
                End If
            End If
            counter += 1
    End Sub

The above yellow highlighted line will call the below File download event of UI Model which will actually start the downloading and writes the data into the text file, see the below event source code

Private Sub ExportOMerViewDataFormUIModel_FileDownloadCustom(sender As Object, e As FileDownloadCustomEventArgs) Handles Me.FileDownloadCustom
        If e.Key = "MYEXPORTKEY" Then

            '''''' Lines are commented for OM project business

            Dim omerID As String

            'omerID = Me.ID.Value.ToString
            omerID = Me.RecordId

            If String.IsNullOrEmpty(omerID) Then
                Throw New Exception("OMer Not Found")
            End If
            Dim fileText = String.Empty

            Dim request As New DataFormLoadRequest()
            request.FormID = New Guid(OMEREXPORT_FORMID)
            request.RecordID = omerID
            request.SecurityContext = Me.GetRequestSecurityContext()

            Dim reply As DataFormLoadReply = Nothing
                reply = DataFormLoad(request, Me.GetRequestContext())
            Catch ex As Exception
                reply = Nothing
            End Try

            If (reply IsNot Nothing) Then
                Dim fieldValue As DataFormFieldValue = Nothing
                If reply.DataFormItem.TryGetValue("FILETEXT", fieldValue) Then
                        If fieldValue.Value IsNot Nothing AndAlso fieldValue.Value.ToString().Length > 0 Then
                            fileText = fieldValue.Value.ToString()
                        End If
                    End Try
                End If
            End If
            fileText = fileText.Replace(vbCrLf, vbLf)

            Dim file = fileText.Split(vbCrLf)
            If (Not fileText.Contains(vbCrLf)) Then
                file = fileText.Split(vbLf)
            End If
            Using sr As New IO.StreamWriter(e.Stream)
                For Each x In file
            End Using
        End If
    End Sub

The view data form Spec contains the below mentioned code

                @ID uniqueidentifier,
                @DATALOADED bit = 0 output,
  @MYEXPORTFILE varchar(max) = null output
                set nocount on;
                -- be sure to set this, in case the select returns no rows
                set @DATALOADED = 0;
                -- populate the output parameters, which correspond to fields on the form.  Note that
                -- we set @DATALOADED = 1 to indicate that the load was successful.  Otherwise, the system
                -- will display a "no data loaded" message.
                select @DATALOADED = 1
                return 0;

                <!-- describe fields on the form, which correspond to parameters on the SP.  Note that system paramters
                like the context @ID, @DATALOADED, and @CURRENTAPPUSERID need not be listed. -->
      <common:FormField FieldID="ID" Caption="ID" DataType="Guid" Hidden="true" />
      <common:FormField FieldID="MYEXPORTFILE" Caption="File Name" DataType="String" />

            <common:UIModel AssemblyName="Blackbaud.CustomFx.OMer.UIModel.dll" ClassName="Blackbaud.CustomFx.OMer.UIModel.ExportOMerViewDataFormUIModel" />
                <common:ExternalResource Url="browser/htmlforms/custom/OMER/ExportOMerViewDataForm.html" />

      <common:UIAction ActionID="EXPORT" Caption="Export" />

No comments:

Post a Comment