Import list / library function

16/01/2012 16:51


#Input PARAMETERS - calling function is on the bottom
$RootUrl="http://td_sp_ui/sites/test"

 

 


function ImportLibrary
{
    param(
          [string]$RootUrl,
          [string]$libraryUrl,
          [string]$libName
                        )
                      
#source folder for import file
$ExportFolder="C:\Export"

#check if export folder exists
if(!(Test-Path -path $ExportFolder))
{
        write-host "Folder " $ExportFolder " doesnt exist, check the name of import folder."
}
 else
 {
    #take site url from library url (one level folder)
   $siteUrl=$libraryUrl.substring(0, $libraryUrl.LastIndexOf("/"));


    #Imported file paths

    write-host "Importing to site: "$RootUrl " from "$ExportFolder

    $ImportPath=$ExportFolder + "\"
    $fileName=read-host "Insert import file name for " $libName " (e. g. fileName.cmp)"
    $ImportPath+=$fileName

    if(!(Test-Path $ImportPath))
        {
              $ImportPath=$ExportFolder+"\CategoryCards.cmp"
       write-host "File with "$fileName " doesn't exist." 
  exit
         }

    $ErrorActionPreference = "Stop"; #Make all errors terminating


    # 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((get-spweb $siteUrl -erroraction silentlycontinue) -eq $null)
            {
                write-host "Url " $siteUrl " doesnt exist. "
             }
        else
            {
                #import library
                write-host "Importing " $libName " library..."
                try {
                    import-spweb -Identity $siteUrl -path $ImportPath  -force
                     write-host  $libName " library was succesfully imported."
                }
                catch {
               
                    write-error "Error: " $_
                    write-host $libName " import failed."
                }
              
                          
            }
  
   
    }
    catch [Exception]
    {
        write-error("Error: " + $_)
    }
     
  }#else if source folder didnt exisst

}

 

function ImportList
{
    param(
          [string]$RootUrl,
          [string]$listUrl,
          [string]$listName
                        )
                      
#source folder for import file
$ExportFolder="C:\Export"

#check if export folder exists
if(!(Test-Path -path $ExportFolder))
{
        write-host "Folder " $ExportFolder " doesnt exist, check the name of import folder."
}
 else
 {
    #take site url from library url (one level folder)

    $ListsUrl=$listUrl.substring(0, $listUrl.LastIndexOf("/"));  
    $siteUrl=$ListsUrl.substring(0, $ListsUrl.LastIndexOf("/"));
   

    #Imported file paths

    write-host "Importing to site: "$RootUrl "  from "$ExportFolder

    $ImportPath=$ExportFolder + "\"
    $fileName=read-host "Insert import file name for " $listName " (e. g. fileName.cmp)"
    $ImportPath+=$fileName

    if(!(Test-Path $ImportPath))
        {
              $ImportPath=$ExportFolder+"\Glossary.cmp"
  write-host "File with "$fileName " doesn't exist." 
  exit
         }

    $ErrorActionPreference = "Stop"; #Make all errors terminating


    # 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((get-spweb $siteUrl -erroraction silentlycontinue) -eq $null)
            {
                write-host "Url " $siteUrl " doesnt exist. "
             }
        else
            {
                #import library
                write-host "Importing " $listName " list..."
                try {
                    import-spweb -Identity $siteUrl -path $ImportPath  -force
                     write-host  $listName " list was succesfully imported."
                }
                catch {
               
                    write-error "Error: " $_
                    write-host $listName " import failed."
                }
              
                          
            }
  
   
    }
    catch [Exception]
    {
        write-error("Error: " + $_)
    }
     
  }#else if source folder didnt exisst

}


#libraries to import: Category Cards
$libraryUrl=$rootUrl + "/CategoryCards/Pages"
$libName="Category Cards"

#Import, calling function
ImportLibrary -RootUrl $RootUrl -libraryUrl $libraryUrl -libName $libName


#libraries to import: Faq
$libraryUrl=$rootUrl + "/Helpdesk/Faq/Pages"
$libName="Faq"


#Import, calling function
#ImportLibrary -RootUrl $RootUrl -libraryUrl $libraryUrl -libName $libName


#lists to import: Glossary
$listUrl=$rootUrl + "/Helpdesk/Lists/Glossary-ListInstance1"
$listName="Glossary"


#Import, calling function
ImportList -RootUrl $RootUrl -listUrl $listUrl -listName $listName