Archive for the ‘SharePoint’ Category

Restoring site or list from a full farm backup

June 12, 2011

This post covers a scenario, when a user makes some irreversible changes and the only place where the undamaged data is, is the farm full backup. The scenarios can be list deletion, site deletion, permissions damage etc.
So here are the instructions:

  1. Locate the folder where your farm full backup is and open the file spbackup.xml
  2. Find the content database name in the file and in the found node  locate a line that looks like this:
    <SPParameter Key="SHAREPOINT2010:WSS_Content.dat"><![CDATA[00000055.bak]]></SPParameter>
  3. 00000055.bak is the name of the database backup of the content database.
  4. Go to SQL Management Studio and create a new database.
  5. Perform a restore into this database from the backup file mentioned above. You have to change the name of the ldf and mdf file and check the “overwrite the existing database” checkbox.
  6. Now open the Central administration. Click “Backup and Restore” tab and then “Recover data from an unattached content database” link.
  7. Type in the name of the currently restored database and click Next.
  8. Navigate to a list / site collection that you want to restore and click Next.
  9. On the following page type in where you want to store the exported data of the list / site collection and click Next.
  10. When the backup is created use the Import-SPWeb command to restore the data. The command should look like this:
Import-SPWeb -Path C:\temp\backup.bak -Identity http://localhost

RunWithElevatedPrivileges throws Access Denied Exception

May 8, 2011

There are more reasons why you can receive this exception. The obvious one is that you use SPContext in the elevated block. For more info see this post . I will  describe a less obvious reason in following article. The RunWithElevatedPrivileges method runs the code under the account of the application pool. In the productive environments has the application pool account only limited privileges. Generally the permissions are so defined that you can do anything you want in the current site however if you try to run e.g following code:

var webApp = SPWebApplication.Lookup(new Uri("http://localhost/sites"));
webApp.Sites.Add("http://localhost/sites/", "user", "email");

you will receive the access denied exception. So how to solve this problem:

  1. Define an account that has only the needed permissions to perform the desired operation. In this specific scenario, you can define a user that has self service site creation permissions and modify the code that it uses self service site creation.
  2. Run the code, that throws exception under the specified account. You can achieve this for example so, that you open the SPSite impersonated. See this.

SSD Disks and SharePoint 2010

February 8, 2011

I have recently set up a new virtualized SharePoint development machine with a SSD Disk (Model Kingston 128GB SSDNow V-Series V+ SATA2 2.5).  I measured the time it takes to load a start page of a site collection after iisreset. The virtualization software was virtual box. The tested site collection was Enterprise Wiki. Here is the configuration of the systems:
Host system

  • Windows 7 64bit
  • 8GB RAM

Virtualized Server

  • Windows 2008 Server 64bit R2
  • SharePoint 2010
  • 4GB RAM

Here are the results

System Time (seconds) 
with SSD disk 8
with standard disk 32 

Pretty impressive

Quick tips for using Powershell in SharePoint

October 12, 2010

Here are some helpful powershell commands that I am afraid that I will forget:

This powershell command will install PowerShell ISE. A Pretty tool for editing powershell scripts:

Import-Module ServerManager;Add-WindowsFeature PowerShell-ISE

This powershell command will enable SharePoint commands:

Add-PsSnapin Microsoft.SharePoint.PowerShell

This command opens a new SPSite and root SPWeb

