Aidan Garnish

Collaboration Not Competition

Setting up a SharePoint custom error page

The process for creating a custom error page for SharePoint is different from how you would normally set one up for a website hosted by IIS.

First create your custom error page as an html document.

Save the custom error page to the following location:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033

The error page then needs to be assigned to the web appication it is to be used for. Because the custom error page is assigned at the web application level you can create a different page per web application if required.

The assignment is done with the following code:

  Microsoft.SharePoint.Administration.SPWebApplication webapp =
            Microsoft.SharePoint.Administration.SPWebApplication.Lookup(new Uri([site url]));
            webapp.FileNotFoundPage = [errorpage.html];

Replace [site url] and [errorpage.html] with the relevant values for your site and your error page.

To test that this has worked try to navigate to a page on your site that does not exist. Rather than seeing the standard page not found error you should now see your custom page. 

Delete all items in a SharePoint list more efficiently

One way to remove all items from a SharePoint list is to iterate through every item and call delete like this.

However, a more efficient way to clear down an entire list is to use the ProcessBatchData method like this:

private void deleteAllListItems(SPSite site, SPList list)

  StringBuilder sbDelete = new StringBuilder();

  sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

  string command = "<Method><SetList Scope=\"Request\">" + list.ID + "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar     Name=\"Cmd\">Delete</SetVar></Method>";

  foreach (SPListItem item in list.Items)
    sbDelete.Append(string.Format(command, item.ID.ToString()));



Trouble shooting SPWeb object closed or disposed

I recently came across an issue on a WCM site where the following error was being shown whenever a dropdown selection was changed on a custom web part:
SPException: Trying to use an SPWeb object that has been closed or disposed and is no longer valid 

This looked like the SPWeb object was being incorrectly disposed of but careful inspection of the web part showed that best practices for disposal of SPObjects had been followed .
To try and track down the cause of the error and to really satisfy myself that it wasn't the web part causing the issue I removed all references to SPWeb from the custom web part. The error was still appearing so this ruled out bad coding practice in this control.
The next step was to take a look at the other controls on the page. As this was a WCM site there were a number of custom controls being used for navigation and other functionality.
It didn't take too long to track down the culprit in a control being used to set the site title in the browser title bar. This contained the following:
using(SPWeb web = SPContext.Current.Web)
   //some code
When the dropdown selection was changed a postback was fired and this piece of code brought the site crashing down.
This was because both the using statement and the use of SPContext handle garbage collection so the SPWeb object was being disposed of twice and as it didn't exist on the second dispose the error above was thrown.
Once corrected the code looked like this:
SPWeb web = SPContext.Current.Web;
//some code 

Most of the links about this error on the web will point you towards the issue being incorrect disposal of objects but what is learnt here is that it might not be a coding issue in the control that seems to be causing the problem. In this case it was the postback being fired by the control rather than the control itself that was triggering the error. The root cause was actually a completely different web part on the page.

SharePoint projects are like vegetables

SharePoint is often marketed as a way for businesses to deliver results quickly. The breadth of capabilities in SharePoint is impressive straight out of the box – collaboration, search, business intelligence, web content management and the rest. Because you get all of this without any custom development it is possible to deliver SharePoint solutions to the business very quickly without having to write a line of code.

At this point the title of my post may seem a little misleading. SharePoint and vegetable growing are definitely not similar in terms of speed of delivery or quick wins but bear with me.

I recently planted some vegetable seeds and I can now tell you from my limited gardening experience that it takes a while for lettuce, potatoes, chillies and tomatoes to make progress from being tiny seeds to being little shoots. Delivering results in the form of tasty things I can eat still seems a very long way off.

In a world where we are seemingly obsessed with instant gratification growing your own vegetables forces you to slow down and accept that whilst there are things you can do to encourage growth there is also a natural cycle that cannot be rushed.

Whilst sitting in the garden wondering how long it was going to take before I could make a meal from the things I was growing I started thinking about how different this was to how I spend my rather more fast paced working life delivering SharePoint solutions. However, the more I thought about it the more similarities I saw. A truly successful SharePoint implementation isn’t going to survive based on those initial quick wins it is going to be judged on the value that it provides to the business over time as it continues to grow and develop to maturity. I started thinking about the SharePoint projects I have been involved in and particularly those that involve intranets and lots of business users. Whilst SharePoint does provide a huge amount out of the box that can produce quick wins and deliver fast results there also seems to be a natural cycle to SharePoint success that cannot be rushed, although there are things you can do to speed it up.

Thanks for making it this far, I promise that this is where the similarities between vegetable growing and SharePoint start to become clearer, in my slightly warped mind anyway. In any SharePoint or gardening project there is initially a flurry of activity where the infrastructure or garden is prepared. Any existing content or compost is migrated from the current infrastructure or garden to provide a useful or fertile environment for users or seeds to become a part of.

The users or seeds are then given access to the content or compost and the first shoots of use or growth appear. At this point everything looks good and you could be fooled into thinking that your job as an IT professional/gardener is over, think again. Those users or seeds are going to need supporting or watering and some of them are simply going to lose faith with the project or wither and die unless you provide training or protection from the elements.

Over time the content or garden can become a bit of a mess and data cleansing or weeding is necessary otherwise your users or vegetables will not be able to get to the content or nutrients they need to prosper and meet their full potential. As with all SharePoint project or gardening tasks this is not something you can just do once and tick it off a list. It is a never ending process of supporting or watering, training or protecting, removing clutter or weeding.

Often a SharePoint project is seen like any other IT infrastructure project that has a start and an end date which will produce immediate results as soon as the magical project end date is reached. This is the gardening equivalent of digging over your vegetable patch, adding compost and nutrients to the soil and then going back indoors thinking your job was done. People are then surprised when nothing grows and the vegetable patch is still empty come the summer. When this happens in a SharePoint project it is usually the technology that gets the blame for underperformance rather than a lack of governance or assurance plan.

The concept of SharePoint as an ongoing process rather than a fixed term project is crucial to the success of SharePoint within an organisation. Although there are quick wins to be had when you first implement SharePoint depending on your circumstances the tastiest rewards will only come in time if you carefully tend to your installation and feed your users the right mix of training, knowledge and support.

WCM SharePoint utilities

I have created a number of small utility console applications and web parts that are useful for quickly completing a few fairly common tasks - well if you are a SharePoint dev anyway. They are all fairly rough and ready but the project code can be downloaded from the link below if you want to make your own additions/improvements.

These applicatons all have the power to make lots of changes to your site collection very quickly so use them at your own risk - backup your site and always test them in a non-production environment first, you have been warned!

Download projects here

Modify Workflows

Update the properties on approval workflows. This is particularly useful for WCM sites where each subsite has it's own Pages library and workflow by default. This means that once you have a decent sized site collection hierarchy it could take a very long time to go through every site and change the workflows. This app allows you to quickly update properties such as whether users can change who the approver is when submitting a page for approval and whether a single task is assigned to a group. This is a console application so settings are changed through the app.config.

Uncustomize Files

Customized files are the plague of any SharePoint developer who wants to be able to update page layouts, master pages or any other file that makes up a SharePoint site. A customized file is a file that has been modified in SharePoint Designer and is therefore different to the original site definition file that was used when the site was created. A customized file stores it's customizations in SQL so the file on disk is no longer used which means your update of the file on disk is ignored. If you are wondering why your updates are not overwriting the files on your site then it is worth checking to see if the file has been customized. If it has then you will probably want to find out why and what the customization is and then maybe revert it to it's uncustomized state. The UncustomizeFiles console application allows you to do just that.

WCM Control Panel

Quite a grand title for something that at the moment only does two things - if anyone has any ideas for what else they would like to see on a WCM control panel please leave a comment. This web part will tell you how many pages there are in a WCM site collection, something I had hoped CQWP would do but unfortunately not (if anyone knows of a better OOB way of getting this information please let me know in the comments). It will also list out the number of pages in each individual subsite if you modify the web part settings.

The second function this web part has is to Update All Pages which simply checks out every page and then checks it back in then publishes and approves the page. This is a bit of a cheat for people who want to make their content look more up to date than it is for Internet sites where a last updated date is displayed on each page. In reality this is actually quite useful for launch day to be able to create the right impression for users - especially if you have had a particularly long content creation cycle before going live.

These projects can be downloaded here

Viewing a SharePoint page layout results in 404 error

Trying to open some pages in a SharePoint WCM site gave a 404 error.

A quick Google search led to this explanation but this was not the exact issue for me. In my case there was a reference to a user control .ascx file in the page layout that had not been deployed to the bin folder of the web application. Once this resource was deployed to the path the page layout expected to see it in the page loaded without a problem.

Update - another reason for this error is if your page layout has become customized. This will happen if you edit the page layout using SharePoint Designer.

Deploying a wsp solution file to MOSS 2007

I recently wrote a post that included a .wsp file. In the comments a reader asked for more information on how to deploy this to their SharePoint environment.

I suppose it is a sign that I have been developing SharePoint solutions for too long when I start to assume that this is basic knowledge that everybody has! To try and make amends for this assumption here is a quick guide to deploying a wsp using stsadm and central admin. The first part is generic to all .wsp solutions and the second part is specific to the deployment of a web part that has been added using a SharePoint solution.

First we need to use STSADM.exe to add the .wsp file to the SharePoint farm.

To find STSADM.exe navigate to the bin folder in the 12 hive - here C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN

Open a command window and drag stsadm.exe onto it and use the following command to add the solution to the solution store.

stsadm -o addsolution -filename c:\solution.wsp

You can also use stsadm to deploy the solution but here I am going to describe the point and click method using SharePoint Central Admin which does the same thing.

Open SharePoint Central Admin and select the Operations tab.

Select Solution Management.

Click the link to the .wsp solution you just added.

Click Deploy Solution - If the solution is not globally deployed select the web applications you want to deploy to.

Click OK.

The solution is now deployed to the web applications you selected or to all web applications if it deployed globally. This is not the end of the deployment process though. If the solution contains a web part there are still a few steps to go before it will be displayed on your site. To add the web part to a page on your site do the following:

Navigate to the site collection where you want to place the web part and go to Site Settings and Site Collection Features (this assumes that the feature is scoped at the site collection level, sorry for making more assumptions that you understand scopes but that is another post and something that other people have already written plenty about if you want to find out more).

Activate the feature that contains the web part that has been deployed in the solution.

Next, go to Site Settings and Web Parts and click New.

Select the web part to add it to the wbe aprt gallery for that site collection.

The web part is now ready to be added to a page in the usual way - edit page, add web part to zone, browse to web part.

Hope that helps somebody...

Twitter SharePoint web part - another update!

** Update June 2010 - new Twitter web part using OAuth rather than Basic Auth **

When I mentioned that I was developing a Twitter web part to Mirjam van Olst she asked if it would be possible to work on it with me. So thanks to Mirjam, in the true spirit of international collaboration, we are now able to bring you an updated version of the web part which is more secure and easier to deploy.

Improvements include -

  • The password input now uses a password textbox to protect the Twitter credentials
  • TwitterLib.dll has been added to the wsp for easier deployment

Download the new and improved Dutch/British/Euro version here: TwitterPublicTimeline.wsp (24.25 kb)