Wednesday, February 16, 2011

VSTA Integration with InfoPath

We don't need the VSTA SDK to use VSTA in InfoPath 2007. We need some prerequisites and  make a specific selection in setup:

Microsoft .NET Framework 2.0 (or later) and Microsoft Core XML Services (MSXML) 6.0 must be installed first.

The VSTA development environment is not installed by default when you choose Typical to install InfoPath. To install VSTA, you must either choose Customize when first installing, or use Add or Remove Programs to update your Office or InfoPath installation to include VSTA. The option to install VSTA is available by expanding Microsoft Office InfoPath, .NET Programmability Support, and .NET Programmability Support for .NET Framework version 2.0. The easiest way to do this, is to expand .NET Programmability Support, and then choose Run All From My Computer.

Then, you need to configure your InfoPath form template to use managed code. Open the form template in Design view, click Form Options on the Tools menu, and then click Programming in the Category list. Under Programming, set the Form template code language to either Visual Basic or C#. After doing that, you should have Microsoft Visual Studio Tools for Applications under Tools > Programming.

Testing:

On the InfoPath form 2007 click Button --> Edit Form Code and then start writing the code...
Ex:
//InfoPath SQL Database integration - accessing data from sql through Stored procedures in InfoPath
var serviceid = XDocument.DOM.selectSingleNode("/dfs:myFields/dfs:dataFields/d:titleauthor/@serviceid").text;

//Set the Command for the Query Adapter of the Data Source. Incorporate the
//parameter values that you want to use.
XDocument.QueryAdapter.Command = 'execute "dbo".""storedprocname" ' + serviceidValue;

//Query the Data Source.
XDocument.Query();

InfoPath - Inserting line breaks into text using Rules

Create a new XML file 'linebreak.xml' and save it.

<?xml version="1.0" encoding="UTF-8"?>
<characters
    cr="&#xD;"
    lf="&#xA;"
    crlf="&#xD;&#xA;"
/>


Then in InfoPath go to Tools | Data Connections and click Add. Select Receive data, then XML Document. Browse to characters.xml then complete the wizard. When it asks “The selected file is not part of the form...add this file...?” click Yes. At this point we’ve just added a resource file to the template that gets loaded into a declaratively-accessible DOM - no code required.

Testing it:

Add a Button control, open up the properties and click Rules. Add a Rule, and add an Action of the type “Set a field’s value”. For the field pick the text box’s field (e.g. field1). For the new value, use the formula builder and build the following expression:
concat(field1, @crlf, "Hello, World!")
Result would look like:

field1Value
Hello, World!

 


Tuesday, February 8, 2011

Time Zone Webpart (CEWP)

In the CEWP add this code and in any document library add timezone.js and reference it properly

