Aidan Garnish

Collaboration Not Competition

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.