$spsite = new-object Microsoft.SharePoint.SPSite(“http://localhost&#8221;)

$spweb = $spsite.OpenWeb()

Creating Loops in SharePoint Designer 2010 Workflows

October 7, 2010

By default SPD 2010 does not allow possible infinite loops to be added to a workflow. Here is the guide how you can do it:

  1. Open SPD 2010
  2. Add a new Workflow
  3. Add new IfElseActivity and fill in the conditions and the actions you want to loop through
  4. Save the workflow
  5. Go to Alle Files -> Workflows -> Your workflow name  folder and open  “your workflow name.xoml” file as xml.
  6. Locate the lines that look like this:

<IfElseActivity x:Name="ID411">

<IfElseBranchActivity x:Name="ID412">


<RuleConditionReference ConditionName="__Rule_ID412" />


And replace if with something like this:

<WhileActivity x:Name="ID411">


<RuleConditionReference ConditionName="__Rule_ID412" />



Keep in mind that you have to remove also the ending tag of the IfElseBranchActivity.  Keep also in mind that if you have multiple activities in the IfElseActivity taht you have to encapsulate these activities into one single SequenceActivity.

Now you can Publish your workflow back to SharePoint.

One drawback of this approach is that SPD does not support designing of a whileactivity in the worklow designer so you have to change the activities direct in the xoml file or you can replace the whileactivity again with ifelseactivity, do your changes and replace it again.

You have to also modify the web.config of the web app where you want to deploy your workflow because by default is the WhileActivity disabled. Locate following line

<authorizedType Assembly="System.Workflow.Activities, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Workflow.*" TypeName="WhileActivity" Authorized="False" />

and set the “Authorized” attribute to True.

Pex and Moles Testing Framework for SharePoint

September 1, 2010

Here is a quickstart for working with unit testing framework for SharePoint. This Framework is called Moles. You can download it here. It is free academic use and Microsoft partners.

So how this Framework works: It allows you to replace any method with by you defined delegate. So here is a simplistic function:

Simple Intro

public static bool IsInFuture(DateTime date)
	return date > DateTime.Now;

So this function is not easily unit testable, because the DateTime.Now is not a constant value. What we can do with moles is following:

public static void IsInFutureTest()
      MDateTime.NowGet = () => new DateTime(2010,09,01);
      bool actual = SuperClass.IsInFuture(new DateTime(2010, 09,03));

The interesting thing here is the MDateTime construct, that comes from moles and the function replaces the actual method call with by us defined function that returns a constant date.
So here is what we have to do when we want to use the MDateTime construct:

  1. Install Pex and Moles.
  2. Add a new item of type “Moles and Stubs for Testing”  into the unit test project.  This file has to be called mscorlib.moles, because  the DateTime class is located in the mscorlib.dll. When you add this file the moles Framework analyses the dll and create the stubs. These stubs are the classes with the “M” prefix.
  3. Add following attribute [HostType(“Moles”)] to the test method. This tells moles framework to replace the actual methods with custom delegates.
  4. For this type you have to also add following row to the assemblyinfo.cs of the test project [assembly: Microsoft.Moles.Framework.MoledType(typeof(DateTime))]

SharePoint Unit Testing

You can stub in this way all SharePoint objects e.g. SPContext, SPList etc. All you have to do is to add new moles file with the name Microsoft.SharePoint.moles. This has actually one drawback and it is that it is a lot of coding to fullfill all the expectations that you use in the method to be tested. There come behaviors for SharePoint in play. It offers stubs with predefined behaviors against you can unit test your code. In all these cases you  don’t access the physically SharePoint, in extreme there can be no SharePoint installed at all.

So where can you find Behaviors for SharePoint. They are part of the Moles project and you can find it on the machine where moles are installed on the following path \Program Files (x86)\Microsoft Moles\Documentation\\behaviors\Microsoft.SharePoint.Behaviors. All You have to do is to compile this project and you have an assembly called Microsoft.SharePoint.Behaviors.dll that you can reference in your test projects. Here is a short sample how you can work with SharePoint behaviors:

public static void  IsListNameCorrectTest()
BSPSite site = new BSPSite();
BSPWeb web = site.SetRootWeb();
BSPList list = web.Lists.SetOne();
list.Title = "TestList";
bool actual = MyClass.IsListNameCorrect(list);

public static class MyClass
public bool IsListNameCorrect(SPList list)
return list.Title.Length < 200;

Changing column type in a list template – upgrade scenario

July 29, 2010

In this article I would like to describe the scenarios that you can come across when changing  the structure of a list.

Changing column type

Consider following scenario: We have a list that should hold information about a person. the person has following fields: first name , last name and birth date. The part of schema.xml file, that defines the columns could look like this:

<pre><Field Type="Text" DisplayName="Firstname" Required="FALSE" ID="{168a8095-32a8-4b25-a4b6-046b3dc3d466}" StaticName="FirstName" Name="FirstName"Version="1" ColName="nvarchar1"/>
<Field Type="Text" DisplayName="LastName" Required="FALSE" ID="{168a8095-32a8-4b25-a4b6-046b3dc3d466}" StaticName="LastName" Name="LastName"Version="1" ColName="nvarchar2"/>
<Field Type="Text" DisplayName="BirthDate" Required="FALSE" ID="{168a8095-32a8-4b25-a4b6-046b3dc3d466}" StaticName="BirthDate" Name="BirthDate"Version="1" ColName="nvarchar3"/>

So far so good. Now we released this list template and the users happily created lists based on this template and filled thousands list items into these list. But there is an error in the xml: the birth date should not be of type text but DateTime. So we modify the schema.xml accordingly:

<Field Type="Text" DisplayName="Firstname" Required="FALSE" ID="{168a8095-32a8-4b25-a4b6-046b3dc3d466}" StaticName="FirstName" Name="FirstName"Version="1" ColName="nvarchar1"/>
<Field Type="Text" DisplayName="LastName" Required="FALSE" ID="{168a8095-32a8-4b25-a4b6-046b3dc3d466}" StaticName="LastName" Name="LastName"Version="1" ColName="nvarchar2"/>
<Field Type="DateTime" DisplayName="BirthDate" Required="FALSE" ID="{168a8095-32a8-4b25-a4b6-046b3dc3d466}" StaticName="BirthDate" Name="BirthDate" Version="1"/>

After the activation of the feature the SharePoint will update all columns in the list instances. However it is very probable that the existing list items will not be editable. In my case I become “Input string not in a correct format”. And if you e.g. convert  a column from DateTime to Text you can receive even a CATASTROPHIC_FAILURE. Cool. So this is not the way how to fix it.

Deleting column

If you want to create a robust scenario for updating columns do not ever change the type of the column, instead mark it as hidden and create a new field e.g. BirthDate2 with the correct data type. Then after the installation and activation of the feature you have to iterate through all the existing list instances of the type and transfer the data programmatically from the old to the new field, wherever possible. If you have a temptation to delete the old column and create a new column with another type and guid, be advised that SharePoint will update the list instances accordingly. This means in our case it would delete all data in the Birthdate column in all these lists. This is really dangerous side effect of updating the lists.

Simple advice

Don’t screw up in the first place, because it could be really time consuming to make it right.

Exception 0×80040E14 occured in SPListItem.Update()

May 8, 2010

I ran into this exception on a user defined list. The problem was with the custom fields that were defined in the schema.xml of the list template. It was done in the following way:
      <Field … Type=”Text” DisplayName=”CustomField1″ ” ColName=”nvarchar3” />
      <Field … Type=”Text” DisplayName=”CustomField1″ ” ColName=”nvarchar3” />
The problem is the these two different fields link to the same ColName. So if you run into this exception double check that the values of ColName attribute are unique through the list.

Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))

