Latest Entries »

NOTE: this post corrects the incorrect advice given out by Microsoft – read this before you try using any MS blogs to fix this issue.

I’m sure we’ve all come across this error once in a while when launching SharePoint 2010 Management Shell – mostly because (as security-conscious devs and admins) we’ve got UAC on and have not chosen to run the shell as an administrator. Then there’s the other reason, which is the lack of SharePoint_Shell_Access role assignment in SQL Server for the logged in user. These are both extensively documented on the internet, in places such as Bradley Schacht’s blog.

However, there is now a NEW cause of this error!

The cause is documented on this MS blog and is correct. However, the solution provided doesn’t work. When you put in the “-version 2” it should come before the -NoExit switch. i.e. the ‘Target’ property of the shortcut should read:

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -Version 2 -NoExit " & ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1 ' "

If you try and use the Microsoft recommended solution you get an error saying “Ampersand not allowed. The & operator is reserved for future use;”. If you remove the ampersand you get the error “The term ‘-Version’ is not recognized as the name of a cmdlet[… etc.]”

I hope this post saves people a bit of time.


Visual Studio has a whole load of variables (or ‘macros’ as they’re labelled in VS) for pre- and post-build commands. Variables like $(ProjectDir) are essential for providing a consistent build across different dev machines. With the VS2010 SharePoint integration have the ability to specify pre- and post-deployment commands, which can be very useful if you have very specific requirements for feature activation and deactivation. However, with different dev machines the deployment URL may differ. A quick look in the .csproj.user file points us in the right direction – the variable ‘SharePointSiteUrl’ can be used in your deployment commands as such:

$(ProjectDir)ActivateMyFeatures.bat $(SharePointSiteUrl)

Shove a batch file in the root of your project directory, read the command line args in it and you’re away!

Today I’ve had a bit of a wrestle with list item validation in SharePoint 2010. Consider the following scenario:

We have a list for expenses, which includes a few fields for the cost of the expense. We may have a field for the cost of the goods, a field for the rate of tax (VAT, GST, or whatever it’s called in your country) and a field for the total cost including the tax. Obviously we don’t want our users to have to fill in all of those fields, since given either a base cost or total cost we can calculate the other using the tax rate. As such we need to check that the user has not left both fields blank.

Do it through the UI

So we deploy the list, we open the list settings in the UI, we choose ‘Validation Settings’ and put this in the formula:

=IF([Cost ex tax]="",IF([Total cost]="","Error",""),"")=""

In simple terms, if the cost excluding tax is empty and the total cost is empty then return “Error”, otherwise return blank. The validation result is true if the result returned from the IF is “”. Click Save, try it out, and sure enough it works!

Put it in your schema.xml

HOWEVER, we want to deploy this expenses solution to a load of different sites, so we want to put the validation rules into our schema.xml for the list definition. This is where the fun starts – in our <MetaData> element under our <List> element we can add a <Validation> element. The MSFT documentation, rather predictably, doesn’t give any useful information about this validation element. Through a bit of trial and error this is how it needs to be done:

        <Validation Message="You must complete at least one of 'Cost ex tax' or 'Total cost'">
=IF(CostExTax=&quot;&quot;, IF(TotalCost=&quot;&quot;, &quot;Error&quot;, &quot;&quot;), &quot;&quot;)=&quot;&quot;

Note: I have added white space around the validation formula to make this example more readable.

Things to note about this markup

  • The validation element has a ‘Script’ attribute, that is used to specify javascript validation, NOT the validation formula
  • The validation formula itself must be put in between <Validation> and </Validation>
  • When you’re entering the formula through the UI you use the display name of the fields with brackets around it.
  • When you’re entering the formula in the CAML you use the internal name of the fields with no brackets.

Hopefully this will save someone a bit of trial and error!

Recycling application pools in IIS 7

In the olden days on IIS 6 recycling your application pool (rather than resetting IIS and recycling every app pool) was a case of doing this:

%windir%\system32\iisapp.vbs /a "MyAppPool" /r

Then Microsoft decided that appcmd should do it all for us in IIS 7. Firstly, this works for me:

appcmd recycle apppool "MyAppPool"

Do not take any notice of the Microsoft documentation which wrongly instructs you to put / before your application pool name.

Hope this helps someone else out there!