Darren Ferguon - Weblog

Wednesday, May 28, 2008

Using Umbraco Webservices with Perl

Filed under: Umbraco, Perl - by Darren Ferguson @ 12:27 PM

I had quite a few spam comments on my site that I wanted to get rid of. Without a content search option available in Umbraco, I decided to use the webservices to get a list of all blog comments.

As a result of this, I have quite a nice example of how to access Umbraco via webservices with Perl. The example below will simply get all documents from Umbraco and print them to screen.

use SOAP::Lite +trace => 'debug';
use Data::Dumper;

use constant HOST => 'myhost';
use constant UID  => 'admin';
use constant PWD  => 'mypwd';
use constant ROOTNODE => 0;

my $access = SOAP::Lite
  -> proxy('http://'.HOST.'/umbraco/webservices/api/DocumentService.asmx')
  -> uri('http://umbraco.org/webservices/')
  -> on_action( sub {sprintf '%s%s', @_} );

my $username = SOAP::Data->type(string=> UID)->name('username');
my $password = SOAP::Data->type(string => PWD)->name('password');

my $result = $access->UserAuthenticates($username, $password);

if ($result->fault) {
    print $result->faultcode, " : ", $result->faultstring, "\n";
    die "Couldn't login";
}

my @docs = allDocs($access, ROOTNODE, $username, $password);
foreach my $doc (@docs) {
	print Dumper($doc);
}

sub allDocs {
	
	my ($access, $nodeId, $username, $password) = @_;
	my @docs;
	my $node = SOAP::Data->type(int=> $nodeId)->name('parentid');
	$result = $access->readList($node, $username, $password);
	if($result->result)
	{
		if($result->result->{documentCarrier}->{Id}) { 
			push @docs, $result->result->{documentCarrier};
		} else {
		 	@docs = @{$result->result->{documentCarrier}};
		}
	
	}
	foreach my $doc (@docs) {
		push(@docs, allDocs($access, $doc->{Id}, $username, $password));
	}
	return @docs;
}

2 comment(s) for Using Umbraco Webservices with PerlComments RSS

  1. Paul says:

    PaulPerl! You're very retro! Nicely done. I see you are attending CodeGarden so introduce yourself when you see me.

    Best,
    -Paul

  2. Darren Ferguson says:

    Darren FergusonHi Paul,

    Was good to meet you in Copenhagen.

    Perl still powers some of the biggest sites on the WWW including the BBC and parts of Amazon.

    It is quite a common perception that it is a dated or 'retro' technology but to my mind it is still very current!

    Hope you had a good trip to the states, best of luck with the store.

    Darren.

Leave a comment