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
   Change the Appearance of a Saleslogix (Now Infor CRM) Read Only TextBox Web Control
In the Salelogix web client, a read only control has a css style applied to it to make the background app
Posted on Oct 24, 2014 by Kris Halsrud to Kris Halsrud's Blog
 
   Saleslogix (now Infor CRM) Web Client- Changing Label Appearances on Quick Forms
 I wrote previously about setting a label's appearance on a quick form using server side code.&n
Posted on Oct 02, 2014 by Kris Halsrud to Kris Halsrud's Blog
 
   Error Attempting to Save an Entity: "E_FAIL(0x80004005)" in the Infor CRM (Saleslogix) Web Client
We recently had a client that was running into an issue when attempting to create one particular entity i
Posted on Sep 30, 2014 by Kris Halsrud to Kris Halsrud's Blog
 
   Video: Demystifying Infor CRM (Saleslogix) Updates
Watch it now! If you missed yesterdays demo, "Demystifying Infor CRM (Saleslogix) Updates" ,
Posted on Sep 26, 2014 by Brianna Ojard to The Inbox
 
   Official Infor CRM Acquisition FAQ
What to expect now that Saleslogix is Infor CRM. Like many of you, it will take some time (probably a
Posted on Sep 18, 2014 by Brianna Ojard to The Inbox
 
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