July 11, 2012

Updating an xml file using VBScript Custom Action

  1. Create a VBScript Custom Action stored in Binary Table (MSI Type Number: 6)
  2. Here is the Sample xml file (say address.xml), which is considered for updating in the below VBScript CA
         <Address>
              <Name>Smitha</Name>
              <Address1>123B, 16th Cross</Address1>
              <City>Bangalore</City>
         </Address>
  3. Write the following code in the VBScript file
Function UpdateXMLFile
     On Error Resume Next               
         
    Set objDOM  = CreateObject("Microsoft.XMLDOM")
    Set objParentNode = CreateObject("Microsoft.XMLDOM")    
    if Err.Number = 0 then             
          
                       ' Declaration
                       
dim FileName
                       dim name
                   
   dim address1
                     
  dim city
   Set objNet = CreateObject("WScript.NetWork")                
   set objNet=nothing                
              
    name=Session.Property("NAME")    
    address1=Session.Property("ADDRESS1")
    city=Session.Property("CITY")
 
     'update address.xml file
      FileName= Session.Property("INSTALLDIR")+"\"+"address.xml" 
      objDOM.async = false  
      objDOM.load(FileName)                                                
     if (trim(name)<>"" and trim(address1)<>"" and trim(city)<>"") then
         ' update name
          strXPath = "
/Address/@Name"
          set objParentNode = objDOM.selectSingleNode(strXPath)
          objParentNode.text = name           
 
         ' update address1
          strXPath = "
/Address/@Address1"
          set objParentNode = objDOM.selectSingleNode(strXPath)
          objParentNode.text = address1           
 
        ' update name
         strXPath = "
/Address/@City"
         set objParentNode = objDOM.selectSingleNode(strXPath)
         objParentNode.text = city 
  
         objDOM.Save(FileName)
                     end if
                 end if
           end function ' End of UpdateXMLFile

  4.    Set the following properties for the Custom Action (RestartIIS)

  Script Function -> UpdateXMLFile
  Return Processing -> Synchronous (Check exit code)
  In-Script Execution -> Immediate Execution
  Install Exec Sequence -> After InstallFinalize
  Install Exec Condition -> &FeatureA=3 and Installed.
This condition means that FeatureA is selected for installation and the FeatureA is installed (Installed state will be true as the CA is scheduled to execute after InstallFinalize).       

  [Note: This code snippet is tested with InstallShield 2012 Basic MSI Project. Should be compatible with earlier versions of InstallShield as well.]

No comments:

Post a Comment