Sample Code

  • Friday, October 28, 2011By Mark Graber

Today, I just wanted to share a few lines of code that have been useful in many situations. This introduces a few of the built-in Sitecore utility methods.

Here is the sample code:

if (Sitecore.Data.ID.IsID(itemId)) {
	Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));
	strPageTitle = Sitecore.StringUtil.RemoveLineFeeds(item.Fields["Page Title"].Value);
}

Let's break it down:

  • Sitecore.Data.ID.IsID()
    This method takes in a string guid and returns true if it's a valid guid in Sitecore
  • Sitecore.Data.ID.Parse()
    This method takes in a string guid and returns a Sitecore ID object
  • Sitecore.Context.Database.GetItem()
    This method takes in a Sitecore ID object and returns a Sitecore Item object
  • Sitecore.StringUtil.RemoveLineFeeds()
    This method takes in a string and returns the same string with line breaks removed.  This is helpful if you are populating an HTML tag attribute or a line of JavaScript where line breaks would be invalid.

Lucene Field Type Configuration

  • Friday, October 14, 2011By Mark Graber

This topic is related to using Lucene with the Sitecore shared source module Advanced Database Crawler from Alex Shyba, http://sitecoreblog.alexshyba.com/2010/11/sitecore-searcher-and-advanced-database.html.  My searches were not returning the expected results consistently when using the FieldValueSearchParam.  The search worked on some fields, but not others.  The FieldValueSearchParam allows you pass in a collection of fieldName, fieldValue pairs to search for values in specific fields.  This is a very powerful tool because it executes much faster than Sitecore queries.  After a number of test cases, I noticed that the fields there were not working were of field type, Droplist and AccountSelector.  Droplist is a built-in Sitecore field type that allows you to see a dropdown of values to select from, but it stores the raw value as plain text rather than a Guid like the Droplink field type.  The AccountSelector is a Sitecore shared source module that lets you select a user or multiple users from the Sitecore security database.  It stores the logins as plain text '|' delimited for multiple logins.  Since both were essentially single-line text values, the configuration you need to add is the same as the single-line text field type.  Add the following to your search config file and rebuilt the index.

<fieldTypes hint="raw:AddFieldTypes">
       …
       <fieldType name="droplist" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
       <fieldType name="accountselector" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" />
       …
</fieldTypes>

Without these settings you can search for the items with the Full Text query, but not the Field search because the text from the Droplist and AccountSelector fields are added to the full text but not stored as the field level without this config change.

Sitecore API Docs

  • Wednesday, October 5, 2011By Mark Graber

Here are two links to Sitecore API docs.  I'm not sure what the version number represents, but I'm developing on Sitecore 6.0 through 6.5 and haven't found a class or method that is not listed.

http://sdn.sitecore.net/doc/api%205.0/Sitecore.html

http://sdn.sitecore.net/doc/api%205.1.1/Sitecore.html

Sitecore Utilities

  • Wednesday, September 14, 2011By Mark Graber

Here are some helpful methods in some of Sitecore's built-in utility classes.  There are many helpful utilities burried in the Sitecore API, but these are a few to explore.

Browser Detection

Sitecore.UIUtil.IsFirefox()
Sitecore.UIUtil.IsIE(6)

String Utility

Sitecore.StringUtil.ExtractParameter()
Sitecore.StringUtil.ArrayToString()

File Utility

Sitecore.IO.FileUtil.ZipFiles()

Getting Guid from Sitecore

Sitecore.ItemIDs.RootID
Sitecore.ItemIDs.AnonymousUser
Sitecore.ItemIDs.EveryoneRoleID

Subversion to FTP Site Release Process

  • Friday, June 24, 2011By Mark Graber

Subversion is our preferred tool for managing our source code revisions and merging code between development teams.  This article describes a way to leverage your Subversion revisions to release files to a hosting environment that may not allow you to run an update via SVN, but instead your only access point is through FTP site.

Managing CSS Files in Sitecore

  • Sunday, May 22, 2011By Mark Graber

Sitecore is a content management system, but can it also be a web design management system?  The short answer is that you can design some solutions to provide flexibility for content management users to manipulate the the design via CSS files, allow them to manage and upload their own CSS files or make changes to the initial CSS files.

Read more for the configuration steps required to support CSS files as Sitecore Media Assets.

