So I recieved another of the fan mails with the following questions. If you come across similar issues feel free to email me.
Is there any sample code to use web service to pull the data from Microsoft SQL server 2005/2008?
The Sitemeter Gadget chapter 9 uses Ajax to pull data from online web service and sitemeter uses database in the background to pull and display data. so as long as your web service is able to pull and display data in the form of TEXT, XML, RSS, ATOM, JSON, HTML and HTML, you can display it in the Gadget.
I have an icon/image to refresh the data on the Gadget.. is it possible to change the cursor to wait/ progress till the data gets refreshed?
Search for this functionality in JavaScript in an html page. Gadget main page is essentially an html page and you can inlcude a JavaScript file which can be used to change the cursor.
Is it possible to show spinner on clicking the OK button in the settings page?
Yes, we do this in blog gadget(loading animation) chapter 8
Is it possible to show some alerts upon clicking the OK button in the settings page?
Most of the widgets, allows for message alert, you should also check debugging section to see how to use JavaScript and VBScript alert boxes, look also the last chapter Tips and Tricks
I recently got this email from a user who is trying to build an enterprise level Gadget for Windows 7. If you are in the same boat see if you come across similar problem (my comments below).
"I am working to build a windows 7 gadget for our enterprise. We wanted this gadget to be our desktop source for our application. Below is a brief overview of what we want to achieve:
Launch the gadget. Display a login screen in a flyout and try to connect to our server
Once we login, try to display various modules and also favorites across each module. Each module here is an application
If we select a module, display a flyout screen which displays the transactions within the module
Once a transaction is selected, open the transaction in a chrome less window
All our application logic resides on a server. We do have all the API’s to perform the above functions, but we are not sure if we can achieve the same through a gadget. Below are some of the specific problems that we are coming across:
How to initialize a session in a gadget and how to handle sessions? This is critical because we need to find some way of logging into the application and storing the login information
Does gadgets support handling of cookies?
Which is the best approach to connect to our server? Based on our research, we found out that using iFrames is a good option; but the problem is they do not support or hold session variables
Your inputs we be greatly helpful and appreciated. At this point we are currently in a deadlock. Looking forward for your response. "
Here is my comments on the question:
About Your scenario
1. Launch the gadget. Display a login screen in a flyout and try to connect to our server
I would suggest have the login in the settings page (Check sitemeter gadget chapter 9)
2. Once we login, try to display various modules and also favorites across each module. Each module here is an application
If these modules are hosted on a server and have a server page then you can directly login to server page in the iframe and redirect to module page, the cookies are maintained
3. If we select a module, display a flyout screen which displays the transactions within the module
this again can be both iframe based solution (server page embedded in the flyout) or an Ajax API calling the server and returning data in xml/json and parsing and displaying it in the flyout.
4. Once a transaction is selected, open the transaction in a chrome less window
This can be open a new window JavaScript method (note that Gadget supports full javascript as well as VB script)
About the comments:
In a Gadget the best way to maintain session is by using Gadget settings. You can easiliy read/write to the gadget settings which will persist. This can be used for both login information and user choice.
To connect a server the gadget allows Ajax to connect to a server and retrieve data in the form of Text, HTML, XML, XHTML, XML RPC, JSON, and SOAP, any of the option can be used with Ajax with cross domain access from the gadget. (check Blog gadget example chapter 5-8)
You can always use Iframe inside the flyout window if you need special server pages/ graphs (see chapter 9)
If you come across any problem feel free to email me.
I got the following email the other day from a fellow gadget developer, regarding a problem with a Gadget which access a SOAP based Web Service.
"I bought your book 'Creating Vista Gadgets' and congratulations... It’s a very good book since it gives both technical details and a broader view of the subjects on other areas which engineers usually forget about. My aim is very simple… I wanted to migrate a small windows application to the vista gadget platform. It’s an astrological quality time meter using the Webservice http://webservices.plaintime.com/Service.asmx?wsdl. Since my knowledge of XML and Javascript is more google based I’m having problems understanding how to code it. Any help is welcome.
Although I could not help him directly, except for telling him that,
A SOAP based Web Service can be called inside a gadget similar to an rss feed. which is thoroughly covered in Chapter 5 - Chapter 8. Blog Gadget covers this topic in details. A webservice also return an XML file which then will be required to parse.
Sending notes to refer Chapter 9, Information Gadget which uses a Text based Web Service for a Sidebar Gadget, to get some insight and
A link to an article which elaborates a usage of webservice http://www.codeproject.com/KB/gadgets/DynamicsCRMGadget.aspx.
You wll be glad to know that he was finally able to use the Gadget Template in the book for the webservice and with his permission I am sharing his gadget with all of you. The gadget download page: http://www.maladilusoes.com/DLTL. Here is screenshot of the Gadget: Ontime – Astrology Time Management.
If you ever wanted a book on Creating Vista Gadgets, with lots of useful samples. Here is a book on the topic. Click here to buy the book.
Creating Vista Gadgets: Using HTML, CSS and JavaScript with Examples in RSS, Ajax, ActiveX (COM) and Silverlight (Buy from Amazon)
releasing on "May 19, 2008"
Table of Content
Advanced Gadgets included in the book This book has advanced samples using ActiveX (COM), Ajax, & Microsoft Silverlight. This guide will walk you through the process of designing and developing of each of them.
(a) Build a Blog Gadget using RSS /Atom Feed
MyBlog Gadget will walk you through a complete gadget development lifecycle using an online RSS/Atom Feed. We see the design patterns and standard practices, common assumptions and then add advanced features to the gadget with multiple feeds and advance page and record management.
(b) Create an Information Gadget with Ajax
Site Statistics Gadget uses Ajax and a Web API provided by Sitemeter.com to create a unique Information Gadget which mimics the online free service provided by Sitemeter.com. It shows how to do screen scraping and load part of a web page in the Flyout window.
(c) Develop a Utility Gadget using ActiveX COM technology
Most Recent Used (MRU).Net Projects is a innovative gadget which lists Most Recent Used projects in .Net. It shows projects for Visual Studio 2003, Visual Studio 2005 and Visual Studio 2008. The gadget also shows how to read and enumerate registry data. The gadget also explains how to use advanced features of Windows Power Shell and Windows Management Instrumentation (WMI) in a sidebar Gadget.
(d) Incorporate Internet Radio and YouTube Video in Fun Gadgets
Learn to create a Radio Gadget based on an online streaming server and Video Gadget using a YouTube Video feed. The gadgets also show how to incorporate windows Media Player as well as Flash Media Player inside a Sidebar gadget. See how to use XMLDOM to read online feed instead of Ajax approach.
(e) Create Cutting edge Sidebar gadget using Microsoft Silverlight
Silverlight World Clock Gadget shows time for multiple cities of the world. It also gives you an insider view of XAML and Microsoft Silverlight. Know the programming model of Silverlight in gadget development.
(f) Developing a Sidebar Gadget using Microsoft Popfly.NET
Website Comparison Gadget uses Microsoft Popfly.net to create an innovative gadget which compares the popularity of different websites with the help of services provided by Live Search and Microsoft Bar graph. It shows how to create the mashups in minutes and port it to Sidebar platform.
I would like to hear any questions/ comments/ suggestions, you may have !
Rajesh Lal is an author, technology evangelist, and solutions engineer specializing in windows and web technologies. He has received numerous awards for his articles on Windows Vista and Sidebar Gadgets and is a frequent contributor to Windows Vista Magazine and Code Project website.
With over a decade of experience in the IT industry, Rajesh enjoys taking an objective and pragmatic approach to developing applications using Microsoft technology. He has a master’s degree in computer science and holds both MCSD and MCAD titles.
I would like to tell you that this book contains no errors, but of course I'd be lying. I've done everything possible to verify the accuracy of every sentence and every code samples, but errors inevitably surface.
Here is a list of errors, that have been found in the book so far, since going to print. If you find an error that isn't listed here, let me know. or add it as a comment, for other fellow readers.
When I first started sidebar gadget development1, I found gadgets cool and innovative. But the more I learned about them, the more I realized their usefulness. Gadget development provides a whole new platform, becoming more productive in ways unimagined. On one hand, it's a mini web application which can host a web page, call a web service, and use AJAX to read an RSS feed. On the other, it can interact with the Windows API using the power of .NET languages. Wow! That's the best of both the worlds, wouldn't you agree?
The most important factor is the unique way in which a gadget provides the interaction between the web, Windows and users. In this example I try to utilize this, to give user the "Power of Videos" in the sidebar. Check the architecture below.
To hold your interest, here is how it looks in the sidebar.
About the gadget: The power of videos
Before I start, I must warn you: this gadget is quite addictive. Indulge yourself only during your spare time.
There are a number of RSS Feeds available from the Soapbox on MSN Video Beta page which give you the contents of the videos uploaded by the users. Some of the more interesting ones are: "Most Popular Videos", "Most Rated videos", and "Most Recent Videos". There is even a feed for "Videos By Tag" available, which we will use for searching videos. The idea behind this is to use those regularly updated feeds to show the user the video of his choice on the sidebar. If you want to know the basic barebones required to create a gadget, please refer to my first article Daily Dilbert . This article is actually an extension of that first article; I try to explain only those extra features which can be used to refine a gadget further:
Create a gadget which can play a video in the flyout
How to take care of paging when you read an XML file
Settings and Search Videos
Add video to "Favorite List"
Create, read, update, & delete local XML file from Sidebar Gadget (new)
Some tips in general
The architecture overview
Let's Begin
Create a video Gadget
A standard XML file with the name Gadget.xml is required as shown below:
The name of the gadget is "Soapbox @ MSN" (as seen above). The namespace is basically to group more than one gadget (reserved for future use), which you can write in "mynamespace". MinPlatformVersion. Required. The expected value is "1.0."
"Permission" controls the amount of permission in the gadget. "Full" permission is required if you want to access a webpage through the gadget. With these two files you can deploy and test your gadget in the sidebar. The <permission> tags and <type> tags will be more flexible in future versions of gadget development.
Gadget.xml An XML file defining the gadget properties, including name, icon and description main.html the Main HTML page main.js The core code/script for the gadget Settings.html Exposes gadget settings for the user to change Settings.js Script for the Settings file soapboxFlyout.html The html file which will be used in Flyout window Icon, images etc For use in the gadget selection box
The Main.html file will have five elements ("DIV") to get the feed data: cell0, cell1 etc. We can show the videos as a list of five videos (below) or single video.
The Feed for the Gadget
We have the URL which we call using the MSXML2.XMLHTTP object. This is the core of the AJAX until the feed gets loaded. Here is the magical JavaScript for this:
function getRSS() { document.getElementById("mylogo").Title = System.Gadget.Settings. read("feedText");
As you can see, if there is an error while connecting the feed or an error in the internet connection, we want to show the "Service not available" screen.
Parse the XML File
Here is the portion of the RSS Feed for a single item in the XML file and the corresponding "partial" ParseXML file to give you an idea of what we are doing with the feed.
Since we have the feed required, we can create a flyout from the video. This is the showFlyout function, which is created dynamically and is used to pass all the parameters from the item to the flyout page.
function showFlyout(sTitle,sDescription,sGuid, sImage, sRImage,sView, sDateUpdate,sDuration,sRatingFormated) {
Apart from the settings which are set on the showFlyout function, there are other settings which can be managed by the user. The user can search under "Video" or select from one of the Video feeds.
Once we have all the settings, we Build the video object in the flyout window.
if (rssRemoveItems[i].getElementsByTagName("guid") [0].firstChild.nodeValue==sGuid) { var lastNode=rssRemoveItems[i]; var delNode=xmlObj.firstChild.removeChild(lastNode); xmlDoc.save(System.Environment.getEnvironmentVariable (variableName) + "\\Videos\\SoapBoxFavorites.xml"); mytext = "Deleted and Saved"; break; } }
And we are done.
Tips in general
To resize a windows media player to a custom width, set the ‹param name="AutoSize" value="1"›
Any exceptions in a sidebar gadget main window should be handled with the "Service Not Available" screen, with information icon as shown above
Whenever you use a JavaScript function, encapsulate it inside a try catch statement to avoid surprises
Escape(^) and unescape(^) are nice little JavaScript functions for URL encoding and un-encoding when you need to pass URL's, etc. as parameters to the flyout window
The HTML code for < is "«" , << is "‹" , >> is "›" and > "»"
To check whether a file exists in a local folder use System.Shell.itemFromPath(Path) inside try catch
For a timer functionality, use the setinterval4 method
For multiple language support, create a folder for that language (e.g. 'IT-CH', if the language is it-ch Italian Switzerland) and copy all files from the 'EN-US' folder to the new created folder (language codes ^)
Check design concepts and usage pattern5
In Action
Soapbox Gadget in Action : Size: 400 X 360 in flash player