I needed to test some user profile functionality in my lab farm which did not yet have the User Profile Service Application (UPA) configured. I had previously attached a content database from another farm to my web application as there was content in there that was relevant to my tests.
To configure the UPA you first need a My Site host site collection. I decided to create one using PowerShell. I ran PowerShell as the setup user account, and I also opened the PowerShell window as an administrator. The setup user account is a farm administrator and my prompt was elevated:
Windows PowerShell Copyright (C) 2009 Microsoft Corporation. All rights reserved. PS C:\Windows\system32> Add-PSSnapin Microsoft.SharePoint.PowerShell PS C:\Windows\system32> Get-SPWebApplication DisplayName Url ----------- --- Contoso Portal http://www.contoso.com/
Out of habit, I like using an explicit managed path to ensure my site collection goes exactly where I want it, so I created this using the New-SPManagedPath cmdlet:
PS C:\Windows\system32> New-SPManagedPath -RelativeUrl "my" -WebApplication http://www.contoso.com -Explicit Name Type ---- ---- my ExplicitInclusion
To create a My Site host, the site collection needs to use the SPSMSITEHOST#0 site template:
PS C:\Windows\system32> $my_template = Get-SPWebTemplate "SPSMSITEHOST#0" PS C:\Windows\system32> $my_template Name Title LocaleId Custom ---- ----- -------- ------ SPSMSITEHOST#0 My Site Host 1033 False
For simplicity's sake I specified my setup user account (CONTOSO\svc-sp-install) as the primary site collection administrator, but if you were doing this in a production environment you would likely want this to be a real user or a real administrative account. Now that I had the My Site Host template, a URL where it will live, and the primary administrator, I was all set to create the site collection:
PS C:\Windows\system32> New-SPSite -Url http://www.contoso.com/my -OwnerAlias contoso\svc-sp-install -Template $my_template New-SPSite : The URL '/my' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web. At line:1 char:11 + New-SPSite <<<< -Url http://www.contoso.com/my -OwnerAlias contoso\svc-sp-install -Template $my_template + CategoryInfo : InvalidData: (Microsoft.Share...SPCmdletNewSite:SPCmdletNewSite) [New-SPSite], SPException + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletNewSite
Odd. I dug around for a bit to make sure everything was right. I checked the managed paths and confirmed that /my was there. In Central Admin I double checked to make sure the /my managed path was available for a new site collection, which it was. I decided to check the content database to confirm that the path was there. In SQL Management Studio I opened the content database and realized that the content database name didn't match my usual naming conventions for my lab farm because it came from another farm... In other words, my farm didn't originally create the content database.
So then I checked to make sure the setup user account had the appropriate permissions. Sure enough, it didn't have a login to the content database. Normally when you create a content database, the account that creates it is added to the database's db_owner role, but because I had manually attached the content database (from a different farm with different users) this account wasn't in the membership for the role. So I added CONTOSO\svc-sp-install to the db_owner role, though note I could have instead added it to both the db_datawriter and db_datareader roles. Then I tried creating the site collection again:
PS C:\Windows\system32> New-SPSite -Url http://www.contoso.com/my -OwnerAlias contoso\svc-sp-farm -Template $my_template Url --- http://www.contoso.com/my
Right on. But what was with that exception? The path existed so why did it say otherwise?
Since the user didn't have access to the database, the cmdlet couldn't find the managed path. So technically the exception was correct! The path didn't "exist" because as far as New-SPSite was concerned it couldn't locate it. It makes you wonder. If SharePoint can't connect to the content database with the account then perhaps the exception should note this as well or instead of the doesn't exist exception. In any event, I was lucky I didn't burn a lot of time trying to figure it out and in the end it was my fault for not correctly setting the permissions on my database when I attached it; so let's call it even!
Issue: When creating a new site collection with New-SPSite, you receive an exception that the URL is invalid or doesn't exist for a URL that does in fact exist.
Exception Message: New-SPSite : The URL '%1' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web.
Cause: The user who is running the cmdlet does not have appropriate permissions to the content database.
Resolutions: Add permissions (db_owner or both db_datawriter and db_datareader) to the database for the current user or run New-SPSite as a different user who does have the necessary permissions.
- New-SPSite PowerShell cmdlet reference for creating a new site collection
- New-SPManagedPath PowerShell cmdlet reference for creating a new managed path
- Get-SPWebTemplate PowerShell cmdlet reference for getting a site collection template
- Database-Level Roles Description of the SQL Server roles available to a database
- Initial deployment administrative and service accounts (SharePoint Server 2010) A list of recommended service accounts for setting up SharePoint 2010
- Common site collection templates (SharePoint Server 2010) A list of out-of-the-box site collection templates