Changes to FieldRenderer When Upgrading from Sitecore 6.2 to 6.3

  • Wednesday, April 27, 2011By Mark Graber

In addition to the following references to review and consider when upgrading from Sitecore 6.2 to 6.3, you should be aware of a small change to the FieldRenderer control.  Sitecore 6.3 removes new-line (\n) characters from Multi-Line Text fields so if your code is setup to use those new-line characters and replace them with <br /> tags or split on them to build an array of lines, you should get the raw value from the Value property on the Field collection instead of getting it from a FieldRenderer because the Value property will still include the new-line character.

Sitecore 6.3 Release Notes

http://sdn.sitecore.net/SDN5/Products/Sitecore%20V5/Sitecore%20CMS%206/ReleaseNotes/ChangeLog/Release%20History%20SC63.aspx#631initial

Introducing Sitecore 6.3 PDF Reference

http://sdn.sitecore.net/Reference/Sitecore%206/Introducing%20SC63.aspx

Helpful Admin Pages to Secure in Production Environments

  • Friday, April 22, 2011By Mark Graber

The following URLs are very helpful admin pages, and they are not secured with password protection by default.  You should secure these in production environments.  I recommend securing your whole sitecore folder with IP restrictions in IIS because this is very quick and easy to setup.

  1. /sitecore/admin/cache.aspx - lets you monitor the caches, sizes, and clear cache
  2. /sitecore/admin/stats.aspx - lets you see the Sublayout Html caches to confirm if you Sublayout caching is working as expected
  3. /sitecore/admin/showconfig.aspx - lets you see the working Sitecore config settings in case your not sure if there is a patch config in your App_Config/Include folder is overriding something in the Web.config file

Subversion Tip - Simple Rollback to Previous Version

  • Saturday, January 29, 2011By Mark Graber

Although this is not specifically a Sitecore topic, it is something that comes up often for us on our Sitecore projects because we almost always use Subversion to manage our code and configuration files. In the case where you've made an update that you want to revert back and have it become the latest revision again, take the following steps:

  1. First, make sure the file you want to rollback is identical to the latest version checked in, and you can do this by running an Update on the file. Then, right-click the file and select Diff. It should return the identical message. If not, you can deleted the file and get the latest.
  2. Now that you have the latest version of the file, right-click on the file you want to rollback and select TortoiseSVN>Show Log
  3. Right-click on the file in the bottom half of the window where it shows Action, Path, Copy from path, and Revision. Select "Revert changes from this revision"
  4. It will prompt you with a message about reverse-merging the revision into your working copy. Click Yes to the prompt.
  5. To confirm it matches the previous version, right-click on the file and select Diff with previous version. You should get a prompt that the files are identical.

Using Akamai Net Storage for Sitecore Media Items

  • Sunday, January 23, 2011By Mark Graber

This article provides some ideas on how to leverage an externally hosted Media Library, such as, Akamai NetStorage.  Click to view the full post.

Include config files

  • Wednesday, January 19, 2011By Mark Graber

As Sitecore continues to move components out of the 2000+ line Web.config file, don't forget to sift through the config files in the App_Config/Include folder.  An example of this came up for me today with the Sitecore Ecommerce Module where a config file in the Include folder was overriding the Web.config setting.

Some of the important things to remember about these config files is that they can override settings that are in the Web.config file.  Here is the simple example from the Ecommerce Module.

Sitecore Announces Better Shared Source Support

  • Friday, January 14, 2011By Mark Graber

Here is a trackback to Jimmie Overby and the Sitecore Shared Source Blog.  The Shared Source Blog is a great way to read about new things happening with the shared source modules.  The shared source modules are great, and now even better with "Tested by Sitecore" assurance.  Here is a link to the full post:

http://sharesitecore.wordpress.com/2011/01/14/announcing-the-better-shared-source-initiative/

Linking to Content Editor

  • Wednesday, December 15, 2010By Mark Graber

Here is an easy way to link to the Sitecore Content Editor for your Custom Applications within the Sitecore Desktop. I also recommend that you set the target of the link to _blank or a named target so that it opens in another window.

protected string GetInternalItemLink(string guid, string lang) {
    return string.Format("/sitecore/shell/sitecore/content/Applications/Content Editor.aspx?id={0}&la={1}&fo={0}", guid, lang);
}

