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
   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
 
   Problem with the Clean Build Folders option in Application Architect
On occasion, it becomes necessary to clean out the build folders and deployed website prior to a build/de
Posted on Sep 16, 2014 by Jason Buss to Jason Buss' Blog
 
   Restricting Adding Opportunity Contacts to Contacts at the Opportunity's Account
 One thing that clients have asked for is how to restrict the selection of the Opportunity Contacts
Posted on Sep 09, 2014 by Kris Halsrud to Kris Halsrud's Blog
 
   How do I access the new Opportunity that I created in Saleslogix??
I had a user ask why they could not access a Salelogix opportunity when they clicked the hyperlink from t
Posted on Sep 04, 2014 by Dale Richter to SalesLogix Questions & Answers
 
   Configuring and Packaging secured actions in Saleslogix 8.0
Using Secured Actions, you can restrict access to user interface elements in the Saleslogix web client.
Posted on Aug 12, 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