Sitecore Replace Tokens Command

Quite often when developing Sitecore sites, as I’m optimising templates I add additional fields and tokens into the standard values. This means for already created items based on the updated template there are unexpanded $token variables in the content item. The same issue is present if you reset a content item to standard values, the tokens are adding into the content item rather than the calculated value.

Therefore I’ve developed a new Sitecore module that adds a new command into the content editor to replace all tokens in the current item.

Mike Reynolds has also got a great blog post on using Sitecore PowerShell to replace tokens also. http://sitecorejunkie.com/2015/02/15/expand-tokens-on-items-using-a-sitecore-powershell-extensions-toolbox-script/

Replace Tokens Command
Replace Tokens Command

The below code is the heart of logic behind the component. We create an instance of MasterVariablesReplacer and then call ReplaceItem(item) on the current context item.

Sitecore Marketplace

https://marketplace.sitecore.net/Modules/Replace_Tokens_Command.aspx?sc_lang=en

Source On GitHub

https://github.com/sobek1985/TokenReplacementCommand

 

My Sitecore Modules and Contributions

Sitecore 8 Modules

Other Projects

Sample Projects

  • Entity Service Demo Application

    This demo Entity Service / SPEAK application was built to demonstrate how to build in Entity Service. I built it for a Sitecore User Group talk I did.
    Source: Entity Service Demo on GitHub

Visual Studio Templates

My gists

https://gist.github.com/sobek1985

Projects with other developers

Entity Service Visual Studio Project Template

Recently I’ve been working with Entity Service in Sitecore.Services.Client in A few projects. I realised I was repeating the same process time and time again to set up the framework of Entity Service.

I have therefore created a Visual Studio project template with the framework of Entity Service set up. All the references for Web API, MVC etc are included via NuGet ready to build your project on. Just bring your own Sitecore kernel and Sitecore.Service DLLs.

Entity Service Project Template
Entity Service Project Template
Entity Service Project
Entity Service Project

Visual Studio Template Package

https://github.com/sobek1985/EntityServiceTemplate/releases

Source on GitHub

https://github.com/sobek1985/EntityServiceTemplate

Install MongoDB as a Windows service for Sitecore

  1. Download and install MongoDB https://www.mongodb.org/downloads
  2. Run Command Prompt as Admin. CD to the directory containing the bin directory of your MongoDB installation. Usually “C:\Program Files\MongoDB 2.6 Standard\bin” on a standard installation of MongoDB.
  3. Run the following script to install MongDB as a service.
    mongod --dbpath "PathToStoreMongoDbs" --serviceName "NameYouWantToCallTheService" --logpath "PathOfWhereToStoreLogFiles" --logappend --install

    e.g.

    mongod --dbpath "C:\inetpub\wwwroot\MikeRobbins8\Databases" --serviceName "MikeRobbinsMongoDB" --logpath "C:\inetpub\wwwroot\MikeRobbins8\MongoDBLogs\mongodb.log" --logappend --install
  4. Within the services snap in you will see the MongoDB service listed where you can start and stop the service. The service will be installed with the name you specified, however the display name will be set to MongoDB.
    MongoDB Service
    MongoDB Service
  5. You can uninstall the MongoDB service using the following command.

    mongod --dbpath "C:\inetpub\wwwroot\MikeRobbins8\Databases" --serviceName "MikeRobbinsMongoDB" --remove

EntityService Sitecore.Services.Client

Sitecore.Services.Client provides a powerful and flexible method of creating Web API based services within Sitecore. Using the provided interfaces and the repository pattern means that you will be building services in a consistent way, allowing you to build more flexible services and reduce the time to develop and maintain them. Sitecore.Services.Client was introduced first in Sitecore 7.5 and supported also in Sitecore 8.

Sitecore.Services.Client consists of two separate services. ItemService which allows you to work with standard Sitecore Item class. The other is EntityService which allows you to work with business entities that you create, and allows you implement the business logic as you require.

Both EntityService and ItemService offers a number of ways of consuming the service, allowing you to work via web requests to the rest API, use the JavaScript API or bind to SPEAK components. This post I will be using the JavaScript API with EntityService in context of an Sitecore SPEAK application.

Model

The first stage of creating a EntityService based service is to create a Model. This model will define your business entity you wish to work with. All Models have to implement the Sitecore.Services.Core.Model.EntityIdentity class.