Version Control with Sitecore and Subversion

  • Tuesday, December 7, 2010By Mark Graber

This post will be very brief because I mainly wanted to share a link to a Sitecore Developer Network Article that walks through how you should use Subversion for your Sitecore projects.  We use the same process at Oasis with Subversion.  Pay close attention to the ALL CAPS note: "DO NOT ADD PARTS OF SITECORE DISTRIBUTION TO SVN UNLESS YOU WANT TO MAKE AND TRACK MODIFICATIONS TO THEM."

We make sure that we do not commit files that are part of the Sitecore installation, unless we make changes to them.  This way you can download any version of Sitecore that you want, checkout your SVN files, copy them on top of the Sitecore installed files, and now you have a new version of your code running on that version of Sitecore.  Clearly, there are a few more steps, such as, configuration file considerations, but you get the general idea.  If you keep your committed code separate from the Sitecore installation, it will be easier to manage your Sitecore version updates, and you won't fill your Subversion repositiory with unnecessary files.  In addition, you will kill the performance of your repository if you fill it with the Sitecore folder under the website.

Here is the link to the article.  http://sdn.sitecore.net/Developer/Team%20Development%20with%20Sitecore/Tuning%20Environmfent%20for%20Team%20Development/Developer%27s%20Workstation%20Installation/Creating%20a%20New%20Progect%20in%20SVN.aspx

Sitecore Caching with Sublayouts

  • Tuesday, November 30, 2010By Mark Graber

This post will walk through some of the great caching features that Sitecore provides using sublayouts.  I will walk through 3 simple examples to get you starting with using sublayouts to cache content.  Once you get the basics, there are many more ways to use caching to meet the needs of your web applications.  Here are the 3 examples:

  1. Static Content – Same for All Users
  2. Dynamic Content – Vary by Logged-In State
  3. Dynamic Content – Vary by SessionID & AccountID

The following examples use hard-coded sublayout tags you can drop into your Layout code, but the same concepts apply when using the dynamic Presentation layer in Sitecore.

FTP Client from Sitecore Codebehind

  • Wednesday, October 27, 2010By Mark Graber

Have you ever wanted to download files from an FTP server with ASP.NET code?  If so, you probably considered a few different paths: (1) buy some code or a DLL for the FTP Client, (2) build your own class library, or (3) search from something built-in to ASP.NET.

This article is going to discuss the third option.  There are some built-in classes that you can use in ASP.NET 3.5 even though the articles are titled .NET v4.0.  I can confirm that this code works on a Sitecore 6.2 website that is running ASP.NET 3.5 and not 4.0.

Download FTP Client Samples Here:

http://code.msdn.microsoft.com/nclsamples/Release/ProjectReleases.aspx?ReleaseId=3503

http://msdn.microsoft.com/en-us/library/b7810t5c%28v=VS.90%29.aspx

Click into the article detail to view the key source code.

Sitecore Configuration API

  • Wednesday, October 20, 2010By Mark Graber

Here is a very helpful class that is not so often discussed... the Sitecore.Configuration class, and it is super easy to use.

Below is a simple code example of how this class can be helpful. We all know that Sitecore's Web.config file is loaded with more settings and configuration attributes then any of us can explain. Therefore, it could be common that you would want to know some of these settings in your C# code.

For example, you could have a page that you only want Sitecore Admin users to view and redirect them to the specific type of error page if they are not logged in as an Admin (i.e. permission denied or No Access URL). Here is how you would code that:

if (!Sitecore.Context.User.IsAdministrator) {
    Response.Redirect(Sitecore.Configuration.Settings.NoAccessUrl);
}

Click Event Handlers Hackery

  • Monday, October 11, 2010By Mark Graber

Another interesting need came about today working with the Sitecore Ecommerce Module because most of the module is compiled into a DLL. There came about a need to update a value when the click event was fired, but the click event was in the DLL. Therefore, to avoid rewriting the event by copying code from the Reflector tool, I removed the event in the Page_Init that I added to the ASPX file. This allowed me to add my own event btnUpdate_Click2 to the ASPX file.

So, the question is, are these events synchronous or asynchronous? All testing so far has indicated that the events are asynchronous, meaning that they run in order and not at the same time. The code below shows how this was done.

