codeflood logo

Content How To

This page lists how to perform some common tasks when dealing with Sitecore content using Revolver.


Replace spaces in item names with dashes

To perform this task we’ll use the find command to locate items with spaces, then we’ll use sa to set the new name while manipulating the previous name using the replace command.

Start by finding the items we’re looking for. Use cd to navigate to the root of the item where we’ll be working. Now formulate the find command using an attribute filter. If you want to operate recursively don’t forget to use the -r parameter.

find -a name \s pwd

Now let’s work out the replace command. We want to pass it the current name, search for spaces and replace them with dashes.

replace > (ga -a name) \s -

Now, we combine these commands. We aren’t using the sa command yet, so no updates will be made. We’ll instead see the effect of the replacements in a simulated run.

find -a name \s (replace < (ga -a name) \s -)

If all is looking good we can go ahead and put the sa command in to make the updates.

find -a name \s (sa name < (replace < (ga -a name) \s -))

This is another locate, modify, store example. In this case we’re looking for URLs in content fields that need to be updated to fit a new URL structure. In this example we’ll be changing links of the structure /au/content.aspx to /au/en/content.aspx.

The first thing we need to do is work out how to locate the content we need to update. We’ll use the trusty find command here to locate items which need to be updated. Start by navigating to the root of the content where we wish to start making updates. We’ll assume the URLs are limited to the “text” field, so we’ll use a field filter with the find command.

find -r -f text /au/ pwd

The above command will search all descendants for items with a text field that contain anything with a /au/ in it. As usual we don’t want to start modifying items until we’re sure our RegExs are working, so we use pwd to list the matched items.

Next we need to work out how we want to modify the matched item. We can use the replace command combined with the gf command. To test this part of the command, navigate to one of the items matched in the previous command.

replace &lt; (gf -f text) /au/ /au/en/

If we already had a /au/en link in the field we were testing against the effect of the above command would be to replace the /au/ still and we’d end up with a duplicate en such as /au/en/en. To prevent this we need to tweak our RegExs to exclude the match if it is followed by en.

replace &lt; (gf -f text) /au/\(?!en\) /au/en/

We can now put the replace command into the find command. This is our dry run. Executing this command will show the updates that will be made.

find -r -f text /au/\(?!en\) (replace &lt; (gf -f text) /au/\(?!en\) /au/en/)

The only thing left to do is to alter the command to update the existin fields using the sf command.

find -r -f text /au/\(?!en\) (sf text &lt; (replace &lt; (gf -f text) /au/\(?!en\) /au/en/))

If you have multiple combinations of URLs that need updating (or need to execute again in the future), create a script to execute this for you. The path parts can be passed in as parameters.

find -r -f text /$1$/\(?!$2$\) (sf text &lt; (replace &lt; (gf -f text) /$1$/\(?!$2$\) /$1$/$2$))

So if we store the above in a script called “fixlinks”, then execution would be as simple as:

fixlinks au en

Copy content from one language to another

The Copy Item To Language command (cpl) can be used to copy content from the current context language to another language.

Before using this command you’ll need to make sure you know which language you’re currently working in. You can display the current context language by using the Print Current Language command (pwl).

pwl

But for an easier way to quickly check the current context language make sure it’s part of your command prompt. The %lang% token when used in the prompt environment variable is replaced by the current context language. The following will set the command prompt to display the context database, context language and context path.

set prompt (%db%:%lang%:%path% >)

Now we’ve ensured we’re in the correct language the Copy Item To Language command (cpl) can be used to copy all field data from the current context language to another language. The following command will copy the current language field data to the Danish language.

cpl da

By default cpl will only copy field data over to the target language field if the target field is empty. This helps to prevent accidental overwriting of field data. This is not always desirable though, so you can force cpl to overwrite all target fields regardless of whether they have existing data or not, by passing the -o parameter.

cpl -o da

By default cpl will copy all fields from the source language to the target language. If you only want to copy a single field, use the -f parameter and specify the field name.

cpl -f title da

And of course you can use the cpl command with the find or query commands to copy the content from the context language to a target language on multiple items at once. To copy all content from the current language to the Danish language for all items under the /sitecore/content/home item use the following:

cd /sitecore/content/home
find -r (cpl da)