Logo
CAR REVIEW
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
PREGNANCY
 
 
Windows Server

Microsoft Dynamics AX 2009 : Working with Data in Forms - Adding images to records

8/26/2012 5:40:48 PM
Dynamics AX can store images to be used along with data. Images could be used for various purposes, like a company logo that is displayed in every printed document, employee photos, inventory images, and so on. One of the most convenient ways to attach images to any record is to use the Document handling feature of Dynamics AX. This does not require any changes in the application. But Document handling is a very generic way of attaching files to any record and might not be suitable for specific circumstances.

Another way of attaching images to records could be to utilize standard application objects, though minor application changes are required. The company logo in the Company information form is one of the places where an image is stored in exactly this way.

In this recipe, we will explore the latter option. As an example, we will add the ability to store an image for each inventory item. Although inventory items already have a similar option, which is a part of the Enterprise Portal Framework, it is very inventory specific and unlike our example cannot be used anywhere else.

How to do it...

  1. 1. Open the InventTable form in AOT and add a new MenuItemButton control to the bottom of its ButtonGroup with the following properties:

    Property Value
    Name CompanyImage
    MenuItemType Display
    MenuItemName CompanyImage
    Text Item image
    DataSource InventTable

  1. 2. Open Inventory management | Item Details and notice a new Item image button on the right:

  1. 3. Click on the button and then use the Change button to upload a new image for the selected item:

  2. 4. The Remove button could be used to delete an existing image.

How it works...

There are only three standard Dynamics AX objects involved here:

  1. 1. The CompanyImage table, which holds image data and the information about the record the image is attached to. The separate table allows easily hooking image functionality to any other existing table without modifying it or decreasing its performance.

  2. 2. The CompanyImage form, which shows an image and allows modifying it.

  3. 3. The Display menu item CompanyImage, which opens the form.

We only need to add the menu item to any form, like InventTable in our example and set its DataSource property against which we want to store an image. The rest is done by the application.

There's more...

The following two topics will explain how a stored image could be displayed as a new tab page on the main form and how a stored image could be saved back to file.

Displaying an image as a part of a form

Attaching an image to a record sometimes is not enough. I noticed that sometimes users find that clicking the button to see an image is not as convenient as having it on the same form. So here we will extend our recipe by displaying the stored image on a new tab page.

First, we need to add a new tab page to the end of the InventTable form's Tab control. This is where our image will be displayed. Set the tab page properties:

Property Value
Name Image
Caption Image

Add a new Window type control to the tab page. This control will be used for displaying the image. Set its properties:

Property Value
Name ItemImage
AutoDeclaration Yes
Height Column height
Width Column width

By setting Height and Width to Column height and Column width respectively, we ensure that the image control takes all possible space.

Add a new method to the InventTable form:

void loadImage()

{
Image img;
CompanyImage companyImage;
;
companyImage = CompanyImage::find(
InventTable.dataAreaId,
InventTable.TableId,
InventTable.RecId);
if (companyImage.Image)
imagesimagesdisplaying, as part of form{
img = new Image();
img.setData(companyImage.Image);
ItemImage.image(img);
}
else
{
ItemImage.image(null);
}
}

This method first finds a CompanyImage record, which is attached to the current record and then displays the binary data using the ItemImage control. If no image is attached the Window control is cleared to display empty space.

Override pageActivated() of the newly created tab page with:

public void pageActivated()

{;
super();
element.loadImage();
}

In here, we simply call the previously created method once the user selects the image tab page.

The form should look like following in AOT:

Now open Item Details from Inventory management and note the new tab page with the same image displayed:

Saving a stored image as a file

A couple of times in my practice it so happened that people lost images loaded into the system. Standard Dynamics AX does not provide a way of exporting loaded images. The images could be saved as files by creating and running a small job, which uses the Image class for image processing. In this topic, we will go a little beyond that and we will add a new button Save as to the Image form, which will allow us to save the loaded image as a file.

Let's add a new Button control to the CompanyImage form's ButtonGroup with the following properties:

Property Value
Name SaveAs
Text Save as

Create a new method on the form:

void saveImage()

