Friday, 14 February 2014

Upload Master Page in SharePoint Site using PowerShell

At first you create one folder like “Master Page Unload File” under the folder creates one more folder like “Doc” with two other files UploadMasterPages.bat and UploadMasterPages.ps1.
Master Page Unload File.
-  Doc
                - MasterPage.aspx (Store your master page under Doc folder)
-  UploadMasterPages.bat
-  UploadMasterPages.ps1

Please see the below code part:-   For UploadMasterPages.ps1


param($webAppUrl)
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint.Administration")
 
$checkInComment="Check In"
$publishComment="published"
$approveComment="Approved"
$logfile = "UploadMasterPage_$(get-date -f yyyyMMdd_hhmmss).log"
$spsite = new-object Microsoft.Sharepoint.SPSite($webAppUrl);
$web = $spsite.RootWeb;
 
    $masterPageList = ($web).GetFolder("Master Page Gallery")
    # Get file system path
$filesfolde = Split-Path $script:MyInvocation.MyCommand.Path
 
$masterPageLocalDir = $filesfolde + "\Doc"
    #For upload all files in document library from file system
     
foreach ($file in Get-ChildItem $masterPageLocalDir)
      {
      $web.AllowUnsafeUpdates=$true;
     
try
{
      if ([Microsoft.SharePoint.Publishing.PublishingSite]::IsPublishingSite($spsite))
      {
     
      $stream = [IO.File]::OpenRead($file.fullname)
          $destUrl = $web.Url + "/_catalogs/masterpage/" + $file.Name;
            $masterPageFile=$web.GetFile($destUrl)
      write-host($masterPageFile)
      write-host($destUrl)
      write-host($stream)
           if($masterPageFile.CheckOutStatus -ne "None")
            {
                       
                        $web.AllowUnsafeUpdates  = $true;
                        $masterPageList.files.Add($destUrl,$stream,$true)
                       
                        $stream.close()                                
                        $masterPageFile.CheckIn($checkInComment);                              
                        $masterPageFile.Publish($publishComment);                       
                        $masterPageFile.Approve($approveComment);
                        $masterPageFile.Update();        
                         $web.Update();
                          $web.AllowUnsafeUpdates  = $false;
                          $outputText = $file.Name+ " Master Page uploaded on $web site"
                                write-output $outputText
                                write-output $outputText |  out-File $logfile -Append
            }
          else
            {
                        $masterPageFile.CheckOut();
                        try{
                        $masterPageList.Files.Add($destUrl,$stream,$true)
                        }
                        catch
                        {
                        write-Output $_
                        }
                        $stream.close()                                       
                         $masterPageFile.CheckIn($checkInComment);                                     
                         $masterPageFile.Publish($publishComment);                                     
                         $masterPageFile.Approve($approveComment);
                        $masterPageFile.Update();        
                         $web.Update();
                          $web.AllowUnsafeUpdates  = $false;
                          $outputText = $file.Name +  " Master Page uploaded on $web site"
                                write-output $outputText
                                write-output $outputText |  out-File $logfile -Append
            }
      }
     
      else
      {
     
            $stream = [IO.File]::OpenRead($file.fullname)
            $destUrl = $web.Url + "/_catalogs/masterpage/" +$file.Name
            $masterPageFile=$web.GetFile($destUrl)
             if($masterPageFile.CheckOutStatus -ne "None")
              {
                        $masterPageList.Files.Add($destUrl,$stream,$true)
                        $stream.close()                                
                        $masterPageFile.CheckIn($checkInComment);                              
                        $masterPageFile.Publish($publishComment);                       
                        $masterPageFile.Approve($approveComment);
                        $masterPageFile.Update();        
                         $web.Update();
                          $web.AllowUnsafeUpdates  = $false;
                          $outputText = $file.Name +  "Master Page uploaded on $web site"
                                write-output $outputText
                                write-output $outputText |  out-File $logfile -Append
               }
             else
               {
                        $masterPageFile.CheckOut();
                        $masterPageList.Files.Add($destUrl,$stream,$true)
                        $stream.close()                                       
                         $masterPageFile.CheckIn($checkInComment);                                     
                         $masterPageFile.Publish($publishComment);                                     
                         $masterPageFile.Approve($approveComment);
                        $masterPageFile.Update();        
                         $web.Update();
                          $web.AllowUnsafeUpdates  = $false;
                          $outputText = $file.Name+ "Master Page uploaded on $web site"
                                write-output $outputText
                                write-output $outputText |  out-File $logfile -Append
              }
      }
     
     
}
catch
{
try
         {
     
           
            $stream = [IO.File]::OpenRead($file.fullname)
            $destUrl = $web.Url + "/_catalogs/masterpage/" + $file.Name;
            $masterPageFile=$web.GetFile($destUrl)
             if($masterPageFile.CheckOutStatus -ne "None")
               {
                        $masterPageList.Files.Add($destUrl,$stream,$true)
                        $stream.close()                                
                        $masterPageFile.CheckIn($checkInComment);
                       $masterPageFile.Update();        
                         $web.Update();
                          $web.AllowUnsafeUpdates  = $false;
                          $outputText = $file.Name+ " Master Page uploaded on $web site"
                                write-output $outputText
                                write-output $outputText |  out-File $logfile -Append
               }
              else
                {
 
                        $masterPageFile.CheckOut();
                        $masterPageList.Files.Add($destUrl,$stream,$true)
                        $stream.close()                                
                        $masterPageFile.CheckIn($checkInComment);
                       $masterPageFile.Update();        
                         $web.Update();
                          $web.AllowUnsafeUpdates  = $false;
                          $outputText = $file.Name +" Master Page uploaded on $web site"
                                write-output $outputText
                                write-output $outputText |  out-File $logfile -Append
               }
         }
      catch
         {
            write-Output $_ | out-File $logfile -Append
         } 
}
}
$web.dispose();
$spsite.dispose();

----------------------------------------------------------------------------------------------------
 Please see the below code part:-   For UploadMasterPages.bat

cls
 set startDate=%date%
set startTime=%time%
 set sdy=%startDate:~10%
set /a sdm=1%startDate:~4,2% - 100
set /a sdd=1%startDate:~7,2% - 100
set /a sth=%startTime:~0,2%
set /a stm=1%startTime:~3,2% - 100
set /a sts=1%startTime:~6,2% - 100
set timestamp=%sdm%-%sdd%-%sdy%-%sth%-%stm%-%sts%
 time /t
 powershell.exe Set-ExecutionPolicy RemoteSigned
powershell.exe -noexit .\UploadMasterPages.ps1 "http://server-name.com/site/"
 time /t

No comments:

Post a Comment