Archive

Archive for April, 2012

Importing SharePoint 2010 Lists/Document Libraries from backup directory using PowerShell.

April 30, 2012 5 comments

Previously I wrote a blog about how to Export all the lists and document libraries from a specific site collection in SharePoint 2010 –> http://jshidell.com/2012/04/26/export-all-listdocument-libraries-of-a-specific-site-collection-in-sharepoint-2010-using-powershell/

I’m now going to blog about how to Import these lists and document libraries back into SharePoint.  Now In most cases you would just import these lists and document libraries into a new Site Collection on the same farm, but for whatever reason such as mine I had to import these lists and document libraries into a different site collection on a different farm (Staging farm).

For me to do this I had to move over all my “.cmp” files into a directory on my staging farm server so it would be accessible for the import.  Granted you could possibly set up a share between both farms servers and just map to the location to pull the files.  Regardless the script below should work either way.

——————————————————————————————————————————————————

#Location of files to Import
$path = Read-Host “Location of Import Files”

#This is the Destination Site that will be importing the exported sites and libraries
$destinationWebURL = Read-Host “Enter in the destination site to import lists”

#This will iterate through the Import directory location, select all files with the extension of .cmp and store it in the $List variable
$Dir = get-childitem $path -recurse
$Lists = $Dir | Where-Object {$_.extension -eq “.cmp”} | Select-Object Name

Write-Host “Importing Lists & Document Libraries into  ” $destinationWebURL -ForegroundColor Cyan

#a foreach loop to loop through the Lists, check for files that may contain multiple split .cmp files (i.e documents1.cmp, documents2.cmp) and only selecting the first instance (i.e. documents.cmp), and then importing the list.

foreach($list in $Lists)
{
 if ($list.Name -notlike ‘*[a-z][1-9]*’)
{
 write-host “Importing ” $list.Name
 Import-SPWeb -Identity $destinationWebURL -path ($path + “\” + $list.Name) -UpdateVersions Overwrite-IncludeUserSecurty
}
}

write-host “Import Complete” -ForgroundColor Cyan

—————————————————————————————————————————————————–

Export All List/Document Libraries of a Specific Site Collection in SharePoint 2010 Using PowerShell

April 26, 2012 18 comments

I recently had to do an export of all Lists and Document libraries of  a specific Site Collection in my SharePoint 2010 Farm due to a corrupted Site Collection.  I needed to move all these list and documents into a new Clean Site Collection.  Since the Site Collection had quite a few Lists and Document Libraries I did not want to use the Export functionality within Central Administrations.  That would have taken forever.  So I turned to my beloved PowerShell  to help out.  I had to iterate through the entire lists and document libraries of the Site Collection, grab the relative URL and then export.  Below is the script I wrote to accomplish this.

—————————————————————————————————————————————————-

#This is the source web that is hosting the lists to export
$sourceURL = Read-Host “Enter in the Source Site that is hosting the lists and document libraries to export”

#Location to store the export file
$path = Read-Host “Location to store exported list”

#Select the Title and itemURL from Lists and Document Libraries and store in $list variable
$lists = @($(Get-SPWeb -Identity $sourceURL).lists | Select-Object Title, @{Name=”itemURL”;Expression = {$_.RootFolder}})

Write-Host “Exporting Lists & Document Libraries from ” $sourceURL -ForegroundColor Cyan

#Loop through each list and document library and Export
foreach($list in $lists)

{

Write-Host “Exporting ” $list.Title

Export-SPWeb -Identity $sourceURL -ItemURL $list.itemURL -path ($path + “\” + $list.Title + “.cmp”) -IncludeVersions All -IncludeUserSecurity

}

Write-Host ‘Export Complete” -ForegroundColor Cyan

$sourceWebURL.Dispose()


***Note***

For me I had to do some extra plumbing to some list and document library Titles before export.  Since the export was using the exact same Title as what the list and document library’s were named upon creation in SharePoint, some of my lists and document libraries had invalid characters which prevented me from exporting.

Since I knew what the invalid character was I just simply did a .Replace to replace the invalid character with a space, such as:

#Initialize new variable to hold the $list.Title
$TitleRename = $list.Title
#Perform a Replace to search for “:” in the string and replace with a space “”.
$TitleRename = $TitleRename.Replace(“:”,””)

So instead of using the Export command I used above I used:

Export-SPWeb -Identity $sourceURL -ItemURL $list.itemURL -path ($path + “\” + $TitleRename + “.cmp”) -IncludeVersions All -IncludeUserSecurity

Fixing Error: The Site Is Not Valid The ‘Pages’ Document Library is Missing in SharePoint 2010.

April 16, 2012 Leave a comment

Oh the Joy of SharePoint 2010.  You learn something new everyday.  But hey it’s good  job security right?

Anyways I recently exported a sub-site in SharePoint 2010 and re-imported this Site and converted it into a Site Collection, into its own managed path and into it’s own content database.