{
Image img;
Filename name;
str type;
#File
;
if (!imageContainer)
{
return;
}
img = new Image();
img.setData(imageContainer);
type = '.'+strlwr(enum2value(img.saveType()));
name = WinAPI::getSaveFileName(
element.hWnd(),
[WinAPI::fileType(type),#AllFilesName+type],
'',
'');
if (name)
{
img.saveImage(name);
imagesimagessaving, as file}
}


					  

This method will present the user with Save As dialog allowing choosing of the desired file name for saving the current image. Note that imageContainer is a form variable, which holds image data. If it is empty, it means there is no image attached and we do not run any of the code. We also check the loaded file type to make sure that our Save As dialog filters only files of that type.

Override the button's clicked() with the following code to make sure saveImage() is executed once the user clicks the button:

void clicked()

{;
super();
element.saveImage();
}

In AOT the form should look like this:

Now when you open the Item image form a new button Save as is available:

Use this button to save the item image as a file:

The CompanyImage form is used system wide, so note that this button is now available system wide too.
Other -----------------
- Microsoft Dynamics AX 2009 : Working with Data in Forms - Coloring records
- SQL Server 2008 : Data Collector and MDW - Custom collection sets
- SQL Server 2008 : Physical server design - Server consolidation and virtualization
- Windows Server 2008 Server Core : Determining File and Directory Status with the ChkDsk Utility, Performing Boot Time Disk Checks with the ChkNTFS Utility
- Windows Server 2008 Server Core : Changing File and Directory Attributes with the Attrib Utility
- SQL Server 2008 R2 : Creating and Managing Databases - Creating Databases
- SQL Server 2008 R2 : Creating and Managing Databases - Database Files
- InfoPath with Microsoft Content Management Server Web Services : Creating the InfoPath Document
- InfoPath with Microsoft Content Management Server Web Services : Creating the ASP.NET Web Service
- Active Directory Domain Services 2008 : Configuring Attributes to Be Indexed for Containerized Searches, Configuring Attributes Not to Be Indexed for Containerized Searches
- Active Directory Domain Services 2008 : Configure Attributes to Be Copied When Duplicating Users, Configure Attributes Not to Be Copied When Duplicating Users
- Windows Server 2008 Server Core : Associating a Folder to a Drive with the Subst Utility, Displaying a Directory Structure with the Tree Utility, Managing the Volume Shadow Service with the VSSAdmin U
- Microsoft Dynamic GP 2010 : Module Setup - Inventory (part 3) - Inventory Item Setup
- Microsoft Dynamic GP 2010 : Module Setup - Inventory (part 2) - Item class setup
- Microsoft Dynamic GP 2010 : Module Setup - Inventory (part 1) - Inventory Control Setup, Inventory sites, Unit of Measure Schedules
- Exchange Server 2007 : Designing Exchange Infrastructure, Integrating Client Access into Exchange Server 2007 Design
- Exchange Server 2007 : Designing Exchange Server Roles in an Exchange Environment
- Microsoft Dynamics CRM 4.0 : Infrastructure Design Considerations - Outlook Infrastructure Optimization
- Microsoft Dynamics CRM 4.0 : Infrastructure Design Considerations - Office Communication Server Setup
- BizTalk 2009 : Implementing a Scatter or Gather Pattern
 
 
Most view of day
- Microsoft Word 2010 : Viewing a Document’s Statistics, Evaluating a Document’s Readability
- Maintaining Dynamics GP : Maintaining updated code by rolling out Service Packs with Client Updates
- Maintaining Desktop Health : Monitoring Reliability and Performance (part 2)
- SQL Server 2012 : Running SQL Server in A Virtual Environment - COMMON VIRTUALIZATION PRODUCTS
- Windows Server 2003 : Protecting Hosts with Windows Host Firewalls - Firewall Basics
- Microsoft OneNote 2010 : Doing Research with Side Notes (part 1) - Creating Side Notes
- Microsoft Lync Server 2013 : Administration of the Director Role (part 4) - Services Management, Client Version Filter
- Windows Server 2003 : Protecting Hosts with Windows Host Firewalls - Protocol Filters
- Windows Server 2003 on HP ProLiant Servers : Security Planning and Design (part 3) - Microsoft Software Update Service and Windows Update Service
- Microsoft Visio 2010 : Introducing Data Graphics (part 2) - Creating Data Graphics,Applying Data Graphics to Shapes
Top 10
- Microsoft Lync Server 2013 : Director Troubleshooting (part 3) - Synthetic Transactions,Telnet
- Microsoft Lync Server 2013 : Director Troubleshooting (part 2) - DNS Records, Logs
- Microsoft Lync Server 2013 : Director Troubleshooting (part 1) - Redirects, Certificates
- Microsoft Lync Server 2013 : Administration of the Director Role (part 4) - Services Management, Client Version Filter
- Microsoft Lync Server 2013 : Administration of the Director Role (part 3) - Topology Status
- Microsoft Lync Server 2013 : Administration of the Director Role (part 2) - Ports,Firewall Rules
- Microsoft Lync Server 2013 : Administration of the Director Role (part 1) - Services
- Microsoft Lync Server 2013 : Configuring the Director (part 2) - Web Services Ports,Reverse Proxy
- Microsoft Lync Server 2013 : Configuring the Director (part 1) - SRV Records, Web Services FQDN Overrides
- Sharepoint 2013 : SharePoint Designer 2013 (part 2) - Locking Down SharePoint Designer
 
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro