Introducing SPUrlExpressionBuilder to resolve SharePoint URL Tokens


Introduction

One of the great things when developing with SharePoint is being able to use URL tokens such as:-

  • ~SiteCollection – this will resolve to the current site collection root
  • ~Site – this will resolve to the current web root

For an extensive list of SharePoint URL tokens, take a look at Namwar Rizvi’s SharePoint Insight blog post, http://sharepoint-insight.com/2008/12/01/list-of-sharepoint-url-tokens/.

If you have done much with creating or modifying Master Pages or Page Layouts then these tokens become very useful to locate resources such as images, XSL or CSS files.

For example, to reference an image which is stored in a SharePoint Publishing web’s Site Collection Images folder you could use something like this:-

~SiteCollection/SiteCollectionImages/myimage.png.

Recently, I have been working with multiple language user interfaces and wanted to be able to display a different image based on the language. One of the steps to do this was to work out how to process these URL Tokens.

Solution

This is where the SPUrlExpressionBuilder class comes to the rescue. This class is part of the Microsoft.SharePoint.Publishing.WebControls assembly.

To evaluate a string with a url token like ~SiteCollectionUrl/Images/MyImage.png you can use the following SPUrlExpressionBuilder class’s method EvaluateUrlExpression().

string imagePath = SPUrlExpressionBuilder.EvaluateUrlExpression("~SiteCollection/Images/myimage.png").ToString();

 

Fix: Content Editor Web Part Rich Text Editor Popup fails to display


 

Introduction

This problem has been bugging me for the past 3 days and so I thought I’d be kind and document it just in case it has been causing someone else problems.

So the issue is with the SharePoint 2007 Content Editor Web Part and when you edit the web part and click the button to show the Rich Text Editor you get a JavaScript error.

There does seem to be another post on MSDN (http://social.msdn.microsoft.com/Forums/en-NZ/sharepointcustomization/thread/90e14ff3-7003-44fd-9b4c-d9e654f7d8e1 that was showing the same issue and although the fix was related, the fixes that were specified in this post didn’t help me.

 

Issue

So as I have already mentioned when using the SharePoint 2007 Content Editor Web Part’s Rich Text Editor Dialog you get the following JavaScript error:-

Invalid Argument in HtmlEditor.js at line 5740, character position 2

On further investigation it is this line in the HtmlEditor.js file that is causing the problem.

elem=document.getElementByid(containerId);

Actually if you follow the JavaScript code then the issue seems to be down to the piece of JavaScript where the web parts are registered on the page. At the bottom of each SharePoint page is a section of JavaScript that looks something like this:-

 1: <script LANGUAGE='Javascript'>


 

 2: <!--


 

 3: WPSC.Init(document);


 

 4: var varPartWPQ1 = WPSC.WebPartPage.Parts.Register('WPQ1','[GUID]',document.all.item('WebPartWPQ1'));


 

 5: WPSC.WebPartPage.WebURL = 'http:\u002f\u002fsharepointurl';


 

 6: WPSC.WebPartPage.WebServerRelativeURL = '\u002f';


 

 7: //-->


 

 8: </script>

It seems that the JavaScript which creates the varPartWPQ1 variable does not work properly as the varPartWPQ1 is created without populating its ID property. This ID property is the value that is assigned to containerId in the line:-

elem=document.getElementByid(containerId);

 

The Solution

After a lot of trial and error I found that it was to do with the master page and a <meta> tag within the master page’s <head> tag.

The tag causing the problem is this one:-

 1: <meta http-equiv="X-UA-Compatible" content="IE=8" />

Removing this line and redeploying the master page fixed the JavaScript error.

This meta tag tells IE to run in IE 8 Standards mode which seems to cause a problem with the browser when its trying to get a document item using the function document.all.item([itemid]).

For more information on the <meta> tag please see the following article on Defining Document Compatability (http://msdn.microsoft.com/en-us/library/cc288325(VS.85).aspx)

 

Further Information

The reason for this happening was actually that the master page was being ported back from a SharePoint 2010 master page to a SharePoint 2007 master page.

This tag was left in from the SharePoint 2010 master page.

I really do hope this helps someone as it drove me mad for a while.

Issue with Updating SharePoint Publishing Page Layouts and Ghosting


Introduction

This post has come about due to some challenges when upgrading a publishing site page layout.

Before we delve into the issues I best explain how the Page Layouts are setup. The page layouts are all contained within a feature called “Company.Intranet.Publishing” which are deployed to the folder:

SharePointRoot\Template\Features\Company.Intranet.Publishing\PageLayouts.

Each of the page layouts are deployed into the master page gallery (/_catalogs/masterpages) using a Module. The page layouts are deployed as uncustomised or ghosted files.

The fact these resources are deployed as ghosted files is very important as you will see in the next section.

Finally the feature is deployed using the SharePoint Solution framework as a single SharePoint WSP using WSP Builder.

What do you mean by Ghosted / UnCustomised files?

There has been quite a lot of information published about this and the following are good resources:-

My understanding is that a ghosted file is where the binary content is actually stored on the file system of the server and a pointer to that binary content is held in the content database. If a user modifies the content of a page layout in a site collection by a tool such as SharePoint Designer then a copy of that binary content is taken from the file system and merged along with the changes made by the user and stored in the content database.

Hence when you retrieve an uncustomised file its binary content is being pulled from the content database.

Why are Ghosted / Uncustomised files preferred?

Recently at the SharePoint Best Practices conference, Eric Shupps and Rob Foster did a great presentation on Fine Tuning and Optimising SharePoint Performance. One of the key points in improving performance is to ensure that files are ghosted/uncustomised for the following reasons:-

  • Ghosted files all point to a single file and hence they all point to the same binary content. This means only one file needs to be cached and the load on the database is significantly reduced.
  • Actually Unghosted files cannot be cached within a BlobCache. Unghosted files are therefore slower to load and render (10% slower on average) than a ghosted file.

Upgrading Page Layouts

The method used to upgrade page layouts is simply by redeploying the files through an updated wsp solution file. We have also been careful through training and guidance with users to ensure that page layouts are not edited through SharePoint Designer.

Therefore if a change to a page layout is required, then the page layout is updated within the feature directory using Visual Studio and the WSP solution file is rebuilt.

The solution is then ready to be updated using the command:-

stsadm –o upgradesolution –filename [solutionname.wsp] –name [solutionname.wsp] –allowgacdeployment –allowcaspolicies –immediate

The change to the page layout is then reflected within the SharePoint site.

Why are my changes are not seen?

More history, our last deployment required that we tidied up the page layout’s title and description.

One of the problems with using a Module to deploy files within a feature is that with SharePoint 2007 it is difficult to make changes to existing files deployed by a module without resorting to code. Any changes to the filenames or properties that are deployed by the Module are not reflected until the file is deleted from the site collection.

As there were not that many site collections, we took the decision not to write any code and we modified the page layout titles and descriptions through the SharePoint UI.

Unfortunately when we did our next release into our testing environment the changes to the page layout markup were not seen within the site.

After checking that the page layouts were being deployed correctly to the the WFE’s file system we started thinking of the reason. Finally I thought it must be due to unghosting of the files.

Normally you can use SharePoint Designer to revert page layouts to their site definition. So I fired up SharePoint Designer and browsed to the /_catalogs/masterpages document library but could see that the Page Layouts were not customised.

I started to search for tools to find out which files are customised. The legendary Gary LaPointe (@glapointe) came to the rescue.

If you don’t deploy Gary LaPointe STSADM tools on your Production/UAT environments then I plead with you that you start to do so as soon as possible. Time and time again I have been in a bind and Gary’s STSADM commands have got me out of trouble.

The commands that saved the day this time are:-

The commands give a lot of flexibility, though I will warn you that you need to be a bit careful as you could quickly reghost all the files within your site collection if you are not careful.

I performed the following command:-

stsadm –o gl-enumghostedfiles –url http://sharepointsite.domain.com/

This displayed a list of files that were unghosted. Funnily enough all the page layouts that we had modified and changed their Title and Description fields had become unghosted!

Once we had a list of the unghosted page layouts we could reghost each of them in turn using the command:-

stsadm –o gl-reghostfile –url http://sharepointsite.domain.com/_catalogs/masterpage/RandomPageLayout.aspx –Scope File

This command would reghost each of the files, there were occasions where the page layout reghosting would fail, using the –force option for the gl-reghostfile would ensure that the page was ghosted.

Now looking at the site the page layouts had been updated and reflected the changes that had been deployed by the update solution.

Hope that helps!