Export library/list menu Ready to test

28/01/2012 14:47

#Input parameters

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

$ExportFolder="C:\Export"

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

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



function ExportLibrary
{
    param(
          [string]$RootUrl,
          [string]$libName,
          [string]$ExportFolder
                        )
                     

#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 " $ExportFolder
    $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,
          [string]$ExportFolder
                        )
                      

#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 " $ExportFolder
    $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: " + $_)
    }
     
 
}


function ExportLibraryMenu
{
    param([string]$libName,
          [string]$RootUrl,
          [string]$ExportFolder)

#option to change current parameters, siteUrl and sourceDir

    write-host "Current settings of " $libName " export parameters"
    write-host "=============================================="
    write-host "Library url to export: " $RootUrl
    write-host "Export folder path: "$ExportFolder
    
    $input=read-host "Do you want to change library url or export folder path? Y/N"
    if($input -eq "y")
    {
        
            $siteUrl=read-host "Input library url (e. g. https://td_sp_ui/sites/test/CategoryCards/Pages):"
            #$errorActionPreference="Stop"
            while(($site=get-spsite $siteUrl) -eq $null)
            {
                
                $siteUrl=read-host "Library url doesn't exist. Check the address and try again: "
                
            }    
            $ExportFolder=read-host "Input export folder path (e. g. C:\Export): "      
    }
    ExportLibrary -RootUrl $RootUrl -libName $libName -ExportFolder $ExportFolder
}


    
function ExportListMenu
{
    param([string]$listName,
          [string]$RootUrl,
          [string]$ExportFolder)

#option to change current parameters, siteUrl and sourceDir

    write-host "Current settings of " $listName " export parameters"
    write-host "=============================================="
    write-host "List url to export: " $RootUrl
    write-host "Export folder path: "$ExportFolder
    
    $input=read-host "Do you want to change list url or export folder path? Y/N"
    if($input -eq "y")
    {
        
            $siteUrl=read-host "Input list url (e. g. https://td_sp_ui/sites/test/Helpdesk/Lists/Glossary-ListInstance1):"
            #$errorActionPreference="Stop"
            while(($site=get-spsite $siteUrl) -eq $null)
            {
                
                $siteUrl=read-host "List url doesn't exist. Check the address and try again: "
                
            }    
            $ExportFolder=read-host "Input export folder path (e. g. C:\Export): "      
    }
    ExportList -RootUrl $RootUrl -libName $listName -ExportFolder $ExportFolder
}         


#calling Export functions
ExportListMenu -RootUrl $RootListUrl -listName $listName -ExportFolder $ExportFolder
ExportLibraryMenu -RootUrl $RootLibraryUrl -listName $libName -ExportFolder $ExportFolder