1. You can upload master pages as well as Blank-web-part
2. Just create a folder in c drive and rename the folder to Docs and paste the master page or blank web part page in the folder
3. Run the command in power shell
4. The command will upload, publish and approve the files which are under Docs folder.
Note: Do not forget to change the url.
$webAppUrl = "http://c4968397007/" # Web Application URL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint.Administration")
$checkInComment="Check In by PowerShell"
$publishComment="published by PowerShell"
$approveComment="Approved by PowerShell"
$logfile = "UploadMasterPage_$(get-date -f yyyyMMdd_hhmmss).log"
$spsite = new-object Microsoft.Sharepoint.SPSite($webAppUrl);
$web = $spsite.RootWeb;
$masterPageList = ($web).GetFolder("Content Web Parts")
# Get file system path
$masterPageLocalDir = "C:\Docs"
#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/Display Templates/Content Web Parts/" + $file.Name;
$masterPageFile=$web.GetFile($destUrl)
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/Display Templates/Content Web Parts/" +$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
{
write-Output $_ | out-File $logfile -Append
}
}
$web.dispose();
$spsite.dispose();
2. Just create a folder in c drive and rename the folder to Docs and paste the master page or blank web part page in the folder
3. Run the command in power shell
4. The command will upload, publish and approve the files which are under Docs folder.
Note: Do not forget to change the url.
$webAppUrl = "http://c4968397007/" # Web Application URL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint.Administration")
$checkInComment="Check In by PowerShell"
$publishComment="published by PowerShell"
$approveComment="Approved by PowerShell"
$logfile = "UploadMasterPage_$(get-date -f yyyyMMdd_hhmmss).log"
$spsite = new-object Microsoft.Sharepoint.SPSite($webAppUrl);
$web = $spsite.RootWeb;
$masterPageList = ($web).GetFolder("Content Web Parts")
# Get file system path
$masterPageLocalDir = "C:\Docs"
#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/Display Templates/Content Web Parts/" + $file.Name;
$masterPageFile=$web.GetFile($destUrl)
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/Display Templates/Content Web Parts/" +$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
{
write-Output $_ | out-File $logfile -Append
}
}
$web.dispose();
$spsite.dispose();
No comments:
Post a Comment