namespace MikeRobbins.News.Models
{
 public class NewsArticle: Sitecore.Services.Core.Model.EntityIdentity
 {
public string Title{ get; set; }
public string Description{ get; set; }
 }
}

Repository

Create a repository based on the IRespository interface. E.g Sitecore.Services.Core.IRepository<NewsArticle>. Once you implement the interface you can start to code up the functionality that your EntityService will expose.

IRepository will give you methods for Add, Delete, Exists, FindById, GetAll and Update of your Model. You can however extend this if you require.


using MikeRobbins.News.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data;

namespace MikeRobbins.News.Repository
{
 public class NewsArticleRepository : Sitecore.Services.Core.IRepository<NewsArticle>
 {

 public void Add(NewsArticle entity)
 {
 throw new NotImplementedException();
 }

 public void Delete(NewsArticle entity)
 {
 throw new NotImplementedException();
 }

 public bool Exists(NewsArticle entity)
 {
 var newsArticle = Sitecore.Data.Database.GetDatabase("master").GetItem(new ID(entity.Id));

 return newsArticle != null;
 }

 public NewsArticle FindById(string id)
 {
 var newsArticle = Sitecore.Data.Database.GetDatabase("master").GetItem(new ID(id));

 if (newsArticle != null)
 {
 return new NewsArticle() { Id = newsArticle.ID.ToString(), DisplayName = newsArticle.DisplayName };
 }
else
{
return null;
}
 }

 public IQueryable<NewsArticle> GetAll()
 {
 throw new NotImplementedException();
 }

 public void Update(NewsArticle entity)
 {
 }
 }
}

Controller

Next create a controller and add the ServicesController attribute. Implement the EntiryService<T> class where T is your Model. Create a second constructor that creates new instance of your repository class.

namespace MikeRobbins.News.Controllers
{
 [ValidateAntiForgeryToken]
 [Sitecore.Services.Core.ServicesController]
 public class NewsArticleController : EntityService<NewsArticle>
 {
 public NewsArticleController(IRepository<NewsArticle> repository)
 : base(repository)
 {
 }

 public NewsArticleController()
 : this(new NewsArticleRepository())
 {
 }

 }
}

SPEAK PageCode

Using RequireJS require a reference to the entity service JavaScript library, and update the define element to include the entityService.

require.config({
 paths: {
 entityService: "/sitecore/shell/client/Services/Assets/lib/entityservice"
 }
});

define(["sitecore", "jquery", "underscore", "entityService"], function (Sitecore, $, _, entityService) {
 var News = Sitecore.Definitions.App.extend({

 initialized: function () {

 },
 });
return News;
});

Working with EntityService via the JavaScript API

To work with EntityService via the JavaScript API you create an instance of EntityService passing in the url property. The url property is the url of your controller. EntityService is always prefixed with “/sitecore/api/ssc”. The next part of the url is the namespace containing your controller with the “.” replaced with “-” followed by your controller name.

E.g. If you your controller is in the namespace MikeRobbins.News.Controllers and called NewsArticleController. Your url would be “/sitecore/api/ssc/MikeRobbins/api/ssc/MikeRobbins-News-Controllers/newsarticlearticle”

Get Entities Example

Create an instance of EntityService with the service url, you can then call fetchEntities. This will then call the GetAll method within your repository defined within the controller. An array of the entities are then returned.

GetNewsArticles: function () {

 var newsService = new entityService({
 url: &amp;quot;/sitecore/api/ssc/MikeRobbins-News-Controllers/newsarticle&amp;quot;
 });

 var result = newsService.fetchEntities().execute().then(function (newsArticles) {
  for (var i = 0; i &amp;lt; newsArticles.length; i++) {
 var title = newsArticles[i].Title;
}
 });

 },

Get Entity Example

Create an instance of EntityService with the service url, you can then call the fetchEntity function passing in an id of an entity. This will then call the FindById method within your repository defined within the controller. An entity class in returned.

GetNewsArticle: function () {

 var newsService = new entityService({
 url: "/sitecore/api/ssc/MikeRobbins-News-Controllers/newsarticle"
 });

 var result = newsService.fetchEntity("73e3a4fe-7d42-4ed9-b66e-ad948f340f27").execute().then(function (newsArticle) {
 var title = newsArticle.Title;
 });

 },

Update Entity Example