btnUpdate.Click -= btnUpdate_Click;
btnUpdate.Click += new EventHandler(btnUpdate_Click2);
btnUpdate.Click += btnUpdate_Click;


More on ASP.NET Events 

Selecting CSS Class Names with Dots Using jQuery

  • Monday, October 4, 2010By Mark Graber

Another day... another unique issue.  The title of the post pretty much summarizes the new issue.  Working with someone else's code, you don't have control over naming conventions of CSS classes, and I just happened to be using code that has dots in the CSS class names.  I know what you are thinking.  Isn't that illegal?  Well, apparenltly, you can do it, but I wouldn't.  So, I will say this is how to work with it if you are using someone else's code because you would never code dots in your CSS class names.

So, here is the jQuery code to select CSS class names with dots.  You need to escape the dot with two backslashes.  I won't reveal the source of the "unconventional" CSS code, but you may be able to tell from this example.

// Hide fields on Checkout Name and Address form
$(".name\\.Country").hide();
$(".name\\.ShippingCountry").hide();

How to Build an XSLT Extension

  • Thursday, September 30, 2010By Mark Graber

I know that most of us developers hate using XSLT, but what if someone else wrote the XSLT for you and you just wanted to make a few tweaks to it. Then, you might like the XSLT because it's already done. Therefore, you need to understand how it works and how you can write C# code to make some simple changes. Here is a code example from the Sitecore Ecommerce Module, where I wanted to override the GetSinglePrice method in an XSLT rendering.

Step 1 : This first thing you need to do is write your own class like the following that inherits from the Sitecore.Xml.Xsl.XslHelper class.

class XslExtensions : Sitecore.Xml.Xsl.XslHelper
    {
        public string GetSinglePrice(XPathNodeIterator ni) {
            IProduct product = 
               Sitecore.Ecommerce.Context.Entity.Resolve<IProduct>();
            IPriceParameters priceParams =
               Sitecore.Ecommerce.Context.Entity.Resolve<IPriceParameters>();

            return String.Format("{0:C}", product.GetSinglePrice(priceParams));
        }
    }

Read the full article for the next steps.

Selecting a Sitecore Vendor

  • Thursday, May 20, 2010By Nick Laidlaw

As an established Sitecore certified partner, Oasis consults a variety of clients with Web Content Management (WCM) needs.  Every situation is unique but the team has found some common areas that may help your WCM vendor selection process.  You may be a CMO, Marketing Director or IT manager in charge of the vendor selection process for your web content management implementation.  You already understand the benefits of WCM and perhaps have selected a WCM such as Sitecore for your implementation.  First, congratulations on your selection.  Sitecore is a mature, robust content management platform that provides the support of a global corporate organization with a codebase that supports developer community contribution.  It offers the best of both the open-source and corporate worlds.  Oasis is a seasoned Sitecore certified partner with over twenty production implementations in a variety of industries and market verticals.  The team can work across the entire continuum of the project life-cycle.  Based on our adaptability and range of implementations we are qualified to offer the following recommendations for choosing a Sitecore vendor.

Are Sitecore and SharePoint friends or foe?

  • Thursday, May 6, 2010By Nick Laidlaw

Why choose between Sitecore and SharePoint?  Use them both if you need to but please don’t use SharePoint for your consumer facing website.

At Oasis we consult with clients in many different industry verticals.  Their businesses and organizations are unique but their web content management issues at its core tend to be similar.  Many of them use SharePoint for their internal collaboration needs.  It’s common for clients to be confused about the need for, what they consider to be, an additional web content management platform when we recommend Sitecore.  Microsoft does a great job selling SharePoint as a full suite of tools, including web content management.  But, its web content management drawbacks are only evident once you’ve tried to deploy a visually stunning and flexible site.

Sitecore 6.2 does NOT include Analytics

  • Thursday, April 29, 2010By Nick Laidlaw

I wanted to give everyone a heads up to save you some aggravation when working with Sitecore Analytics.  I'm using Sitecore 6.2 100104, and as far as we knew, this version of Sitecore comes with Sitecore Analytics.  As a matter of fact, it does have a link to the Sitecore Analytics application from the Sitecore start button.  You don’t have to install any packages to get that link, it’s there with the default installation.  So, you would think you have the actual application...think again!