Export list / library function

24/01/2012 08:51

#Input parameters

$RootLibraryUrl="http://td_sp_ui/sites/test/CategoryCards/Pages"
$RootListUrl="http://td_sp_ui/sites/test/Helpdesk/Lists/Glossary-ListInstance1"


#libraries to export: Category Cards
$libName="Category Cards"

#lists to export: Glossary
$listName="Glossary"

 

 


function ExportLibrary
{
    param(
          [string]$RootUrl,
          [string]$libName
                        )
                     
#Destination folder for export file
$ExportFolder="C:\Export"

#check if export folder exists
if(!(Test-Path -path $ExportFolder))
{
     
        write-host "Folder " $ExportFolder " doesnt exist."
        write-host "Creating folder..."
        New-Item $ExportFolder -type directory
        write-host "Folder " $ExportFolder " was succesfully created."
      
}

   #take site url from library url (one level folder)
   $siteUrl=$RootUrl.substring(0, $RootUrl.LastIndexOf("/"));
  
  
   #take Library url (relative) from absolute library url
   $libUrl=$RootUrl.substring($RootUrl.LastIndexOf('/')+1, ($RootUrl.length - $RootUrl.LastIndexOf("/")-1))
 


    #Export file paths

    write-host "Exporting from: "$RootUrl

    $ExportPath=$ExportFolder + "\"
    $fileName=read-host "Insert export file name for " $libName " (e. g. CategoryCards.cmp). File location is C:\Export "
    $ExportPath+=$fileName

    if($fileName -eq "")
        {
              $ExportPath=$ExportFolder+"\CategoryCards.cmp"
         }

   

    # check to ensure Microsoft.SharePoint.PowerShell is loaded
    $snapin=Get-PSSnapin | Where-Object {
       $_.Name -eq 'Microsoft.SharePoint.Powershell'}
       if ($snapin -eq $null) {
       Write-Host "Loading SharePoint Powershell Snapin"
       Add-PSSnapin "Microsoft.SharePoint.Powershell"
       }
    
    try
    {
                      
        #check if site exists
        #if($siteUrl -eq "lukas")
        if((get-spweb $siteUrl -erroraction silentlycontinue) -eq $null)
            {
                write-host "Url " $siteUrl " doesnt exist. "
             }
        else
            {
                #export library
                write-host "Exporting " $libName " library..."
                try {
                     export-spweb -Identity $siteUrl -path $ExportPath -ItemUrl $libUrl -force
                     write-host "site url: "$siteUrl
                     write-host "path: " $ExportPath
                     write-host "Item Url: " $libUrl
                     write-host  $libName " library was succesfully exported."
                }
                catch {
              
                    write-error "Error: " $_
                    write-host $libName " export failed."
                }
             
                         
            }
 
  
    }
    catch [Exception]
    {
        write-error("Error: " + $_)
    }
    
 
}

 

 

function ExportList
{
    param(
          [string]$RootUrl,
          [string]$listName
                        )
                     
#Destination folder for export file
$ExportFolder="C:\Export"

#check if export folder exists
if(!(Test-Path -path $ExportFolder))
{
     
        write-host "Folder " $ExportFolder " doesnt exist."
        write-host "Creating folder..."
        New-Item $ExportFolder -type directory
        write-host "Folder " $ExportFolder " was succesfully created."
      
}

    #take site url from list url (one level folder)
   $siteUrl=$RootUrl.substring(0, $RootUrl.LastIndexOf("/"));
   $siteUrl=$siteUrl.substring(0, $siteUrl.LastIndexOf("/"));
 
 
  
   #take list url (relative) from absolute list url
   $listUrl=$RootUrl.substring($RootUrl.LastIndexOf('/')+1, ($RootUrl.length - $RootUrl.LastIndexOf("/")-1))
   $listUrl="Lists/" + $listUrl

 

    #Export file paths

    write-host "Exporting from: "$RootUrl

    $ExportPath=$ExportFolder + "\"
    $fileName=read-host "Insert export file name for " $listName " (e. g. fileName.cmp). File location is C:\Export"
    $ExportPath+=$fileName

    if($fileName -eq "")
        {
              $ExportPath=$ExportFolder+"\Glossary.cmp"
         }

   

    # check to ensure Microsoft.SharePoint.PowerShell is loaded
    $snapin=Get-PSSnapin | Where-Object {
       $_.Name -eq 'Microsoft.SharePoint.Powershell'}
       if ($snapin -eq $null) {
       Write-Host "Loading SharePoint Powershell Snapin"
       Add-PSSnapin "Microsoft.SharePoint.Powershell"
       }
    
    try
    {
                      
        #check if site exists
        #if($siteUrl -eq "lukas")
        if((get-spweb $siteUrl -erroraction silentlycontinue) -eq $null)
            {
                write-host "Url " $siteUrl " doesnt exist. "
             }
        else
            {
                #export list
                write-host "Exporting " $listName " list..."
                try {
                     export-spweb -Identity $siteUrl -path $ExportPath -ItemUrl $listUrl -force
                     write-host "site url: "$siteUrl
                     write-host "path: " $ExportPath
                     write-host "Item Url: " $listUrl
                     write-host  $listName " list was succesfully exported."
                }
                catch {
              
                    write-error "Error: " $_
                    write-host $listName " export failed."
                }
             
                         
            }
 
  
    }
    catch [Exception]
    {
        write-error("Error: " + $_)
    }
    
 
}


#Export, calling function
ExportLibrary -RootUrl $RootLibraryUrl -libName $libName
ExportList -RootUrl $RootListUrl -listName $listName