Blog Archives

Migrating Master Pages from MOSS 2007 to SharePoint 2010

I’ve mentioned before that I am doing a complex migration from MOSS 2007 to SharePoint 2010 and I came across an absolute doozy of an issue over the last couple of days. I doubt anyone else will come across this, but it worth noting, if not just for my sanity, but also in case it crops up somewhere else.

The source 2007 site was a publishing site with a heavily customised Master Page. The Master Page containing some controls in it for navigation and so on – unfortunately the source code for these controls were written in Visual Studio 2005 and deployed, well, somehow. I rebuilt the controls in VS2010 and a nice WSP for deployment.

There remains the problem of getting it to work in the new environment. The old control would not work, giving the SharePoint Screen of Death. No problem, I don’t need it anyway. I copied the old 2007 Master Page, uploaded it into 2010 and worked on it with SharePoint Designer to run the new control. All is hunky dory.

Until we come to roll out. Plan was to do a migration via Attach Database, then put my new Master Page into the system, flip over to it and all is well.

The migration goes OK, the site pops up with the expected Master Page error, I go into SPD to upload the new custom MP, check it in, requires Approval so open the page in the browser and…

Error. The Approval page still uses the old, failing, MP.

I can change the MP using the GUI but I can’t flip it to the new Master page because it hasn’t been Approved and I can’t Approve it because that relies on a Master Page that fails.

OK, so off to Powershell I go and Approve the page in the back end. Back to the Change Master Page page and my custom MP either does not appear in the dropdown or gives me a warning that it is set for SharePoint v4 (2010) and not the v3 (2007) UI that we are keeping.

The problem is this. If you upload a Master Page to the Master Page Gallery, the UI Version is automatically set to v4 – SharePoint 2010. This is a problem if you are using the v3 GUI – either the new MP will not appear for selection and if it does, it is set to v4. You can’t change this because your Master Page is faulty and you can’t get to the List through the browser. The value is not available in SPD to change.

After a lot of faffing, the solution I found is this.

1. Using Powershell, change the MasterPage Url to default.master (or something other than your current MP, but still v3).
2. Go into the Master Page Gallery and edit the new custom Master Page item you added and set the UI Version to 3 (not both 3 and 4, but 3).
3. Back into Powershell and change the MasterPage Url to your new custom Master Page.

It will all work now.

The key fact to take away is that by default in SharePoint 2010, any Master Page uploaded to the Gallery is set to v4 even if your site is running as v3.

Advertisements

Quota Templates in Powershell

I’m currently working on a few migration type things and need to set up some quota templates.  The actual sizes of those templates is TBA, so I figure a set of quick Powershell functions will help me set them up as an when I need them.

The majority of the code is from Ryan Dennis at SharePointRyan.com, though I believe he built on work from Gary LaPointe.  But then, doesn’t everyone?

function New-SPQuotaTemplate {
<#
.Synopsis
    This advanced function creates a new Site Quota Template.
.Example
    C:\PS>New-SPQuotaTemplate -Name "Custom" -StorageMaximumLevel 2GB -StorageWarningLevel 1GB -UserCodeMaximiumLevel 100 -UserCodeWarningLevel 75
     
    This example creates an SP Quota Template called Custom with a maximum size of 2GB and a warning size of 1GB. Sandboxed solutions are limited to 100, with a warning level of 75.
.Example
    C:\PS>New-SPQuotaTemplate -Name "Custom" -StorageMaximumLevel 4GB -StorageWarningLevel 3GB
     
.Notes
    Name: New-SPQuotaTemplate
    Author: Ryan Dennis
    Last Edit: 4/27/2012
    Keywords: Quota Template, Quotas and Locks
	Modified By: Mike Landers, 02/04/2013
.Link
http://www.sharepointryan.com
http://twitter.com/SharePointRyan
#Requires -Version 2.0
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][String]$Name,
[Parameter(Mandatory=$true)][Int64]$StorageMaximumLevel,
[Parameter(Mandatory=$true)][Int64]$StorageWarningLevel,
[Parameter(Mandatory=$false)][System.Double]$UserCodeMaximumLevel,
[Parameter(Mandatory=$false)][System.Double]$UserCodeWarningLevel
)
# Instantiate an instance of an SPQuotaTemplate class #
Write-Verbose "Instantiating an instance of an SPQuotaTemplate class"
$Quota = New-Object Microsoft.SharePoint.Administration.SPQuotaTemplate
# Set the Properties #
Write-Verbose "Setting properties on the Quota object"
$Quota.Name = $Name
$Quota.StorageMaximumLevel = $StorageMaximumLevel
$Quota.StorageWarningLevel = $StorageWarningLevel
$Quota.UserCodeMaximumLevel = $UserCodeMaximumLevel
$Quota.UserCodeWarningLevel = $UserCodeWarningLevel
# Get an Instance of the SPWebService Class #
Write-Verbose "Getting an instance of an SPWebService class"
$Service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
# Use the Add() method to add the quota template to the collection #
Write-Host "Adding the $($Name) Quota Template to the Quota Templates Collection" -foreground Green
$Service.QuotaTemplates.Add($Quota)
# Call the Update() method to commit the changes #
$Service.Update()
Write-Host "Quota Template $Name added successfully" -foreground Green
}

