Sitecore Profile Cards: The Lazy Way

22 December 2011
Szymon Kuzniak
A good software developer is a lazy software developer

The sentence above is a quotation by one of my college professors. Of course, it was one of the most overused phrases by the students during the rest of the semester but honestly, it's hard to deny truth that hides underneath. The point is that we should be lazy enough to avoid dull and repetitive tasks which can be easily automated, yet productive enough to come up with easy to use solutions that will save the world us time for more interesting work.

You guessed it - I have done just so. The problem I faced was simple: I had to assign Sitecore Analytics profile cards to pages in the Sitecore content tree. Various tree branches should be assigned with different cards: for example all events with one type while all news with another. No big deal but terribly boring and even more terribly time consuming, and I did not have many items. On the other hand I also had Adam Najmanowicz, author of the great tool for Sitecore: PowerShell console, sitting just few desks away. It would be unwise not to use his knowledge to speed up the process.

The solution that we came up with was good, but not easily reusable and not straightforward for the user. We simply wrote a piece of script that iterates over children of the item and sets it's __Tracking property to one of the profile card values.

 Set-Location master:\content\home\clients\
Get-Childitem * | ForEach-Object { $_.Tracking = '<tracking>
<profile name="Proffesional Type" presets="solution seeker|100||">
<key name="Client" value="5" />
       <key name="Proffesional" value="1" />
       <key name="Spectator" value="3" />
 </profile></tracking>' }

Although it did it's job, the script needs XML representation of the profile card which is not so elegant and easy to use. It did what was supposed to do so I could spent saved time on more robust solution.

The obvious idea was custom PowerShell cmdlet. To warmup myself I started with two simple cmdlets for iterating over existing profiles and related cards. It turned out that after you get the idea on how to write such cmdlets it's extremely trival to do so. The main cmdlet was ready in nearly two hours almost without any problems. The only strange thing I noticed was the difference between XML representation of the profile card and what was actually added to the item.

The usage is simple: call some cmdlets to produce list of items and then pipe the result to my custom cmdlet which will assign corect values.

 Set-Location master:\content\home\clients\
Get-ChildItem * |
Set-ProfileCard -ProfileName "ProfileName" -ProfileCardName "ProfileCardName"

 Using PowerShell gives us almost endless possibilities of producing list of items: -Recurse parameter to browse the content tree down to the bottom or Where-Object to select items basing on some condition, to name a few. Actually while working on the cmdlets I wrote one to execute sitecore queries which also produces list of items.

The downside of this solution is that even though is simple and straightforward for developers, marketing people who will be facing similar problems might not fully appreciate the raw beauty of PowerShell. This is the occasion where developers can demonstrate the true power of automation and use of CMSes for simplifying one's life.

Cognifide's PowerShell console can be downloaded from Sitecore Marketplace.