Upgrading EPiServer CMS from version 7 to 7.5 – TinyMCE

Here’s a little note on how to upgrade EPiServer CMS 7.x to EPiServer CMS 7.5.

Bug in property settings for TinyMCE

When everything seemed to work properly after the update I discovered that Edit view occasionally seemed to stop working. The problem occurred when switching from on-page edit to all properties editing and EPiServer seemed to load for a endlessly long time. When I brought up the console window in Firebug I discovered that there where something wrong with the JavaScripts used by EPiServer CMS.

WidgetFactory: Could not resolve one or more widgets
http://www.mysite.com/Admin/Shell/7.7.0.0/ClientResources/epi/shell/widgets.js
Line 2

My first thought was that I was missing the latest version of the assemblies for the EPiServer UI, but after some investigation, deployment and configuration review I found out that everything seemed to be ok with the binaries and configuration for the web site. Another thing that indicated that everything was correct was that I did not find any direct information among the reported bugs that existed for EPiServer CMS. In other words, it was either an unreported bug or something with my settings.

Since I didn’t find a solution to my problem I let it be a little while, but being a developer means that it is hard to resist to fix broken things, so after a little bit of brainwork I found out that the problem ought to be in TinyMCE and its settings.

Solution

The problem where the custom property settings that I used for XHTML properties. By some reason the previous settings made TinyMCE stop working.

I solved the problem by creating a new property setting for the XHTML property in Admin Mode, and set it to be default for all XHTML properties. That solved my problem.

More information

You will find a list of solved bugs in EPiServer products here.
All current found bugs for EPiServer products can be found here.

Creating a MenuProvider in EPiServer CMS

EPiServer CMS 6 made it is possible to extend the main navigation that is being displayed in the user interface. Extending the navigation in EPiServer goes under the name Menu Providers and is one of the features in the OnlineCenter concept.

The code below is an example on how a menu provider could be implemented.

[SearchProvider]
public class MySearchProvider : ISearchProvider
{
    public string Area
    {
        get { return “My Area”; }
    }

    public string Category
    {
        get { return “Content Types”; }
    }

    public IEnumerable<SearchResult> Search(Query query)
    {
        var contentTypeRepo =
            ServiceLocator.Current.GetInstance<IContentTypeRepository>();

        return contentTypeRepo.List()
            .Where(a => a.Name.ToLower().Contains(query.SearchQuery))
            .Select(a => new SearchResult(“”, a.DisplayName, a.Description));
    }
}

Missing MIME types in IIS

When working with web applications that uses IIS you might see the following error in the console window for your web browser.

image

Since we are using custom typefaces on our website we need to provide several different fonts to target different devices and platforms. It’s quite common that a client wants to have their own custom font to be used in headings and teasers on their website.

The problem above is because of a missing MIME type mapping in IIS on the web server. The solution is to add the corresponding MIME type extension in the list of supported MIME types. In this case we needed to add the MIME type “application/x-woff” to the extension “.woff”.

image

Problem solved!

Read more:

List constraints for all tables in a database

I’ve been working on a project where I’m been responsible for creating a new relational database model. As the database model grew it became quite hard to get a clear picture over naming of constraints among other things, so I decided to create a script that listed all constraints using SQL Server Management Studio.

Below you can see my solution in how to accomplish this. Just remember to rename the MyCustomSchema to your schema name, or omit it if all your tables belong to the dbo schema.

 

DECLARE @table_name varchar(50)
DECLARE @current_table_name varchar(50)

DECLARE all_tables CURSOR  
FOR SELECT Name from sys.tables

OPEN all_tables

FETCH NEXT FROM all_tables INTO @table_name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @current_table_name = 'MyCustomSchema.' + @table_name
EXEC sp_helpconstraint @current_table_name
FETCH NEXT FROM all_tables INTO @table_name
END

CLOSE all_tables
DEALLOCATE all_tables

Placeholders for images and Lorem ipsum

Images