March 1, 2010

What is th reason that this exception shows?
It is usually caused by incorrect disoposing of SharePoint objects.
So what to do when this exception appears?
First thing to do is to look into the SharePoint log and look for something like this

Potentially excessive number of SPRequest objects (29) currently unreleased on thread 1.

There shoud be a call stack log and there you can sometimes find the function that does not correctly release the resources.

Here are some catches tha I found in our code that caused this problem
Iterating to parent web

This is BAD!

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
 using(SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
  while (web.ParentWeb != null)
   //do you stuff
   web = web.ParentWeb;

This will actually dispose of the root web and not the web you actually opened.

This is also BAD!

SPList GetList(string listName)
    SPList result = null;
    SPSecurity.RunWithElevatedPrivileges(() =>{
        using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                result = web.Lists[listName];
    return result;

The disposing is in this case OK, but if you want to use the list outside the function. It can happen (and it will happen) that when you call the list it will try to access the already disposed web and it can also cause this exception.
There are several other reasons why this exception happens and thery are listed here:
Incorrect webtemp.xml template: here
Another problems with disposing described: here

SharePoint Solution Installer support for SP2010

February 28, 2010

I modified the SharePoint Solution Installer and now it supports SharePoint 2010.

I added following config entry into the setup.exe.config

<add key=”SupportedSharePointVersion” value=”2007″/>
If it contains value “2007”  then the solution can be installed only to MOSS 2007 and WSS 3.0

If it contains “2010” the the solution can be installed only to SharePoint 2010 and SharePoint Foundation.

If you leave it empty then no check for specific version of SP is done and you can install it to all above versions.

You can download the modified SSI binaries and source code from here. (It is actually a zip file renamed to jpeg because of WordPress policy.)