function Get-SPQuotaTemplate {
<#
.Synopsis
 This advanced function retrieves a SharePoint Site Quota Template.
.Example
 C:\PS>Get-SPQuotaTemplate -Name "Custom"
 
 This example retrieves a SharePoint Quota Template called Custom in the current farm.
.Example
 C:\PS>Get-SPQuotaTemplate
 
 This example retrieves all SharePoint Quota Templates in the current farm.
.Notes
 Name: Get-SPQuotaTemplate
 Author: Ryan Dennis
 Last Edit: 5/10/2012
 Keywords: Quota Template, Quotas and Locks
.Link
http://www.sharepointryan.com
http://twitter.com/SharePointRyan
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)][String]$Name
)
$Templates = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.QuotaTemplates
if ($Name)
 {
 $Templates | Where-Object {$_.Name -eq $Name}
}
else
{
 $Templates
}
}

function Remove-SPQuotaTemplate {
<#
.Example
 C:\PS>Remove-SPQuotaTemplate -Name "Custom"
 
 This example removes a SharePoint Quota Template called Custom in the current farm.
 .Link
http://stuffaboutsharepoint.com
http://twitter.com/micaituk

#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)][String]$Name
)
$Templates = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.QuotaTemplates
$TemplateCheck = $Templates | Where-Object {$_.Name -eq $Name}
if ($TemplateCheck -ne $null)
   { 
   	Write-Host "`nFound Template $Name" -foreground Green
	$Service = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
	$Template = $Service.QuotaTemplates[$Name]
	Write-Host "Deleting Template $Name" -foreground Green
	$Service.QuotaTemplates.Delete($Name)
	$Service.Update()
	Write-Host "Template $Name deleted successfully`n" -foreground Green
	Write-Host
   }
else
   {
    Write-Host "`nTemplate $Name does not exist, no changes made`n" -foreground Red
   }
}

Querying Large Lists using CAML

Struggling with this one for a large point of the day, before I realised what was happening.  I was running a query using SPQuery and CAML against a list that needed to be restricted for a subset of users.  I was happily running it as me, but my test (restricted) user was getting Null reference errors.

I quickly realised that my list had over 36,000 items in it (split into folders of < 2000 items) so I had to come up with a way of running the same CAML query over the list independent of user.

