Tuesday, March 5, 2013

PowerShell : Uploading sharepoint list data during deployment

While working on a project, needed to upload some records soon after the list is created from an xml file. A few things to keep in mind

  • To access the file on the folder (xml in this case) ExecutionPolicy needs to be set. Otherwise, 'Access Denied' error will be raised.
  • Even though script and xml files are in the same folder, we need to specify the folder location for xml explicitly. This ensures xml is not searched in script location. Even if the script location is changed, this ensures the access to xml
  • After fetching the document, file contents cannot be displayed using $variable ($XmlFile in this case). XML contents cannot be displayed as data is loaded in XmlDocument object
  • Another important thing to note is that make sure the content we put in xml document are proper as per xml rules. For example if there is a break point required we cannot use html tags directly.


  • Here is the sample xml
    
    
      
        
        
        
      
     
    

    Here is the code
    Write-Host ""
    Write-Host ""
    Write-Progress -Activity "Setting up environment" -Status "Loading Modules"
    Write-Host "Setting up environment....." -foregroundcolor "Cyan"
    if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)) 
    { 
        Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell" 
        Write-Host -NoNewline "Loading Modules...." -foregroundcolor "Cyan"
        Add-PsSnapin Microsoft.SharePoint.PowerShell 
        Write-Host "Done." -ForegroundColor Green
    } 
    
    Set-ExecutionPolicy -ExecutionPolicy Unrestricted 
    
    $Invocation = (Get-Variable MyInvocation -Scope 0).Value
    $FolderName =  Split-Path $Invocation.MyCommand.Path 
     
    try
    {
        Write-Host "Fetching the file "$ContextHelpFileName "from" $FolderName "..." -NoNewline -ForegroundColor Cyan
        [xml]$global:XmlFile = Get-Content $FolderName$ContextHelpFileName
        Write-Host "Done." -ForegroundColor Green
    }
    catch
    {
        Write-Host "XML file could not be loaded."
        Write-Host $Error
    }
    
    Write-Host "Preparing to upload data to "$ContextHelpListTitle "list...." -ForegroundColor Cyan
    
    #Get access to web and the list in question
    $web = Get-SPWeb $SiteCollectionURL
    $contextHelpList = $web.Lists[$ContextHelpListTitle]
    
    #Clear the list items
    $contextHelpList.Items.d
    
    foreach ($Row in $XmlFile.ContextHelp.Rows.Row){
        
        Write-host "Adding item"$Row.Title"...." -NoNewline -ForegroundColor Cyan
        
        $addedRow = $contextHelpList.Items.Add()
        $addedRow["Title"] = $Row.Title 
        $addedRow["TitleText"] = $Row.Title 
        $addedRow["TitleBody"] = $Row.Body 
        $addedRow.Update();
    
        #Add each item to the list
        Write-Host "Done." -ForegroundColor Green
    }
    
    #Uploading list data complete successfully
    Write-Host "List data uploaded successfully." -ForegroundColor Green
    

    No comments:

    Post a Comment