Create a an instance of EntityService with the service url, you can then call the fetchEntity function passing in an id of an entity. Once you have an entity returned, make the required changes to the entity. From here you can call the save method, this maps to the update method within the repository. A class is returned containing the updated entity, in the below example I’m popping up a success message in the SPEAK message panel.

UpdateNewsArticle: function () {

 var newsService = new entityService({
 url: "/sitecore/api/ssc/MikeRobbins-News-Controllers/newsarticle"
 });

 var result = newsService.fetchEntity("73e3a4fe-7d42-4ed9-b66e-ad948f340f27").execute().then(function (newsArticle) {
  newsArticle.Title = "Hello World";

 newsArticle.save().execute().then(function (savedNewsArticle) {
 messagePanel.addMessage("notification", { text: "Item updated successfully", actions: [], closable: true, temporary: true });
 });
 });

 },

Delete Entity Example

Create an instance of EntityService with the service url, you can then call the Delete function passing in the entity to delete. This will then call the delete method within your repository defined within the controller.

DeleteNewsArticle: function () {

 var newsService = new entityService({
 url: "/sitecore/api/ssc/MikeRobbins-News-Controllers/newsarticle"
 });

 var itemToDelete= {
 Id: itemId
 };

 var result = newsService.delete(itemToDelete).execute().then(function (newsArticle) {
 });

 },

Create Entity Example

Create an instance of EntityService with the service url. You can then create a new object populating the properties of your entity. From here you can call CreateEntity, passing in the object you created. This will then call the add method within your repository defined within the controller. A class is returned of the newly created entity.

CreateNewsArticle: function () {

 var newsService = new entityService({
 url: "/sitecore/api/ssc/MikeRobbins-News-Controllers/newsarticle"
 });

var exampleNewsArticle = {
 Title: "Hello World",
 Description:"Some content describing the news article.";
}; 

 var result = newsService.createEntity(exampleNewsArticle).execute().then(function (newsArticle) {
var title = newsArticle.Title;
 });

 },

Tools

There is a few tools you can use to help with debugging and exploring SSC.

  1. Fiddler http://www.telerik.com/fiddler
  2. Kevin Obee’s Sitecore Glimpse Plugin. Under Sitecore Tab there is a sitecore services section where you can explore your SSC services.

    Sitecore Glimpse
    Sitecore Glimpse
  3. POSTman https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en
  4. Chrome’s Console

Further Reading

Jamie Little has written a useful post on how to add custom Authorisation Filters to the controller to restrict access.

Sitecore services client – Creating a custom authorisation filter

Source

I have an example Sitecore SPEAK application with SSC implemented available on GitHub.  https://github.com/sobek1985/SitecoreSPEAKBulkWorkflows/

Sitecore SPEAK: How to add Applications to Sitecore 8 Launch Pad

Sitecore SPEAK’s Launch pad existed within Sitecore 7.1/7.2/7.5 but has received a major overhaul in Sitecore 8. It has become a focal point welcoming users as they login to Sitecore.

Previously with Sitecore 7.x you could add shortcuts to the launch pad from within the Core database. This was done by adding application shortcut items containing the ID of the new Sitecore SPEAK application to the launch pad shortcuts folder. “/sitecore/client/Applications/Launch Pad/Page Settings/Shortcuts”

Sitecore 7.x SPEAK Shortcuts
Sitecore 7.x SPEAK Shortcuts

With Sitecore 8, the configuration of Launch Pad shortcuts has changed. If you are installing existing Sitecore SPEAK modules built on Sitecore 7.x, then you will need to follow these steps to see the shortcuts in the new Launch Pad.

For Sitecore 8 these are the steps to add an application shortcut to the launch pad.

  1. Switch to the core DB in the desktop or use Sitecore rocks.
  2. Navigate to the following item “/sitecore/client/Applications/Launchpad/PageSettings/Buttons”
  3. Here you can create LaunchPad groups to contain your shortcuts.

    LaunchPad Groups and Buttons
    LaunchPad Groups and Buttons
  4. Create a LaunchPad-button within your new group.
  5. Set the Icon, and Text of the shortcut as required. The link field needs to be set to the URL of your Sitecore SPEAK application (This is single link text field)

    SPEAK Application URL
    Sitecore SPEAK Application URL
  6. The Sitecore SPEAK application URL can be taken from the item path in your SPEAK Application item. E.g. “/sitecore/client/MikeRobbins/SitecoreDataImporter”

    Sitecore SPEAK Application Item Path
    Sitecore SPEAK Application Item Path