Connecting Drupal and Silverlight: By Matt Serbinski

Note: This article is submitted by Matt Serbinski for Silverlight 2 Beta 2 – Articles Competition.Thanks a lot, Matt! Hello All, Please drop a comment if you like it.

Connecting Drupal and Silverlight
Connecting Drupal and Silverlight

Contents

  • Introduction
  • Custom Module for Drupal
  • Silverlight application
  • About the Author (Matt Serbinski)

Download ~

Introduction

Drupal is an open source content management system (CMS) that allows users to add modules to their sites for great flexibility. These modules are typically written in PHP and use drupal’s API to connect themselves with the framework. Silverlight (version 2.0 and greater) is a cross-platform browser plugin that allows users to write managed .NET code for the back end and use XAML as the front end. Since silverlight is a browser application and drupal is a framework for hosting data in browsers there can be data that is shared between the two using HTTP as the transport. In this example, I have chosen to use XML-RPC as drupal uses xml-rpc natively and silverlight can easily take advantage of this.

XmlRPC
XmlRPC

One thing to note, drupal has a module called services that is currently under development that will allow external applications (such as silverlight) to easily communicate through various interfaces, such as: XML-RPC, SOAP, REST and AMF. I am NOT using this module in my example.

Custom Module for Drupal:

As far as the drupal side of things go, I have a basic install of drupal 6.2 with a custom module that I have written to communicate with my silverlight application. I wrote the module based off of tips from Pro Drupal Development using chapters 2 and 19. There are two files that are needed to construct a drupal module: a .info file that contains information about the module and a .module file that is the actual custom module. My custom module is extremely simple in which it implements hook_xmlrpc to map xml-rpc methods to php callback functions. The xml-rpc methods are received from silverlight and then call the appropriate php function with the appropriate arguments. My php functions do various tasks, such as: checking the drupal database to see if a user exists so that user can login if the correct credentials are supplied,logging in an anonymous user, logging a user out and creating a fully authenticated user. Once the module is uploaded to the server, enable the module and your done! Well, done with the drupal side.

Silverlight application

There are currently two methods in silverlight to communicate via a web service (in this case xml-rpc): WebClient and HttpWebRequest. HttpWebRequest contains all of the properties and methods that WebClient has and then some. But of course the extra goodies that HttpWebRequest provides come at a price; added complexity. WebClient is extremely easy to use and only requires one event and one property to be used, all though there are additional events and properties that can make life easier. Here are the events and properties that I have used in my application:

drupalDB = new WebClient();
drupalDB.UploadStringCompleted += new UploadStringCompletedEventHandler(drupalDB_UploadStringCompleted);
drupalDB.UploadProgressChanged += new UploadProgressChangedEventHandler(drupalDB_UploadProgressChanged);
drupalDB.UploadStringAsync(location, xmlData);

drupalDB is my WebClient and there are two events defined that will notify me when upload progress has changed (perfect for a progress bar) and completed. The property, UploadStringAsync takes in a Uri and a string as the data to upload to the web service. One thing to note here is that I am using UploadStringCompleted and not DownloadStringCompleted as WebClient does a GET for downloads and a POST for uploads and xml-rpc only accepts POSTs.

The data that I am uploading to the xml-rpc service is a string that is xml formatted that uses the standard xml-rpc definition:

</p></p>

string xmlData = "<?xml version=\"1.0\"?>"
+ "<methodCall>"
+ "<methodName>userlogin.logAnonIn</methodName>"
+ "<params>"
+ "<param>"
+ "<value>"
+ "</value>"
+ "</param>"
+ "</params>"
+ "</methodCall>";

There are two important things to note here. The tag <methodName> contains the xml-rpc method that the hook_xmlrpc function will map to a php function. The first part of that argument, userlogin is the name of my module and a standard format that drupal follows. The <param> tag contains a <value> tag where an argument can be passed to the hook_xmlrpc function. If multiple arguments are required then one needs to add additional <param><value>my data here</value></param> tags in between the <params> tag. Once the data is sent from silverlight to drupal and the appropiate mapping php function is processed, a return value is sent back to silverlight:

