Thursday 20 February 2014

Custom and OOB Web Part details in one or more site collection in SharePoint

Create one CSV file with site collection url detials in "SiteCollectionName" columns.

Below is the power shell script to fetch the web part detials in Power Shell Grid View


if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
function enumerateWebParts($Url) {
    $site = new-object Microsoft.SharePoint.SPSite $Url   
                foreach($web in $site.AllWebs)
                {
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web))
                                {
            $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
            $pages = $pWeb.PagesList
            foreach ($item in $pages.Items)
                                                {
                $fileUrl = $webUrl +"/" + $item.File.Url
                $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                $wps = $manager.webparts
                $wps | select-object @{Expression={$pWeb.Url};Label="Web URL"},@{Expression={$fileUrl};Label="Page URL"}, DisplayTitle, IsVisible, @{Expression={$_.GetType().ToString()};Label="Type"}
            }
        }
        else {
            $pages = $null
            $pages = $web.Lists["Site Pages"]           
                                                if ($pages)
                                                {               
                                                foreach ($item in $pages.Items)
                                                {
                    $fileUrl = $webUrl +"/" + $item.File.Url
                    $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                    $wps = $manager.webparts
                    $wps | select-object @{Expression={$pWeb.Url};Label="Web URL"},@{Expression={$fileUrl};Label="Page URL"}, DisplayTitle, IsVisible, @{Expression={$_.GetType().ToString()};Label="Type"}
                }
            }
            else {
            }
        }        Write-Host"… completed processing" $web
    }
}

#Get the CSV file and connect to the SharePoint list
$vessellist = import-csv -Path "C:\SiteCollectionName.csv"
$itemCount = $vessellist.Count;
$currentItem = 1;
foreach($item in $vessellist)
{
    #Update the progress information
    Write-Progress -Id 1 -ParentId 0 -Activity "Listing Data In CSV File" -PercentComplete (($currentItem/$itemCount)*100) -Status "Item $currentItem or $itemCount";
    $currentItem++;
    #Write the rows VESSEL_NAME column to the console
                $row += enumerateWebParts($item.SiteCollectionName)
}
$row | Out-GridView

Saturday 15 February 2014

Applying/Change the CustomMasterUrl and MasterUrl in master page setting in SharePoint using PowerShell

Provide the URL in $webAppUrl and Below is the code details:-

param($webAppUrl)
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint.Administration")
 
$site=New-Object Microsoft.SharePoint.SPSite($webAppUrl)
$web = $site.openweb();
$web.AllowUnsafeUpdates=$true;
$MasterPageURL="/_catalogs/masterpage/XYZ_publishing.master"
$web.CustomMasterUrl=$MasterPageURL
$web.MasterUrl=$MasterPageURL
$web.Update()
$web.AllowUnsafeUpdates=$false;
$outputText = "XYZ publishing master page is applying in "+ $web.Name +" site."
write-output $outputText
                                
$web.Dispose()
$site.Dispose()

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