When you do this, there is some clean up that needs to be done.  This site needed to have the publishing features enabled, but it’s not a ‘true’ publishing site.  So I needed to enabled the Site Collection (SharePoint Server Publishing Infrastructure) feature, and also the Site (SharePoint Server Publishing) feature.

I’m not going to get into detail on the other steps needed to successfully convert a Site to a Site Collection in this blog  but I will blog about those procedure later.

So to make a long story short I was attempting to create a new Publishing Page but was I was receiving CorrelationID errors that prevented me from successfully creating the page.

I took the CorrelationID and searched my SharePoint ULS logs.  Based off this CorrelationID I was given this error:

The site is not valid. The ‘Pages’ document library is missing.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Microsoft.SharePoint.Publishing.InvalidPublishingWebException: The site is not valid. The ‘Pages’ document library is missing.

Based off this error I went to check to make sure the ‘Pages’ document library does indeed exists and is not missing.  To my surprise the ‘Pages’ document library was there.  So why am I getting this error?  I was a little confused.

This error is due to the Publishing feature storing a unique ID of the Pages library in the __PagesListId property which resides in the property bag of the publishing site.

The above listed error will occur if the value stored in the __PagesListId does not match the ID of the actual Pages library.

So somewhere between the Export/Import of the Site it lost its unique ID.

To fix this error you can run a PowerShell script to reassign/update the correct unique ID.

Below is the PowerShell script that will fix this problem.

_____________________________________________________________________________________________

$web = get-spweb http://portal/path-to-affected-site
$correctId = $web.Lists[“Pages”].ID
$web.AllProperties[“__PagesListId”] = $correctId.ToString()
$web.Update()

______________________________________________________________________________________________

After running the script I attempted to create the Publishing Page again and it was created successfully with no error.

I’ve only seen this error happen with an Export/Import of Sites to Site Collections in SharePoint 2010.

PowerShell Script: (Creating a Site Collection in a Managed Path In It’s Own Content Database)

April 10, 2012 Leave a comment

Did I mention I love PowerShell.  Well I’ll mention it again.  I love PowerShell.  It makes for performing things within SharePoint 2010 so much easier through automation.

Below is a basic script that will Create a New Site Collection in a Managed Path and in It’s own Content Database.  Just copy the script below into Notepad and save as .ps1.

The Script checks to see if a Managed Path already exists in the Web Application.  If not it creates it.  Creates the new Content Database, Creates the new Site Collection in the new Managed Path, Assigns Primary/Secondary Site Owners, Sets the Site Collection to Anonymous Access.

—————————————————————————————————————————————————

Write-Host “********************************************************************************************************”
Write-Host “*     Checking to see if Managed Path already Exists                                                                 *”
Write-Host “********************************************************************************************************”

$WebApplicationURL = Read-Host “Enter in Web Application URL”
$ManagedPathName = Read-Host “Enter in Managed Path”

$ManagedPath = Get-SpManagedPath -WebApplication $WebApplicationURL -Identity $ManagedPathName -ErrorAction SilentlyContinue
if ($ManagedPath -ne $null)
{
Write-Host “Managed Path $ManagedPathName already Exists”.
}
else
{
Write-Host “Managed Path Does Not Exist, Creating Managed Path $ManagedPathName ….”
New-SPManagedPath -RelativeURL $ManagedPathName -WebApplication $WebApplicationURL -Explicit
}

Write-Host “*********************************************************************************************************”
Write-Host “*     Creating New Content Database                                                                                             *”
Write-Host “*********************************************************************************************************”

$NewContentDB = Read-Host “Enter In Name of New Content Database”
New-SPContentDatabase -name $NewContentDB -WebApplication $WebApplicationURL
Write-Host “Content Database Created”

Write-Host “**************************************************************************************”
Write-Host “*      Creating New Site Collection                                                  *”
Write-Host “**************************************************************************************”

$Template = “STS#0”
$Domain = Read-Host “Enter In Domain”
$NewSiteCollectionURL = $WebApplicationURL + “/” + $ManagedPathName
$PrimaryOwnerAlias = Read-Host “Enter In Primary Site Collection Owner”
$SecondaryOwnerAlias = Read-Host “Enter In Secondary Site Collection Owner”
$OwnerAlias = $Domain + “\” + $PrimaryOwnerAlias
$SecondaryAlias = $Domain + “\” $SecondaryOwnerAlias

New-SPSite -Url $NewSiteCollectionURL -ContentDatabase $NewContentDb -OwnerAlias $OwnerAlias -SecondaryOwnerAlias $SecondaryAlias -Template $Template

$Web = Get-SPWeb $NewSiteCollectionURL
$Web.AnonymousState = 2;

Write-Host “Site Collection Created Succesfully…”
Write-Host “Site Collection Owners Added”
Write-Host “Anonymous Access Enabled”

—————————————————————————————————————————————————-

That’s it, pretty simple.  I will be modifying this script a little to display all the different SharePoint Templates you can choose from when creating your Site Collection.  Currently this script is using the Team Site template.