<methodResponse>
<params>
<param>
<value>
<string>return value</string>
</value>
</param>
</params>
</methodResponse>

The UploadStringCompleted event will fire once all of the above data is received. A popular way to parse the returned data is to use LINQ. LINQ is very powerful and very easy to use once you learn a little about it. I can parse the above data with the following LINQ statement:

</p></p>

XDocument document = XDocument.Parse(e.Result);
List<MethodToCall> methodsToCall = (from user in document.Descendants("value").Elements("string")
select new MethodToCall
{
MethodName = user.Value
}).ToList();

I place the result in an XDocument and use LINQ to parse the document for any node ‘value’ that has a child node ‘string’. This is the standard format that xml-rpc sends. If there are mutliple arguments then there will be multiple ‘value’ nodes. I call ToList() on the LINQ statement to create a generic list for multiple arguments that may be passed back.

With my particular application, since I can control what I send to and from the xml-rpc service, I decided to use reflection and pass back a string from drupal that I have defined as a method in my silverlight application. One thing to note here is that arguments are passed back as objects. If multiple arguments are passed back they are each an object, not an object[] so your method must take multiple arguments that are each objects instead of one argument of type object[].

After my method is called via reflection I simply update my user object as appropriate. And that’s it! That is the basics of how to get drupal to talk to silverlight and vice versa. Of course, one needs to add some more logic and flare to a silverlight application to take advantage of the data it receives from drupal. I have added a few items that hopefully make the user experience that much better (and easier) in my user login application.

As a side note, once silverlight is configured on your web server all you need to display the silverlight application is the following html code placed wherever you would like:

</p></p>

<div style="width: 325px; height: 350px;">
<object data="data:application/x-silverlight," type="application/x-silverlight-2-b2" width="100%" height="100%">
<param name="source" value="/silverlight/SLApps/DrupalGameDemo.xap" />

<!-- Only need the below to show a link to get silverlight if it is not installed -->
<a href="http://go.microsoft.com/fwlink/?LinkID=115261" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" />
</a>
</object>
</div>

The <object> tag contains a <param> tag that has the ‘value’ attribute of the location of your .xap file. The link in the above html code (<a> tag) is the link that will be displayed if a user does not have silverlight installed.

Feel free to try out the attached silverlight application (it is also displayed at the top of this page) and change things to your liking. Enjoy!

About the Author (Matt Serbinski)

My name is Matt Serbinski and I graduated from Rochester Institute of Technology (RIT) in March 2007 with a BS in computer engineering. I have been working at my current job, Naval Surface Warfare Center in Dahlgren, Virginia since 2004 while I was cooping. I have worked on a handful of projects that integrate existing technologies (lethal and non lethal) onto vehicle platforms as a single point to control everything. I was introduced to WPF when it was in beta and started to love everything it offered. Once Silverlight 2.0 was in beta I started to develop applications on the side for fun. I enjoy taking technologies and figuring out ways to use them together and developing WPF and Silverlight applications for fun.

6 thoughts on “Connecting Drupal and Silverlight: By Matt Serbinski

  1. as for a parameter, it should have a name and a value. in your case, why only value? does xml-rpc only take ordinal index(parameter index in the xml) just like Win32 API?

  2. Sir how i can connect a silverlight application to drupal 7. I am newbie to drupal.
    if i can’t run silverlight application on apache server, then is there any way to retrieve data from remote site. Please Help!!

  3. Have you ever thought about including a little bit more than just your articles?
    I mean, what you say is important and all. However imagine if you added some great
    visuals or videos to give your posts more, “pop”!
    Your content is excellent but with images and video clips,
    this blog could undeniably be one of the greatest in its niche.
    Superb blog!

Leave a Reply

Your email address will not be published. Required fields are marked *