<style type="text/css">
 th {filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfff4', endColorstr='#b3bead',undefined ); /* ie */ }
</style>
<table align="center" border="1" bordercolor="black"cellpadding="0" cellspacing="0" width="100%">
<tr>
 <th align="center" valign="middle">EST</th>
 <th align="center" valign="middle">CST</th>
 <th align="center" valign="middle">MST</th>
 <th align="center" valign="middle">PST</th>
</tr>
<tr>
 <td align="center" valign="middle"><span id="tEST">&nbsp;</span></td>
 <td align="center" valign="middle"><span id="tCST">&nbsp;</span></td>
 <td align="center" valign="middle"><span id="tMST">&nbsp;</span></td>
 <td align="center" valign="middle"><span id="tPST">&nbsp;</span></td>
</tr>
</table>

<script type="text/javascript" src="js\TimeZone.js"></script>





TimeZone.js:



function timeSource(tz){
   x=new Date(timeNow().getUTCFullYear(),timeNow().getUTCMonth(),timeNow().getUTCDate(),timeNow().getUTCHours(),timeNow().getUTCMinutes(),timeNow().getUTCSeconds());
   switch(tz)
   {
  case 0: /*PST*/
   x.setTime(x.getTime()+daylightSaving()-28800000);
   break ;
  case 1: /*MST*/
   x.setTime(x.getTime()+daylightSaving()-25200000);
   break ;
  case 2: /*CST*/
   x.setTime(x.getTime()+daylightSaving()-21600000);
   break;
  case 3: /*EST*/
   x.setTime(x.getTime()+daylightSaving()-18000000);
   break;
   }
  
   return x;
}
function timeNow(){
   return new Date();
}
function daylightSaving(){
   return ((timeNow().getTime()>findDay(0,3,1,1).getTime())&&(timeNow().getTime()<findDay(0,9,1,-1).getTime()))?3600000:0;
}
function findDay(d,m,h,p){
   var week=(p<0)?7*(p+1):7*(p-1),nm=(p<0)?m+1:m,x=new Date(timeNow().getUTCFullYear(),nm,1,h,0,0),dOff=0;
   if(p<0){
      x.setTime(x.getTime()-86400000);
   }
   if(x.getDay()!=d){
      dOff=(x.getDay()<d)?(d-x.getDay()):0-(x.getDay()-d);
      if(p<0&&dOff>0){
         week-=7;
      }
      if(p>0&&dOff<0){
         week+=7;
      }
      x.setTime(x.getTime()+((dOff+week)*86400000));
   }
   return x;
}
function leadingZero(x){
   return (x>9)?x:'0'+x;
}
function twelveHour(x){
   if(x==0){
      x=12;
   }
   return (x>12)?x-=12:x;
}
function dateEnding(x){
   if(x==1||x==21||x==31){
      return 'st';
   }
   if(x==2||x==22){
      return 'nd';
   }
   if(x==3||x==23){
      return 'rd';
   }
   return 'th';
}
function displayTime(){
   document.getElementById('tPST').innerHTML=eval(outputTimePST);
   document.getElementById('tMST').innerHTML=eval(outputTimeMST);
   document.getElementById('tCST').innerHTML=eval(outputTimeCST);
   document.getElementById('tEST').innerHTML=eval(outputTimeEST);
   setTimeout('displayTime()',1000);
}
function amPMsymbol(x){
   return (x>11)?'pm':'am';
}
function fixYear4(x){
   return (x<500)?x+1900:x;
}
var dayNames=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var monthNames=new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var outputTimePST="dayNames[timeSource(0).getDay()]+' '+timeSource(0).getDate()+dateEnding(timeSource().getDate())+' '+monthNames[timeSource(0).getMonth()]+' '+fixYear4(timeSource(0).getYear())+' '+':'+':'+' '+twelveHour(timeSource(0).getHours())+':'+leadingZero(timeSource(0).getMinutes())+':'+leadingZero(timeSource(0).getSeconds())+amPMsymbol(timeSource(0).getHours())";
var outputTimeMST="dayNames[timeSource(1).getDay()]+' '+timeSource(1).getDate()+dateEnding(timeSource().getDate())+' '+monthNames[timeSource(1).getMonth()]+' '+fixYear4(timeSource(1).getYear())+' '+':'+':'+' '+twelveHour(timeSource(1).getHours())+':'+leadingZero(timeSource(1).getMinutes())+':'+leadingZero(timeSource(1).getSeconds())+amPMsymbol(timeSource(1).getHours())";
var outputTimeCST="dayNames[timeSource(2).getDay()]+' '+timeSource(2).getDate()+dateEnding(timeSource().getDate())+' '+monthNames[timeSource(2).getMonth()]+' '+fixYear4(timeSource(2).getYear())+' '+':'+':'+' '+twelveHour(timeSource(2).getHours())+':'+leadingZero(timeSource(2).getMinutes())+':'+leadingZero(timeSource(2).getSeconds())+amPMsymbol(timeSource(2).getHours())";
var outputTimeEST="dayNames[timeSource(3).getDay()]+' '+timeSource(3).getDate()+dateEnding(timeSource().getDate())+' '+monthNames[timeSource(3).getMonth()]+' '+fixYear4(timeSource(3).getYear())+' '+':'+':'+' '+twelveHour(timeSource(3).getHours())+':'+leadingZero(timeSource(3).getMinutes())+':'+leadingZero(timeSource(3).getSeconds())+amPMsymbol(timeSource(3).getHours())";
if(!document.all){ window.onload=displayTime; }else{ displayTime(); }




Reference:
// Clock Script Generated By Maxx Blade's Clock v2.0d
// http://www.maxxblade.co.uk/clock