Login / Register  search  syndication  about

          Jason Buss' Blog

Jason Buss on SalesLogix development & customization, SQL, and more.

Exporting Table data via the SalesLogix web client

Recently, I had a request to create export functionality for a datagrid in the SalesLogix web client.   I had a little difficulty exporting data from the actual grid, but as an alternative, we put together functionality to export data based on a provided SQL query. (Thanks to Ryan for working out the SmartPart code)

By creating a new custom smartpart, we can simply export the results of a SQL query to a CSV file, save it on the webserver and download it to an individual client.

First, you'll want to create a folder on the web server to hold the exported file.  In this case, I'm exporting contact data based on the current account, so I created an export folder under the SupportFiles/SmartParts/Account folder in the portal manager.

 

Once we have a location for our file, we need to add the SmartPart files.  I've attached the files I created to this post.  They can be added by right-clicking on the entity folder and selecting "Add Existing Files..."

 

The page I created simply pulls in a few different contact table fields.  This can be easily modified.  On the code page for the Smart Part, there are a couple functions you'll need to understand in order to customize this to pull in the data you want.  First of all, the CreateFile function is where you define the SQL script and fields you would like to return.

 private void CreateFile(string file)
{
    //First, we're checking to see if the file already exists, deleting it if found.
    if (File.Exists(file))
        File.Delete(file);
    using (var writer = new StreamWriter(file, false))
    {
        //This line defines the column headers in the CSV file.
        writer.WriteLine("\"ContactID\",\"FirstName\",\"LastName\",\"Workphone\"");
        using (var conn = new OleDbConnection(ConnectionString))
        {
            conn.Open();
            //Using an OleDBCommand, we pass the query defining the data we want to return.  For this example, 
            //we're just returning data from the contact table where the accountid is what is passed when calling the page.
            using (var cmd = new OleDbCommand(string.Format("SELECT * FROM CONTACT WHERE ACCOUNTID = '{0}'", AccountId), conn))
            {
                //Now we create a reader object to loop through the returned records.
                var reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                while (reader.Read())
                {
                    //Finally, we're defining the fields from the Database to return.  Obviously, these should match the column headers previously defined.
                    writer.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\"", reader["CONTACTID"], reader["FIRSTNAME"], reader["LASTNAME"], reader["WORKPHONE"]));
                }
            }
        }
    }
}

The Page load event calls the CreateFile function and handles pushing the CSV file to the requesting user.  Also, you could modify this to create other types of files, such as an excel spreadsheet.  We chose a CSV in this case to reduce potential overhead on the webserver.

 protected void Page_Load(object sender, EventArgs e)
{
    //The page_load event calls the CreateFile function, saving it to the Export folder 
    //and pushes it to the requesting user.  The file is saved on the server with the
    //name of (userid).csv.  This way there will only be as many files saved on the 
    //server as there are users in SalesLogix.
            
    var file = string.Format("{0}\\SmartParts\\Account\\Export\\{1}.csv", GetSlxClientBaseUrl(), UserId);
    CreateFile(file);
    Response.Buffer = false;
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = "application/csv";
    //This line defines the file name in the Save dialog for the user.  In this case, (accountid)-contacts.csv.
    Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}-Contacts.csv", AccountId));
    Response.AppendHeader("Content-Length", (new FileInfo(file).Length.ToString()));
    //Finally, the file is transmitted to the end-user.
    Response.TransmitFile(file);
    Response.End();
}

Now that the SmartPart is set up, we just need some code to navigate to the page and pass the current accountID.  You can launch this from pretty much anywhere.  Since I'm calling this from within the account entity, I added a button to call a C# code Snippet.  The code is pretty simple:

 var account = this.BindingSource.Current as Sage.Entity.Interfaces.IAccount;
Response.Redirect(string.Format("/SlxClient/SmartParts/Account/TestExport.aspx?accountid={0}", account.Id));

As you can see, I'm getting the current accountid from the current binding source, and performing a redirect to the SmartPart while appending the accountID.

That's all there is to it.   With this in place, your end users can simply push a button and download a file containing any data that you can define in a SQL query.

I hope you find this helpful.  Thanks for reading!

 

  Attachment: ExportData.zip

What's This?
  
Bookmark and Share

About Jason Buss

   Jason is a senior application developer with Customer FX.



Related Content
   Easily Showing and Hiding Tabs at Runtime in Infor CRM (Saleslogix)
Showing and hiding tabs in the Infor CRM (Saleslogix) client should be an easy thing. It should be someth
Posted on Dec 16, 2014 by Ryan Farley to Ryan Farley's Blog
 
   Telnet Sample to Send an SMTP email
I am always forgetting how to test sending an email through an SMTP gateway using the Telnet client. 
Posted on Dec 12, 2014 by Kris Halsrud to Kris Halsrud's Blog
 
   Problem setting SLXTextbox enabled property / Setting unexposed base control properties for a custom control
I recently had an issue with a Quickform where I was attempting to set the Enabled property of a TextBox
Posted on Dec 05, 2014 by Jason Buss to Jason Buss' Blog
 
   Checking Infor CRM (Saleslogix) Picklists for valid data
This week I received a question about a particular picklist. One of the picklist items, when chosen, wou
Posted on Dec 05, 2014 by Dale Richter to Infor CRM Questions & Answers
 
   Avoiding the Dirty Data Message When Programatically Redirecting to a Record in Infor CRM (Saleslogix)
In my last post I wrote about using the OnClientClick property of a control (or button) to run JavaScript
Posted on Dec 02, 2014 by Ryan Farley to Ryan Farley's Blog
 
Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add
All contents Copyright © 2014 Customer FX Corporation
Customer FX Corporation
2324 University Avenue West, Suite 115
Saint Paul, Minnesota 55114
Tel: 800.728.5783

  Follow @CustomerFX on twitter
Follow the best news, tips, and articles
  Subscribe to Customer FX on youtube
Watch SalesLogix tutorial videos from Customer FX
Login / Register