Quantcast
Channel: SharePoint 2010 - Setup, Upgrade, Administration and Operations forum
Viewing all articles
Browse latest Browse all 13778

Edit WebPart ToolBar Type using Powershell

$
0
0

To address the "Accessing SPView ToolbarType from PowerShell" issue at
https://social.technet.microsoft.com/Forums/en-US/0b5c5a35-c4e4-4191-97a6-c7375c739134/accessing-spview-toolbartype-from-powershell?forum=sharepointadminprevious

For those coming in behind me. Here is FULL script and WORKS GREAT. Just fill in these vars within the script.

$webURL = "Your root site or sub site URL"; #Enter as stated
$pageName = "/Pages/Your Page Name.aspx"; #Enter as stated
$wpTitle = "Your WebPart Title"; #Enter as stated
$listName = "Your List Name"; #Enter as stated
$wpViewName = "Your WebPart View Name"; #Enter as stated, most likely to be "Web Part View"

<#references and acknowledgements:
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spview.toolbartype.aspx
http://blog.markstokes.com/post/2009/01/07/Changing-(or-removing)-the-Toolbar-from-a-ListViewWebPart.aspx
http://jasear.wordpress.com/2009/04/05/listviewwebpart-programatically-setting-the-toolbartype-property/
https://social.technet.microsoft.com/Forums/en-US/0b5c5a35-c4e4-4191-97a6-c7375c739134/accessing-spview-toolbartype-from-powershell?forum=sharepointadminprevious
https://social.technet.microsoft.com/Forums/windowsserver/en-US/bad76507-0e1e-4c3d-8b66-3648fd253076/how-to-change-the-toolbar-type-using-the-powershell
#>
try{
$ver = $host | select version
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"}
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell"
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell" -Completed
}
$webURL = "Your root site or sub site URL"; #Enter as stated
$pageName = "/Pages/Your Page Name.aspx"; #Enter as stated
$wpTitle = "Your WebPart Title"; #Enter as stated
$listName = "Your List Name"; #Enter as stated
$wpViewName = "Your WebPart View Name"; #Enter as stated, most likely to be "Web Part View"
$web = Get-SPWeb $webURL -ErrorVariable err -ErrorAction SilentlyContinue -AssignmentCollection $assignmentCollection;
$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web);
$AllowUnsafeUpdatesStatusWeb = $web.AllowUnsafeUpdates;
$web.AllowUnsafeUpdates = $true;
$culture = new-object System.Globalization.CultureInfo("en-US");
$url = $webURL + $pageName;
$wpMgr=$web.GetLimitedWebPartManager($url,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
$webParts = $wpMgr.WebParts;
foreach($wp in $webParts){
    if($wp.Title -eq $wpTitle ){
        $wpOnPage = $wp.GetType();
        if($wpOnPage.Name -eq "XsltListViewWebPart"){
            $wpID = $wp.ViewGuid.ToString();
            $page = $pubWeb.GetPublishingPage($webURL + $pageName);
            $page.CheckOut();
            $list = $web.Lists[$listName];
            $view = $list.Views[$wpViewName];
            $wpView = $list.Views | ?{$_.ID -eq $wpID};
            $wpView.GetType().InvokeMember("EnsureFullBlownXmlDocument", [Reflection.BindingFlags] "NonPublic, Instance, InvokeMethod", $null, $wpView, $null, $culture);
            $nodeProp = $wpView.GetType().GetProperty("Node", [Reflection.BindingFlags] "NonPublic, Instance");
            [System.XML.XMLNode]$node = $nodeProp.GetValue($wpView, $null);
            [System.XML.XMLNode]$toolbarNode = $node.SelectSingleNode("Toolbar");
            $toolbarNode.Type = "None" # or Standard, Full, Freeform
            $wpView.Update();
            $page.Update();
            $item = $page.ListItem;
            $item.File.CheckIn("Edit " + $wpTitle + " WebPart Toolbar Automatic CheckIn. (Administrator)");
        }
    }
}
}#end try
catch [system.exception]{
    Write-Host "Edit WebPart Toolbar to $wpTitle in Pages Library. A system.exception error. Error msg:"$ErrorMessage = $_.Exception.Message
    Write-Host "Full Error Message:" $errMessageInFull = $_.Exception.ToString();
}#end of catch
finally{
#CLEAN UP
#wrap in if's for $null
i
if(!($AllowUnsafeUpdatesStatusWeb -eq $null)){
$web.AllowUnsafeUpdates = $AllowUnsafeUpdatesStatus;
}
if(!($web -eq $null)){
$web.Dispose();
}
}#end of finally for Edit WebPart


Viewing all articles
Browse latest Browse all 13778

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>