Sitecore extension methods

Challenge:

Starting from .NET 3.0 we’ve been introduced to Extension method (If you haven’t heard about extension methods then good to read it from here. Basically, Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. )

Now, In October 2011 got to know about it’s real usage via Sitecore extension method competition on learn sitecore. And at that time challenged my self to submit entries for this competition.

Solution:


UPDATE : 16-February-2014 (Sourcecode is available via GitHub and binary as a Nugget Package)

Since so long was planning to git this Source code (So, everyone can contribute) and create a nugget package. So, you can install it quickly! And today was a day to do it! here you go!

  1. GitHub Repository URL : https://github.com/klpatil/SitecoreBasics.Extensions
  2. Nugget Package : https://www.nuget.org/packages/SitecoreBasics.Extension/

SC.EXT-Nugget

Sounds Interesting? You’ve your own set of Extension Methods? Do you think it will be useful for other Sitecorians? Why don’t you contribute to same GitHub? Open for Fork/Pull requests!


Submitted it and it got published on site as well. You can check them from here — http://learnsitecore.cmsuniverse.net/en/Developers/Articles/2011/12/Sitecore-extension-methods-part-1.aspx

On earlier page few methods got published on web page. But binaries, sourcecode and documentation was not published. And it was there in my treasury. Today, thought to share it  with you! So, you can useb it and if needed you can modify it as per your need! [Yes, I’m going to provide source code as well!]

Which methods are available?

Basically, to get fields value Sitecore provides strongly typed class for field type. Usually we used to create helper file and use it or use generic Field class to do so. But using extension methods we can get strongly typed class specific method which will be really helpful for you.  List of available methods:

Method Name

Description

Example

IsLanguageVersionExist This extension method will be used to check whether item exist in current Sitecore Context language or not boolisLanguageVersionExist = item.IsLanguageVersionExist();
IsLanguageVersionExist This extension method will be used to check whether item exist in current provided language or not boolisLanguageVersionExist = item.IsLanguageVersionExist(Language.Parse(“de-DE”));
GetBooleanField This extension method will be used to get Boolean value from provided field. boolbooleanValue = item.GetBooleanField(“<BOOLEANFIELD>”);
GetMediaItemField This extension method will be used to get media item value from provided field. MediaItemmediaItemField = item.GetMediaItemField(“<MEDIAITEMFIELD>”);
GetStringField This extension method will be used to get string value from provided field. string stringField = item.GetStringField(“<STRINGFIELD>”);
GetDateField This extension method will be used to get date value from provided field. DateTime dateField = item.GetDateField(“<DATEFIELD>”);
GetHtmlField This extension method will be used to get HtmlField value from provided field. HtmlFieldhtmlField = item.GetHtmlField(“<HTMLFIELD>”);
GetLinkField This extension method will be used to getLinkField value from provided field. LinkFieldlinkField = item.GetLinkField(“<LINKFIELD>”);
GetReferenceField This extension method will be used to getReferenceField value from provided field.Useful for : Droplink, Droptree, Grouped Droplink ReferenceFieldreferenceField = item.GetReferenceField(“<REFERENCEFIELD>”);
GetReferenceFieldItem This extension method will be used to get ReferenceField Item’s reference from provided field.Useful for : Droplink, Droptree, Grouped Droplink Item referenceFieldItem =item.GetReferenceFieldItem(“<REFERENCEFIELD>”);
GetMultilistField This extension method will be used to get MultilistField from provided field.Useful for : Checklist, Multilist, Treelist, Treelist-Ex MultilistFieldmultiListField = item.GetMultilistField(“<MultiListField>”);
GetMultilistFieldItems This extension method will be used to get MultilistField items from provided field.Useful for : Checklist, Multilist, Treelist, Treelist-Ex Item[] items = item.GetMultilistFieldItems(“<MultiListField>”);

How to download it?

You can download full package from here. Extract it and you will find following files. Description of each is as follow:

  1. binaries.zip : Binary file.
  2. UserManuals.zip : User Manuals and API documentation
  3. SitecoreBasics.Extensions.sourcecode.zip : source code

How to use it?

  1. Extract SitecoreBasics.Extensions.dll from binaries.zip file.
  2. Reference SitecoreBasics.Extensions.dll in to your Sitecore solution.
  3. Add namespace reference (using SitecoreBasics.Extensions;) wherever you would like to use SitecoreBasics.Extension methods. List of methods are available above or you can find it out from documentation.
  4. If you are a MSDN fan or need detailed API documentation please refer Documentation.chm file.
  5. If you would like to see the source code, please refer SitecoreBasics.Extensions.sourcecode.zip file.

Happy Sitecoring! 🙂

2 thoughts on “Sitecore extension methods

  1. Pingback: Decade’s worth of 11 Sitecore modules | Sitecore basics!

Leave a comment