Cannot call InfoPath 2010 SwitchView on FormEvents_Loading

Yep, when you call this method on your FormEvents_Loading, it will give you COM error. I guess it have something to you with the ViewInfo that own the SwitchView are not properly loaded on Loading.

So this SwitchView cannot be used on FormLoading (or OnChange event handler)

this.ViewInfos.SwitchView("AuthorView");

The error will go like this on VSTA debug mode

Invalid Context: SwitchView cannot be called in the Loading event

or like this if you call it from InfoPath

System.InvalidOperationException
Invalid Context: SwitchView cannot be called in the Loading event.
   at Microsoft.Office.InfoPath.Internal.MomExceptionHelper.EnsureViewSwitchContext(XmlFormHost formHost)
   at Microsoft.Office.InfoPath.Internal.ViewInfoCollectionHost.SwitchView(String name)
   at MainForm.FormCode.FormEvents_Loading(Object sender, LoadingEventArgs e)
   at Microsoft.Office.InfoPath.Internal.FormEventsHost.OnLoad(DocReturnEvent pEvent)
   at Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_SinkHelper.OnLoad(DocReturnEvent pEvent)

I’ve google for it; there’s solution from this MS support Microsoft Support site

They recomend to use FormEvent_Loading LoadingEventArgs attribute, so on loading you just have to do this

e.SetDefaultView("AuthorView"); // e is LoadingEventArgs 

It works!

Cryptic Infopath Error “Field or group: #text”

I get this error on InfoPath 2010 form submit

The form contains the following errors:

Field or group: #text
Error: No exact match was found.

That is because of I set “AccountId” inside Contact Picker to “username()” without setting the other field inside Contact Picker.

I remove the default value in the Contact Picker, and voila! the error has go away!

And then decide to pre-populate the contact picker using VSTA code,
I’ll publish that on next blog post 🙂

cURL di PHP melalui proxy ISA server

So, alkisah suatu hari aku harus membuat sebuah aplikasi menggunakan cURL, tapi somehow koneksi cURL ke webpage yang aku mau selalu gagal, pas ditelaah lebih lanjut ternyata setingan proxy dengan username dan password biasa tidak jalan di ISA server.

Setelah membaca manualnya PHP di bab mengenai cURL, aku menemukan settingan ini

curl_setopt( $ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM ); 

Ah, no wonder.. setingan proxy biasa kan mengirim username dan password melalui HEADER, untuk ISA server kita perlu mengaktifkan metoda autentifikasi melalui NTLM

Menambahkan baris itu di kode akses cURL nya..

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
			
curl_setopt($ch, CURLOPT_PROXY,'alamat.proxyisa.nya');
curl_setopt($ch, CURLOPT_PROXYPORT,'8080');

curl_setopt( $ch, CURLOPT_PROXYAUTH, CURLAUTH_NTLM ); 

curl_setopt ($ch, CURLOPT_PROXYUSERPWD, 'username:password'); 

$content = curl_exec($ch);
curl_close($ch);			

Voila.. it works behind ISA server

XPath di PHP untuk mendapatkan data di halaman web

Tantangan hari ini adalah XPath, Jadi pada suatu hari ada sebuah kasus dimana PHP script harus menangkap informasi di berbagai halaman web.

Yang menjadi masalah adalah halaman web bisa diformat dalam struktur yang bermacam-macam, kita membutuhkan cara membaca yang mudah, portable dan bisa disesuaikan.

XPath

XPath adalah syntax untuk mendeskripsikan lokasi dari elemen XML (makanya disebut PATH), jadi dengan mengetahui XPath tertentu kamu bisa melompat ke bagian tertentu dari dokumen XML dan mendapatkan nilainya.

Misal ada XML dokumen berbentu seperti ini:


        Nilai

Dengan XPath sederhana seperti

//node/elemen/anak

kita bisa mendapatkan isinya node dengan hanya mengetahui XPathnya (jadi tidak perlu iterasi per-node), konsepnya sih mirip dengan Pathnya folder structure.

Misalnya

/html/body

Akan mengembalikan seluruh node didalam “body”

