31.10.2014 Views

Developer's Guide - EPiServer World

Developer's Guide - EPiServer World

Developer's Guide - EPiServer World

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

18 | Developer’s <strong>Guide</strong> <strong>EPiServer</strong> Community 3.1<br />

2.5 Attributes<br />

The Attriibute system allows for developers to setting attributes and attributes values of primitive and complex types to<br />

any object that implements the IAttributeExtendableEntiry interface. This provides a fast and flexible way to extend<br />

<strong>EPiServer</strong> Community classes. The name of the attribute must be pre-defined in the database. This is commonly done<br />

via the administration interface. The attribute name must be unique within a type.<br />

Note that this tutorial only describes the most elementary way to use attributes. A more object-oriented approach is to<br />

create a custom class derived from Blog that expose fixed properties. This would call for creating your own<br />

EntityProvider as explained in section 1.2.4<br />

First import the necessary namespaces:<br />

using StarCommunity.Core.Modules.Attributes;<br />

using StarCommunity.Modules.Blog; //only for this example.<br />

2.5.1 Setting attribute values<br />

In this example we set a DateTime and a DocumentArchive attribute to a blog:<br />

//Get the blog for which set the attributes<br />

Blog blog = (Blog)BlogHandler.GetBlog(1).Clone();<br />

//Set a DateTime attribute that contains a last updated date<br />

blog.SetAttributeValue("attr_last_updated", DateTime.Now);<br />

//Get the document archive to use<br />

DocumentArchive da = DocumentArchiveHandler.GetDocumentArchive(123);<br />

//Set a DocumentArchive as an attribute to the blog<br />

blog.SetAttributeValue("attr_archive", da);<br />

2.5.2 Getting attribute values<br />

//Get the blog for wich we want the attriute values<br />

Blog blog = BlogHandler.GetBlog(1);<br />

//Get the last updated attribute<br />

DateTime lastUpdated =<br />

blog.GetAttributeValue("attr_last_updated“);<br />

//Get the document archive attribute<br />

DocumentArchive da =<br />

blog.GetAttributeValue("attr_archive");<br />

© <strong>EPiServer</strong> AB

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!