Server-side printing

When you call the Print method, the DataWindow is printed to a printer that is installed on the Web server. The DataWindow can reside in either a WebDataWindowControl or a DataStore.

The Web server must be configured so that the ASP.NET worker process has access to system settings and the SYSTEM account has access to printers. Implementing server-side printing requires changing the default permissions on the server.

Configuring the .NET Framework

By default, the .NET Framework runs with the permissions of the local “machine” account. In order to print using IIS, the .NET Framework must run with the permissions of the local “SYSTEM” account. The procedures for configuring the .NET Framework for IIS 5.x and 6.x are different.

On Windows 2000 or Windows XP with IIS 5.x, you need to edit the machine.config file on the Web server to ensure that the process under which ASP.NET is running has sufficient permissions to access printers installed on the network.

On Windows Server 2003 and on Windows 2000 or Windows XP with IIS 6.x, you use the IIS Manager (the inetmgr utility) to configure the application pool identity.

StepsTo configure the .NET Framework to run with local SYSTEM settings with IIS 5.x:

  1. In your C:\WINDOWS or C:\WINNT directory, navigate to the Microsoft.NET\Framework\VersionNum\CONFIG directory, where VersionNum is the version of the .NET Framework, for example v1.1.4322.

  2. Open the machine.config file with a text or XML editor and locate the processModel element.

    You need to change the value of the userName setting in this element. The default settings for userName and passWord are:

    userName="machine" passWord="AutoGenerate"
    
  3. Change the value of userName to “SYSTEM”:

    userName="SYSTEM" passWord="AutoGenerate"
    
  4. Save the machine.config file.

StepsTo configure the .NET Framework to run with local SYSTEM settings with IIS 6.x:

  1. In the Windows Start>Run box, type InetMgr.

  2. In the IIS Manager, expand local computer and select Application Pools.

  3. Right-click Application Pools and select Properties.

  4. Click the Identity tab, select Local System from the Predefined list box, and click OK.

  5. Click Yes in the pop-up warning message window that displays.

  6. Right-click local computer and select All Tasks>Restart IIS to restart IIS.

Exposing printer settings to the SYSTEM account

When a printer is installed on a computer, its settings are stored in the HKEY_CURRENT_USER registry key. IIS runs under the context of the local SYSTEM account. It has access to the HKEY_USERS registry key, but not to the HKEY_CURRENT_USERS subkey, which is only available to a user logged on to the computer.

By default, no printers are defined in the HKEY_USERS key. You can add them by exporting three keys from the HKEY_CURRENT_USERS key and importing them to the HKEY_USERS key.

NoteCaution Incorrectly editing the registry might severely damage your system. Make sure you back up valued data before making changes to the registry.

StepsTo make printer settings available to the SYSTEM account:

  1. Check that the current user on the Web server has the required printer(s) installed.

  2. To launch the Registry Editor, type regedit in the Start>Run dialog box and click OK.

  3. Select the HKEY_USERS\.DEFAULT\Software\Microsoft\Windows NT\CurrentVersion key and export the registry key from the File or Registry menu.

  4. Specify a name and location in the Export Registry File dialog box and click Save.

    This file provides a backup if you need to restore the registry.

  5. In the HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion key, select the Devices subkey and export the registry key.

  6. Specify the name devices.reg and a temporary location in the Export Registry File dialog box and click Save.

  7. Repeat steps 5 and 6 for the PrinterPorts and Windows subkeys, naming the files printerports.reg and windows.reg.

  8. Open devices.reg in Notepad (do not use TextPad or another editor), replace the string HKEY_CURRENT_USER with the string HKEY_USERS\.DEFAULT (note that there is a dot before DEFAULT), and save the file.

  9. Repeat step 8 for printerports.reg and windows.reg.

  10. Double-click each of the edited files to import them into the registry.

  11. Restart IIS so that the configuration changes take effect.

StepsTo restart IIS:

  1. In the Windows Start>Run box, type InetMgr.

  2. In the IIS Manager, right-click the local computer and select All Tasks>Restart IIS.

  3. In the Start/Stop/Reboot dialog box, select Restart Internet Services on ComputerName, where ComputerName is the local computer.

After restarting IIS, you need to restart the Web site.

Where the DataWindow is printed

When you use the Print method, the DataWindow is printed to the printer specified in the DataWindow object’s PrinterName property, or to the default printer as specified in the Devices registry key.

This Visual Basic code gets the name of the printer and writes it to a log file before printing the DataWindow:

Dim printerName As String
printerName = dwGrid.PrintProperties.PrinterName
sMsg = "Printer name: " + printerName
writelog(sMsg)
dwGrid.Print()