zoukankan      html  css  js  c++  java
  • Add/Remove listview web part in publish site via powershell

     

    1. Here is the code: Add WebPart in Publish Site

    Example : AddWebPartPublish http://localhost  "/Pages/Publish.aspx" "Shared Documents" "Header" "0"

    ###########################################################################

    # $siteUrl : The site url -Example: http://localhost                                                                       #

    # $pagePath: The page path                                                                                                       #

    # $listName: The name of list that should be added. -Example: Birthdays                                 #

    # $webpartZone: The zone in page  - Example: "Header"                                                          #

    # $index: The index of web part in the zone  -Example: "0"                                                       #

    ###########################################################################

    #  Import the Microsoft.SharePoint.PowerShell
    
    if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
    {
        Add-PSSnapin Microsoft.SharePoint.PowerShell
    }
    Add-PSSnapin System.Reflection -erroraction silentlyContinue
    
    function  AddWebPartPublish($siteUrl, $pagePath, $listName, $webpartZone, $index)
    {
            $pageUrl = $siteUrl + $pagePath
            $spWeb = Get-SPWeb $siteUrl -ErrorAction Stop
            
            [Microsoft.SharePoint.Publishing.PublishingWeb]$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spWeb);
            $allowunsafeupdates = $spWeb.AllowUnsafeUpdates
            $spWeb.AllowUnsafeUpdates = $true
            
            $page = $spWeb.GetFile($pageUrl);
            if ($page.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
    	    { 
              if ($page.CheckedOutBy.UserLogin -eq $spWeb.CurrentUser.UserLogin) 
              {
                write-host "Page has already been checked out "
              } 
              else 
              {
                $SPWeb.CurrentUser.LoginName
    		    $page.UndoCheckOut()
    		    $page.CheckOut()
                write-host "Check out the page override"
              }  
    		  
            }
            else
            {
               $page.CheckOut() 
               write-host "Check out the page"
            }
            try{
                    #Initialise the Web part manager for the specified profile page.
                    $spWebPartManager = $spWeb.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
                    
                    
                    #  Check list is exist or not
                    foreach($list in $spWeb.Lists)
                    {
                        if($list.Tostring() -eq $listName)
                            {
                                write-host "The list named $list is existing"
                                #$ListViewWebPart = New-Object Microsoft.SharePoint.WebPartPages.ListViewWebPart
                                $ListViewWebPart = New-Object Microsoft.SharePoint.WebPartPages.XsltListViewWebPart
                                $listViewWebPart.Title = $listName
                                $ListViewWebPart.ListName = ($list.ID).ToString("B").ToUpper()
    
                               
                                #$ListViewWebPart.ChromeType = "none"
                              
                               
                                $ListViewWebPart.ViewGuid = ($list.Views[0].ID).ToString("B").ToUpper()
                               
                                $spWebPartManager.AddWebPart($ListViewWebPart, $webpartZone, $index)
                                $spWebPartManager.SaveChanges($ListViewWebPart)
                               
                            
                                
                                break;
    
                            }
                    }
                    
    
                    
                    
                    
                    #Check to ensure the page is checked out by you, and if so, check it in    
                    if ($page.CheckedOutBy.UserLogin -eq $spWeb.CurrentUser.UserLogin)    
                    {        
                        $page.CheckIn("Page checked in automatically by PowerShell script")        
                        Write-Output "Page has been checked in"    
                    }
                    
                    $page.Publish("Published")
                    Write-Output "Page has been published success"
                    $pubWeb.Close()
                    $spWeb.Update()
                    
                    
                    if($flag -eq 0)
                    {
                        write-host "Not exist"
                    }
                    else
                    {
                        Write-Output "success"
                    }
                }
    
            catch
                {
                    write-host "(ERROR : "$_.Exception.Message")"
                    throw
                 }
                 
            finally
                {
                
                    $spWeb.Dispose()
                    
                }
    
    } 
    
    

     

    2. Here is the code: Delete WebPart in Publish Site

    Example : RemoveWebPart " http://localhost"  "/Pages/Publish.aspx" "Shared Documents"

    ###########################################################################

    # $siteUrl : The site url -Example: http://localhost                                                                       #

    # $pagePath: The page path                                                                                                       #

    # $listName: The name of list that should be added. -Example: Birthdays                                 #                                                  

    ###########################################################################

     

    #  Import the Microsoft.SharePoint.PowerShell
    
    if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
    {
        Add-PSSnapin Microsoft.SharePoint.PowerShell
    }
    
    function  RemoveWebPart($siteUrl, $pagePath, $name)
    {
          #$web  = $site.OpenWeb("/Ops")  
            # Check the parameter is null or not
            if($siteUrl.trim() -eq $null -or $name.trim() -eq $null -or $pagePath.trim() -eq $null)
            {
               write-host "The parameter is null"
               return 
            }
            $pageUrl = $siteUrl + $pagePath
            $spWeb = Get-SPWeb $siteUrl -ErrorAction Stop
            
            [Microsoft.SharePoint.Publishing.PublishingWeb]$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spWeb);
            $allowunsafeupdates = $spWeb.AllowUnsafeUpdates
            $spWeb.AllowUnsafeUpdates = $true
            
            $page = $spWeb.GetFile($pageUrl);
            if ($page.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
    	    { 
              if ($page.CheckedOutBy.UserLogin -eq $spWeb.CurrentUser.UserLogin) 
              {
                write-host "Page has already been checked out "
              } 
              else 
              {
                $SPWeb.CurrentUser.LoginName
    		    $page.UndoCheckOut()
    		    $page.CheckOut()
                write-host "Check out the page override"
              }  
    		  
            }
            else
            {
               $page.CheckOut() 
               write-host "Check out the page"
            }
            try{
                    #Initialise the Web part manager for the specified profile page.
                    $spWebPartManager = $spWeb.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
                    $flag = 0
                    #Circle all the Webparts in the specified page
                    foreach ($webpart in $spWebPartManager.WebParts)
                    {    
                        #write-host $siteUrl +": Existing Web part - " + $webpart.Title + " : " + $webpart.ID   
                        if($webpart.Title -eq $name)
                        {
                       
                            $spWebPartManager.DeleteWebPart($spWebPartManager.WebParts[$webpart.ID])
                             $spWebPartManager.SaveChanges($webpart)
                            Write-Output "Delete the webpart"
                            $flag = 1
                            
                            break;
                        }
                    }
                    
                    
                    #Check to ensure the page is checked out by you, and if so, check it in    
                    if ($page.CheckedOutBy.UserLogin -eq $spWeb.CurrentUser.UserLogin)    
                    {        
                        $page.CheckIn("Page checked in automatically by PowerShell script")        
                        Write-Output "Page has been checked in"    
                    }
                    
                    $page.Publish("Published")
                    Write-Output "Page has been published success"
                    $pubWeb.Close()
                    $spWeb.Update()
                    
                    
                    if($flag -eq 0)
                    {
                        write-host "Not exist"
                    }
                    else
                    {
                        Write-Output "success"
                    }
                }
    
            catch
                {
                    write-host "(ERROR : "$_.Exception.Message")"
                    throw
                 }
                 
            finally
                {
                
                    $spWeb.Dispose()
                    
                }
    
    }
    


     

     

     

  • 相关阅读:
    OGG初始化之使用Oracle Data Pump加载数据
    OGG初始化之使用数据库实用程序加载数据
    OGG初始加载过程概述
    管理凭证存储中的身份
    使用ENCKEYS方法加密数据
    使用主密钥和钱包方法加密数据
    配置Oracle GoldenGate安全性
    建立Oracle GoldenGate凭证
    System.DllNotFoundException:“无法加载 DLL“SQLite.Interop.dll”: 找不到指定的模块 的解决方法
    记录一下Oracle添加、查看表、字段注释及导出ddl
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3174369.html
Copyright © 2011-2022 走看看