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
   Saleslogix XBar Installation
When installing the new XBar application I found the instructions a little vague so I thought I would pas
Posted on Jul 18, 2014 by Mark Duthoy to SalesLogix Support
 
   Finding the Current Parent Record ID in SalesLogix Web
Sometimes it's the easiest things that can be a big barrier to entry for getting started with somet
Posted on Jun 27, 2014 by Ryan Farley to Ryan Farley's Blog
 
   SalesLogix 8.1 Job Service Scheduling Executions Error Resolution
Often times a web implementation will have an application server that is used to deploy the web portals t
Posted on Jun 20, 2014 by Mark Duthoy to SalesLogix Support
 
   Saleslogix 8.1 Web Access Tab Throws Null Reference Error
 I recently had a client experiencing a problem access the Contact's Web Access tab.  Whene
Posted on Jun 18, 2014 by Kris Halsrud to Kris Halsrud's Blog
 
   Debugging Custom Assemblies Using Visual Studio
Developing for Saleslogix web using external assemblies provides you with a lot of freedom when developin
Posted on Jun 10, 2014 by Jason Buss to Jason Buss' 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