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.

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

2.2 Tags<br />

A Tag can be considered to be equivalent to a word or phrase that is used by users to organize their content,<br />

commonly for the public. The use the Tag system, first import the necessary namespace:<br />

using StarCommunity.Core.Modules;<br />

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

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

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

2.2.1 Tagging an entity<br />

The process of associating a tag with an entity item is done via the EntityTag class. The EntityTag enables the developer<br />

to add information of who tagged the item, available via the Tagger property of the EntityTag class. Keep in mind that<br />

each entity item can only be tagged with a Tag once, just as services like Flickr.<br />

//Get the user by id<br />

IUser user = (IUser)StarCommunitySystem.<br />

CurrentContext.DefaultSecurity.GetUser(1234);<br />

//Get the blog by id<br />

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

if (b != null)<br />

{<br />

b = (Blog)b.Clone();<br />

b.EntityTags.Add(new EntityTag(new Tag("foo"), new UserAuthor(user)));<br />

BlogHandler.UpdateBlog(b);<br />

}<br />

2.2.2 Retrieving the tags of an entity<br />

Retrieving the tags that an entity object has been tagged with is as simple as enumerating the Tags property of the<br />

StarCommunityEntityBase-derived object.<br />

//Get the blog by id<br />

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

if (b != null)<br />

{<br />

foreach(EntityTag et in b.Tags)<br />

{<br />

Console.Write("Tag name: "+et.Tag.Name);<br />

Console.Write(et.Tagger.Name);<br />

}<br />

}<br />

© <strong>EPiServer</strong> AB

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

Saved successfully!

Ooh no, something went wrong!