Wednesday, April 08, 2009

DomainObjects Update 3 released

Yet another update was just released to the Sitecore Shared Source space.

 

Before I go into detail, let me just pre-warn that this is the first blog post on this blog where I’m using Windows Live Writer (thanks to John West for pointing my attention towards this tool) – and in case everything goes all wrong, this would (or could) be why ;-)

 

So what’s new?

 

  • A bug was fixed, where creating multiple language versions of the same DomainObject wouldn’t get correctly placed in Sitecore. Additional Language versions would get added to the current Context Language instead.
  • Support for Sitecore Query was added. I am a little bit torn on this functionality and whether it “fits” with the DomainObjects mindset or not. However – as there is little in way of alternatives right now – I decided to include it.
  • Support for Sitecore Media Items is now part of the package. As most of you will know, Media Items in Sitecore are (almost) like any other Items, yet there are subtle differences. Getting the Url of a Media Item for instance, takes you via the MediaManager instead of the LinkManager. With this implementation, users of the DomainObject are shielded from such implementation details.

 

How to use it

 

Probably best shown with a few example from the Unit Tests written for this release:

StandardTemplate item = director.QuerySingleObject( "/sitecore/content//*[@IntegerTestField='10']" );
Assert.IsNotNull( item );
Assert.IsTrue( item is TestTemplate );

item = director.QuerySingleObject<TestTemplate>( "/sitecore/content//*[@IntegerTestField='10']" );
Assert.IsNotNull( item );

item = director.QuerySingleObject<StandardTemplate>( "/sitecore/content//*[@IntegerTestField='10']" );
Assert.IsNotNull( item );

item = director.QuerySingleObject<StandardTemplate>( "/sitecore/content/home" );
Assert.IsNotNull( item );

item = director.QuerySingleObject<TestTemplate>( "/sitecore/content/home" );
Assert.IsNull( item );

 

There is, as one would probably expect, a similar method for querying multiple objects. Query gets passed straight through to the Sitecore API, so same rules apply as would any other Sitecore Query.

 

As for Media Items, the following classes have been added – all mapping to equivalent templates in Sitecore:

 

  • Audio, VersionedAudio
  • Doc, VersionedDoc
  • Document, VersionedDocument
  • Docx, VersionedDocx
  • File, VersionedFile
  • Flash, VersionedFlash
  • Image, VersionedImage
  • Jpeg, VersionedJpeg
  • MediaFolder
  • Movie, VersionedMovie
  • MP3, VersionedMP3
  • PDF, VersionedPDF
  • Zip, VersionedZip

 

They all inherit from StandardTemplate and mostly only differ from the fact that their .Url property is gained in a different manner.

 

Creating a new Media Item therefore is no different than any other DomainObject, except that the media file itself needs to be attached. Something like this:

SCDirector director = new SCDirector();
Jpeg logoFile = new Jpeg();
logoFile.CreateFromFilePath = @"C:\Logo.jpg";
logoFile.Title = "New Company Logo";
logoFile.Alt = "Company Logo";
logoFile.Description = "The new logo we received from Big Media Inc";
logoFile.ParentId = director.GetObjectByIdentifier( "/sitecore/media library" ).Id;
logoFile.Name = "Logo";
logoFile.Director = director;
logoFile.Store();

 

That’s it. Enjoy :-)

0 Comments:

Post a Comment

Links to this post:

Create a Link

 

<< Home