Mirip juga dengan JQuery loh, misalnya

//div[@id="one"]

Akan mengembalikan node div dimanapun yang memiliki id “one”

/html/body/div[5]

Akan mengembalikan div ke-5 yang merupakan anak dari body

Untuk info lebih lanjut mengenai XPath bisa lihat disini
W3School Xpath

XPath in PHP

Di PHP kita bisa menggunakan DOMDocument atau simpleXML untuk melakukan query XPath.

Misalnya ada source HTML seperti ini

<html>
<head><title>Hai</title></head>
<body>
<h1>Produk Keren</h1>
<div id="warna">Merah</div>
<div id="price">Rp 3000</div>
<div id="deskripsi">Produk super keren!</div>
</body>

Kodenya sendiri mudah saja… misalnya teks diatas kita masukan ke variabel $content

// inisialisasi DOMnya, siapin objek bertipe DOMXPath
$html = new DOMDocument();
$html->loadHtml($content);
$xpath = new DOMXPath( $html );

// 1. mendapatkan judul dari halaman
$nodes = $xpath ->query('/html/head/title');
echo $nodes->item(0)->nodeValue;

//2. mendapatkan nama produk
$nodes = $xpath ->query('/html/body/h1');
echo $nodes->item(0)->nodeValue;

//3. mendapatkan harga
$nodes = $xpath ->query('//div[@id="price"]');
echo $nodes->item(0)->nodeValue;

//3. mendapatkan harga (incase HTMLnya tidak memiliki atribut
//    jadi kita menggunakan posisi sebagai acuan)
$nodes = $xpath ->query('/html/body/div[2]');
echo $nodes->item(0)->nodeValue;

Perlu diingat kalau metoda “query” akan selalu mengembalikan list of nodes (jika gagal dia akan mengembalikan list of nodes dengan length = 0)

Conclusion

Yang membuat XPath ini spesial adalah, kamu bisa menyimpannya sebagai string, misalnya program kamu mesti membaca bagian tertentu dari sebuah Site, tapi tiba2 site itu merubah desain layoutnya. Kamu tinggal mengubah lokasi XPath kamu (in which configurable bukan?)

Happy XPath-ing…

Embeding Image In InfoPath 2010

There’s a requirement where I have to embed 3rd party Digital Signature inside the form

It turns out in InfoPath 2010 we can directly embed base64 encoded image into the Picture Control. (wow.. no more InfoPath 2007 nightmare huh?)

To test it, I create a Picture and a Button, the button will have rule to inject the image data

1. Insert the Picture Control

It will ask you whether you want to insert as a link or embedded in the form XML. For this demo I use Included in the form (for as a link, you just have to change the Picture Value to the appropriate URL)

2. Set the picture to read-only (I want it to be injected by button event, so the user cannot change the picture)

3. prepare the image, to convert this image to base64 string.. I use this

4. Insert the image base64 string as a field value

5. Test the form


Well the basic ideas of this experiment are; we can replace the field-value by a HTTP call to a page/script/service that generate image or signature in base64 encoded string (or it’s direct URL will do)

I haven’t had the time to publish it to a Form Service, but the Compatibility check for browser mode indicated no Error.

Possible implementation:

  • Captcha in InfoPath?
  • 3rd Party Digital Signature
  • Generated Image based on user input (chart image?

And that concludes the experiment for today

 

 

 

 

Uninstall Visual Studio 2008

This morning, I’ve tried to install SQL server 2008 x64, but the instalation always fail because the installer ask me to update my Visual Studio 2008 into SPI1 version, because I am using Visual Studio 2010 now, so I decide to remove VS2008.

I do a normal Uninstalling process, and ERROR! Visual Studio 2008 refuse to uninstalled.

After googling for a few minutes, I’ve found many people that have similar problems.

The link here said that we have to download a tool to remove Visual Studio 2008, this tools can be applied to these specific version:

  • Visual studio Express Editions
  • Visual Studio Team System
  • Visual Studio Professional

So I download the uninstaller here

Auto Uninstall

Auto Uninstall

It took forever, but can solve the problem >_<