The following link is great when you need just a dummy image. Just put the link in your image source attribute and alter the width and height.

http://placehold.it/640×480&text=Lorem%20ipsum

Icons

Twitter has affected the way web sites are being designed today by giving us the Twitter Bootstrap framework. Along with Twitter Bootstrap comes Font Awesome which basically is lots of icons stored in a font.
http://fortawesome.github.com/Font-Awesome/

Text

When you need text to fill up your layout pages there are many options available. Take a look at the links below.

Lorem Ipsum
http://www.lipsum.com/

Samuel L. Ipsum
http://slipsum.com/

Useful commands when working with ASP.NET in IIS

As a developer I quite often need to e.g. restart IIS, stop or start a web application or list all started/stopped web applications or application pools on my machine. Internet Information Service (IIS) comes with a couple of handy command tools that makes it possible to create small scripts that starts or stops applications.

There are several ways to restart a ASP.NET web application hosted by IIS.

  • Make a minor change in web.config simply by adding a space character somewhere in the whitespace. IIS has listeners that keep track of the configuration files and when a change occur the web application pool for that web application will be restarted.
  • Use IIS to restart the web application or application pool used by the web application.
  • Execute the APPCMD RECYCLE APPPOOL “MyAppName”. This command will recycle the app pool used by the web application with the name MyAppName.

APPCMD

The APPCMD single command line tool can be used to administer sites, application pools, web applications, etc. in IIS. The tool can be found in C:\Windows\System32\inetsrv\ on most Windows machines. Read more about APPCMD here.

Sites

A site is the web application that usually has been created in IIS Manager. It is possible to add, delete, configure, start, stop and list the sites hosted by IIS on a machine.

List all web sites hosted by IIS on the current machine:

%systemroot%\system32\inetsrv\APPCMD\APPCMD LIST SITE

Stop and start a specific web site.

%systemroot%\system32\inetsrv\APPCMD\APPCMD STOP SITE “MySite”

%systemroot%\system32\inetsrv\APPCMD\APPCMD START SITE “MySite”

Web applications

A web application is hosted by a site and the name of the web application will either be the name of the site or a part of the URL that leads to the web application. It is possible to add, delete, configure and list web applications hosted by sites in IIS. Read more about the difference between sites and web applications in IIS here.

List all web applications hosted by IIS on the current machine:

%systemroot%\system32\inetsrv\APPCMD\APPCMD LIST APP

Application pools

In IIS application pools are being used to prevent one web application from affecting other web applications. If e.g. a web application experiences problems that web application will not bring down the other applications since it will be hosted by a separate application pool (normally). In older versions of IIS a single process took care of all web sites configured on that machine. The new architecture makes it more easy to maintain web applications and prevent them from affecting each other. It is possible to list, configure, add, delete, start, stop and recycle application pools in IIS on the current machine.

List all application pools on the current machine:

%systemroot%\system32\inetsrv\APPCMD\APPCMD LIST APPPOOL

Recycle an application pool on the current machine:

%systemroot%\system32\inetsrv\APPCMD\APPCMD RECYCLE APPPOOL “MyAppPool”

Worker processes

A worker process is used by application pools to execute the requests sent to the sites or web applications.

List all worker processes used by IIS on the current machine:

%systemroot%\system32\inetsrv\APPCMD\APPCMD LIST WP

 

IISRESET

IISRESET will stop and restart the web server service including all web applications hosted by IIS.

NOTE: IISRESET should be your last resort if you are working on a machine that is not in production. If you execute this command on a machine that is hosting other web applications (even non ASP.NET web applications hosted by IIS) you WILL restart them as well which might not be desirable.

Examples of usage of IISRESET

Restart the webserver on the current machine:

IISRESET

Or:

IISRESET /RESTART

Get status for the Windows services used by IIS:

IISRESET /STATUS

Stop the Windows services used by IIS:

IISRESET /STOP

Start the Windows services used by IIS:

IISRESET /START

Playing with Regular Expressions, part 2 – Find the first word in a sentence

