Tuesday, 10 November 2015

Programmatically adjusting the "toolbar type of a ListViewWebPart using Powershell script"

I have a requirement to hide new item or edit list in SharePoint list



$powershellSnapin = “Microsoft.Sharepoint.Powershell”
if ((Get-PSSnapin -Name $powershellSnapin -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin $powershellSnapin
}

try
    {
        $web= Get-SPWeb "url"  #url
       $webpartmanager = $web.GetLimitedWebPartManager("/Lists/1Contracts/AllItems.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) #Get the webpart manager class

        for($i=0;$i -lt $webpartmanager.WebParts.Count;$i++)  
            {
                if($webpartmanager.WebParts[$i].title -eq "1Contracts")   #list title
                    {  
                        $wp=$webpartmanager.WebParts[$i]; 
                        #$wp.ChromeType="TitleAndBorder"; #If you want to change ChromeType,Uncomment #.  
                        
                        $wp.View.Toolbar = "AllItems" #Getting view
                        [xml]$x = $wp.XmlDefinition
                        $x.View.Toolbar.Type = "None" # or Standard, Full, Freeform #Toolbar types
                        $wp.View.SetViewXml($x.view.OuterXml)
                        $wp.View.Update()

                        $webpartmanager.SaveChanges($wp);  

                        break; 
                    }
    
            }
 
       $web.Dispose(); 
  }
  catch
    {
        Write-Host  Error  :  $_.exception
    }