Wednesday, March 1, 2017

Powershell Script to Disable Web part – Enable Webpart – Create page in to Page Library – Add Web part in to Page – Publish that page

 


Add-PsSnapin Microsoft.SharePoint.PowerShell

$rootURL = “Your Web Application URL”

$Feature = “Your Feature GUID  ex – https://yourwebappurl
$webPartTitle = “Web Part Name”;
$custwebPartTitle =”Web Part Title”;
$PageName = “Page Name you want to Create in Page Library – Ex – Test.aspx ”
$PageTitle = “Page Title”

// Below variable to create this page and add webpart in to that page in to all site collection

//ex Test:Pages – Pages is the libarry you want to create page in to

$MarketPlaceNamelist = @(
                    "Test:Pages", "Test1:Pages"

)


foreach($MarketPlaceName in $MarketPlaceNamelist)
{
    $arr=$MarketPlaceName.ToString().Split(":")
    $siteURL = $rootURL +"/portal/"+$arr[0]
    Write-Host("Updating " + $siteURL ) -ForegroundColor Yellow ;
    $lstSitePagesName = $arr[1]
   
    # Initialize the Site Object
    $Site = Get-SPSite($SiteUrl)

    Write-Host("Disabling Feature WebPart") -ForegroundColor Yellow ;
    Disable-SPFeature -identity $Feature -URL $siteURL -confirm:$false
    Write-Host("Enabling Feature WebPart") -ForegroundColor Yellow ;
    Enable-SPFeature -identity $Feature -URL $siteURL 
    Write-Host("Adding Page in to Page Library") -ForegroundColor Yellow ;

    function Add-WebPartToPage($web,$pageUrl, $webpartzone,$index,$fileName,$custWPTitle,$ChromeType)
    {
        $Webp=$site.RootWeb
       # $webPartGallery = $Webp.Lists[$lstWebPartGalleryName]
   
        #Write-Host "Searching webpart $fileName in web part gallery" -ForegroundColor Yellow
        #if($webPartGallery -eq $null)
        #{
        #    Write-Host("Unable to retrieve Webpartgallery");
        #}
        $webpart = $null;
        #$webpart=$webPartGallery.Items | ? { $_.Title -eq $fileName}

        $wpCatlog =[Microsoft.SharePoint.SPListTemplateType]::WebPartCatalog
        $spList = $Webp.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::WebPartCatalog)        
       
        foreach ($spItem in $spList.Items)
        {            
            if($spItem.Title.Contains($webPartTitle))
        {          
            $webpart = $spItem;
        }
        }
        
        if($webpart -eq $null) {
            Write-Host("Unable to retrieve webpart: $fileName") -ForegroundColor Red
        }
        else {   
        Write-Host("----------Adding Webpart--------")-ForegroundColor Yellow
        Write-Host $webpart.Title -ForegroundColor Yellow
        $webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
        $errorMsg = "";
        $xmlReader = New-Object System.Xml.XmlTextReader($webpart.File.OpenBinaryStream());
            $webpart = $webpartmanager.ImportWebPart($xmlReader,[ref]"Error")
            $webpart.Title = $custWPTitle;
            $webpart.ChromeType = $ChromeType
        $webpartmanager.AddWebPart($webpart, $webpartzone, $index);
   
           }   

        $Webp.Dispose()
    }


    function CreatePage($web,$PageLayoutRelUrl,$PageName,$PageTitle)
    {

         # Get the Publishing Site based on the SPSite
         $PubSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Site)
    
         # Initialize the PublishingWeb based on the SPWeb
         $PubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($Web)
  
         # Get the PageLayouts Installed on the Publishing Site
         $Layouts = $PubSite.GetPageLayouts($False)

    
 
         # Get our PageLayout
         $PageLayout = $Layouts[$PageLayoutRelUrl]
  
         # Create a new publishing page.
         $Page = $PubWeb.AddPublishingPage($PageName, $PageLayout)
 
         # Assign the Title for the Page
         $Page.Title = $PageTitle
 
         # Update the Page
         $Page.Update();
    
         #Write-Host $PageURL
         return $Page

    }

    function PublishPage($Page)
    {
        TRY
        {
            # Check in the Page with Comments
             $Page.CheckIn("Checked in using powershell")
 
             # Publish the Page With Comments
             $Page.ListItem.File.Publish("Publish using powershell")
         }
         CATCH
         { }

    }

    $web=$site.RootWeb

     # Get the blank webpart PageLayout
    $PageLayoutRelUrl = "_catalogs/masterpage/BlankWebPartPage.aspx"
   
    $Page=CreatePage $web $PageLayoutRelUrl $PageName $PageTitle
    $PageURL=$Page.Uri.ToString()
    Add-WebPartToPage $web $PageURL "Header" 0 $webPartTitle $custwebPartTitle "None"
    PublishPage($Page)
    $web.Update()
    $web.Dispose()
    $site.Dispose()
    Write-Host("All Updates Are done") -ForegroundColor Green ;
    Write-Host("-----------------------------------------------------------") -ForegroundColor Green ;
    Write-Host("-----------------------------------------------------------") -ForegroundColor Green ;

}

No comments:

Post a Comment