There are number of options (well explained by <this excellent blog entry by Steve Peschka to deal with list thresholds and programmatic overrides.

SPList has an EnableThrottling property, which is set to True by default.  You can use the Object Model to set it to False temporarily.  The problem is that the permissions to do so cannot be done by anyone who isn’t a Farm Administrator.  SPSecurity.RunWithExecutivePrivileges run as the Application Pool account which is, unless you’ve configured something wrongly should not be a Farm Admin level account.

To cut a long story short, you need to either do this in code or Powershell when the list is created.  Alternatively, if your list has popped over the item count threshold, you can do it at any time.  The Powershell script is simple.

$url="<%YOUR URL HERE%>"
$listname="<%YOUR LIST NAME HERE%>"
$web = Get-SPWeb $url
$list = $web.Lists[$listname]
$list.EnableThrottling = $false
$list.Update()

To switch throttling back on, simply re-run the script, changing the $false to $true.

The beauty of this is that it applies on a list by list basis, so you don’t have to go into Central Administration and make Web Application wide changes to get a single large list to query.  The script is modified from one published by Dina Ayoub.

Disallowed Updates on creating a Web Application

Hit an unusual one when creating Web Applications on a Farm.  Despite having gone through the process without any problems on two other farms, one particular (existing) setup fell over on every single Web Application I tried to create.  It manifested itself in an IE unable to connect to server error, but when you go into General Settings the following error is shown.

unsafeupdates

Not entirely sure what the cause is, but the fix is quite simple.

Log onto the Central Admin machine using Remote Desktop Connection, open a SharePoint 2010 Management Shell and run the following script:

$w=GetSPWebApplication <%WEBAPPURL%>
$w.HttpThrottleSettings

$w.Update()

You can now go into General Settings and carry on as normal.

Programmatically getting the Site Collection Name

Just was sorting out a quick console app that audited all the details of a MOSS 2007 farm, all Web Applications, down through Site Collections, Sites, Webs and down to list level.  When presenting this to the end user, I wanted to use a cascading drop down, where the user selected the Web Application and then had a nice list of Site Collections to choose from.

The problem I hit was how on earth to get the Site Collection Title and Description.  These are displayed in Central Administration | Application Management | Site Collection List but are not included in the SPSiteCollection object, nor the WebApplication object (which holds stuff like the Content Database which is included on that page).

Answer: the Title and Description that appears on the Site Collection List page is the Title and Description of the root Site in the Site Collection.

Try it!  Check the Title and Description in Central Admin, then change them on the root Site using Site Settings | Title, Description and Icon.

Took me a while to find that one…

Individual MySite Quotas

Had a request from a user who had run out of space for storing documents in their MySite.  One of the fun side effects of this in MOSS 2007 is that you can’t use the GUI to change the quota allocated – it throws the standard SharePoint error.

Because the MySite is a Site Collection, you can change the quota on an individual basis.  I knocked together a short console app in Visual Studio 2008.

Build and run it from the Command Line using the syntax:

SiteQuotaChange.exe -url %URL% -quotaname %QUOTANAME%

Notes:
1. Because %QUOTANAME may be something like “Personal Site”, you need to put the quota name in ” marks if it contains a space.
2. If you get %QUOTANAME% wrong, MOSS 2007 will default back to “Individual Quota” with no space allocated.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace SiteQuotaChange
{
class Program
{
private static string url = String.Empty;
private static string quotaName = String.Empty;

static void Main(string[] args)
{
try {
GetArgs(args);
if (url == String.Empty || quotaName == String.Empty || args.Length > 4)
{
Console.WriteLine("Usage: sitequotachange -url %URL% -quotaname %QUOTANAME%");
}
else
{
using (SPSite site = new SPSite(url))
{
SPWeb web = site.OpenWeb();
Console.WriteLine("Before: " + site.Quota.StorageMaximumLevel);
web.AllowUnsafeUpdates = true;
web.Update();
site.Quota = SPWebService.ContentService.QuotaTemplates[quotaName];
web.Update();
Console.WriteLine("After: " + site.Quota.StorageMaximumLevel);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception thrown: {0}, {1}", ex.GetType(), ex.Message);
}
Console.WriteLine();
Console.WriteLine("Finished, press any key");
Console.ReadKey();
}

private static void GetArgs(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "-url":
url = args[i+1];
break;
case "-quotaname":
quotaName = args[i + 1].Replace("\"", "");
break;
}
}
}
}
}

Programmatically determining whether a List Item is a Record

Was trying to find this one, and the path / references aren’t all that clear.  So…

  1. Add a reference to Microsoft.Office.Policy
  2. In the Usings add using Microsoft.Office.RecordsManagement.RecordsRepository
  3. Get the ListItem or File
  4. Records.IsRecord(item) returns true or false if it is a record

The key bit is figuring out RecordsManagement.RecordsRepository is part of Microsoft.Office.Policy.

MSDN

Using [Me] in CAML Queries

Sometimes you need to put a custom View into a List Definition.  That View could be based on the current user (for example “Show all List Items assigned to me”).  The problem is that the usual GUI method uses [Me] to filter the items appropriately, which breaks CAML.

The trick is twofold.  First, the Value Type is “User” and second, the magic value is <UserID />.  Therefore your code is.

<Query>
  <Where>
    <FieldRef="AssignedTo" />
      <Value Type="User"><UserID /></Value>
  </Where>
</Query>

Hat Tip: Macedonian SharePoint Group (which incidentally also demonstrates how to use [Today] in CAML.  Also MSDN.

Hello world!

This is a blog about SharePoint.  It basically serves as a repository for bits of code that I use every so often keep forgetting whenever I move to another contract.  I don’t claim to be the author of all of the code here, but where I can I’ve credited people appropriately.

The idea is that I make a quick note if I’ve used a handy technique or site and the search engines pick up some of these things and help spread the knowledge around the general SharePoint community.