In all the examples below I have used the same sample text (you will see it in the samples). I have used an excellent tool named Expresso to evaluate all expressions in this blog entry.

Problem

I want to find the first word in all sentences in a text.

Solution

Use the following pattern to find all the first words in a string without any formatting (HTML).

(?:^|\r\n|\.\s+)(?<myMatchedWord>\w+)

 

The pattern above will match the following words:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eu sem nisl.
Nulla elementum consectetur leo nec consequat. Vestibulum quis libero sit amet arcu euismod bibendum a.

Nulla elementum:    1389-89-1443

Praesent a nibh sed augue mollis vehicula.
Vestibulum nisl elit, eleifend a tristique nec, faucibus a sem.

Explanation

  • The part found in the beginning of the expression, (?:^|\r\n|\.\s+), will determine if the word occur in the beginning of the string or after a previous line in the sentence.
  • The pipe character has the same meaning as the OR operator in code. In the expression above we have three blocks that either one has to be true in order to successfully match the beginning of the sentence.
  • The first part of the expression, (?:^, before the pipe character instructs regex that we only want to match the pattern and not store it in the group of matches. We also states that we are looking at the start of the string by using the caret character.
  • The second part of the expression tries to match carriage return and line feed. We can add more OR cases if we like, e.g. if we only have carriage return or only line feed.
  • The third part after the pipe character, \.\s+, will search for the first word somewhere in a paragraph, e.g. after a previous sentence that ends with a dot and has one or more whitespace characters.
  • The second parenthesis tries to match the first word and will put the word found in a named group named myMatchedWord when a word is successfully matched.
  • When using named groups in regex you have to use the (?<myName>myExpression) construct.  

Playing with Regular Expressions, part 1 – Find the last word in a sentence

As a developer I quite often run into situations where I need to find an occurrence of a word or phrase in a text or some kind of number or pattern in a string. Regular expressions makes these tasks relatively simple and usually you will find loads of examples on how to match your specific pattern on the internet. This blog series will cover how to think when working with regular expressions. 

In all the examples in this blog series I have used the same sample text (you will see it in the samples). I have used an excellent tool named Expresso to evaluate all expressions in this blog entry. Of course it is possible to tweak the expressions in my examples below so that it searches other patterns as well.

Problem

I want to retrieve the last word in a sentence.

Solution

Use the \b anchor together with the pattern that ends the sentence to instruct regex that you only want the word that appears right before the dot-character, carriage return (and/or) line feed or the dollar-character that represent the end of the string.

The pattern below will match all the last words in each sentence.

(?<myNamedGroup>\b\w+)(?:\.|\r\n)

 

You should se the following result when executing the expression against the sample text.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eu sem nisl.
Nulla elementum consectetur leo nec consequat. Vestibulum quis libero sit amet arcu euismod bibendum a.

Nulla elementum:    1389-89-1443

Praesent a nibh sed augue mollis vehicula.
Vestibulum nisl elit, eleifend a tristique nec, faucibus a sem.

Explanation

  • The first parenthesis tries to match the first word and will put the word found in a named group named myMatchedWord when a word is successfully matched.
  • \b\w+ will match every word in the text that has at least one character. The \b anchor matches either the beginning or the end of a word.
  • (?:\.|\r\n) will match either a dot or the combination of carriage return and line feed. The (?: part of the expression tells regex to skip the pattern in the matched content.
  • The whole expression together will return all words where the first part of the expression is combined with one of the expressions found in the second part.

Working with comma separated strings in t-SQL

Problem

When working with t-SQL you might run into a scenario where you want to return all the records that corresponds to the IDs found in a comma separated list, e.g. ‘123,456,789’. There are several ways to solve this problem and while browsing around I found an interesting solution.

Solution

In the example below I take advantage of the CHARINDEX and PATINDEX functions in t-SQL to search for specific patterns in the comma-separated string by concatenating the customer ID with commas and then match the concatenated string with the id string. The functions returns a number  greater than 0 if the sought string exists in the ID string which can be useful in my scenario where I got a comma separated list with customer IDs.

CHARINDEX works similar to the Substring or InStr functions that you can find in other programming languages. The function takes three parameters and the first is the string to search for, the second is the string to search and the third (which is optional) is the starting point where the search will begin in the string to search.

PATINDEX works similar to CHARINDEX, but allows the use of wildcards when performing the search. The function takes two parameters and the first is the string or pattern to search for and the second is the column or variable that will be searched. The method returns an integer bigger.

The example below also show that LIKE can be used to search the comma-separated string for specific IDs using a concatenated value along with wildcards.

USE [master]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE DATABASE [WeirdScience]
GO

USE [WeirdScience]
GO

CREATE TABLE [dbo].[Customers](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Customer] [nvarchar](50) NOT NULL
) ON [PRIMARY]
GO

INSERT Customers (Customer) VALUES (N'Dan Jansson')
INSERT Customers (Customer) VALUES (N'Lars Svensson')
INSERT Customers (Customer) VALUES (N'Lisa Karlsson')
INSERT Customers (Customer) VALUES (N'Kalle Johansson')
INSERT Customers (Customer) VALUES (N'Gullan Bengtsson')
GO

DECLARE @customerstring varchar(max);
SET @customerstring = '1,3,4,6'

-------------------------------
-- This example is based on the following article:
-- http://www.projectdmx.com/tsql/sqlarrays.aspx
-------------------------------

SELECT *
  FROM Customers
 WHERE CHARINDEX(
	',' + CAST(ID AS NVARCHAR(50)) + ',', 
	',' + @customerstring + ','
	) > 0
 
SELECT *
  FROM Customers 
 WHERE PATINDEX(
	'%,' + CAST(ID AS NVARCHAR(50)) + ',%', 
	',' + @customerstring + ','
	) > 0

SELECT *
  FROM Customers
 WHERE ',' + @customerstring + ',' 
  LIKE '%,' + CAST(ID AS NVARCHAR(50)) + ',%'

USE master
GO

DROP DATABASE WeirdScience
GO

For more ideas on how to work with arrays in SQL Server you should take a look at the excellent article found here: http://www.projectdmx.com/tsql/sqlarrays.aspx

Shrinking the Transaction Log in SQL Server

Problem

Well, this is a classical problem: The transaction log has been growing since who knows when and is now taking up almost all space on the server. You need to shrink the transaction log in order to free space on the server.

Solution

There are several solutions to this problem and it all depends on your database. This time I used the following script to get rid of all data in the transaction log and worth mentioning is that I didn’t care about the data in the transaction log; my goal was only to get rid of the unnecessary data and to reduce the size of the transaction log.

USE [MyDatabase]  
GO
ALTER DATABASE [MyDatabase]
SET RECOVERY SIMPLE;
GO
DBCC SHRINKFILE ([MyDatabase_Log], 1);
GO
ALTER DATABASE [MyDatabase]
SET RECOVERY FULL;
GO

The script above worked fine for me and you will find similar solutions on the internet. If you analyze the script you will see that it changes the recovery model from FULL to SIMPLE which means that the database will not longer log transactions that occur in the database. After the change we execute the DBCC SHRINKFILE command that will try to shrink the file stated by the logical filename (MyDatabase_Log) to the desired size in megabytes (the second parameter to the DBCC command). When the file has been shrunk we restore the recovery model to FULL that was the original setting for the database; of course this all depends on your scenario, but my guess if you read this is that you are using the FULL recovery model. 

While looking around I found another older solution that were used earlier to accomplish sort of the same thing, however it’s recommended not to use the approach mentioned below.

USE [MyDatabase]  
GO
DBCC SHRINKFILE([MyDatabase_Log], 1)
BACKUP LOG [MyDatabase] WITH TRUNCATE_ONLY
DBCC SHRINKFILE([MyDatabase_Log], 1)
GO