Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2260
  • Last Modified:

Changes To Default.aspx Via Powershell Not Saving To Sharepoint Site

I have this powershell script which attempts to make a change to the "Toobar" property of a webpart on the default.aspx page.

 <Toolbar Type="None"/>

$site = Get-SPSite "http://WebApplication/sites/SiteCollection/"
$webcoll = $site.AllWebs

#Step through each web in site collection
foreach ($web in $webcoll) {
#	$page =  $web.GetFile("default.aspx")
#	$page.CheckOut()
	$wpm = $web.GetLimitedWebPartManager("default.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
	$wpm.WebParts | ft title, description
	foreach($webpart in $wpm.WebParts){
		$wpTitle = $webpart.Title
		if($wpTitle -eq "SiteLibrary"){
			[xml]$x = $webpart.XmlDefinition
			$x.View.Toolbar.Type = 'Standard'
			$x.View.Toolbar.SetAttribute('ShowAlways','TRUE')
			$webpart.XmlDefinition = $x.InnerXml
			$wpm.SaveChanges($webpart)
		}
		$web.Dispose()
	}
#	$file.CheckIn("Updated/Set property of webpart",1)
#	$file.Publish("Updated/Set property of webpart")
	
}#foreach

$site.dispose()

Open in new window


Values Before Script
Toolbar Type BeforeThe properties get set correctly while the script is running...
Toolbar Type After...but when I go to the site, or through designer, the toolbar type is still the original value ("None").  I have pressed refresh in designer and even closed and reopened Designer to see if it refreshes the changes with no success.

Can anyone offer a solution to why it's not saving my changes?
0
skipper68
Asked:
skipper68
  • 8
  • 7
1 Solution
 
skipper68Application Development ManagerAuthor Commented:
I've tried the following with no luck...
1. Check out, edit, and check in
2. Adding the $web.Update()

$site = Get-SPSite "http://WebApplication/sites/SiteCollection/"
$webcoll = $site.AllWebs

#Step through each web in site collection
foreach ($web in $webcoll) {
	$web.GetFile("default.aspx").CheckOut()

	$wpm = $web.GetLimitedWebPartManager("default.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
	$wpm.WebParts | ft title, description
	foreach($webpart in $wpm.WebParts){
		$wpTitle = $webpart.Title
		if($wpTitle -eq "SiteLibrary")
		{
			[xml]$x = $webpart.XmlDefinition
			$x.View.Toolbar.Type = 'Standard'
			$x.View.Toolbar.SetAttribute('ShowAlways','TRUE')
			$webpart.XmlDefinition = $x.InnerXml
			$wpm.SaveChanges($webpart)			
		}
		$web.Update()
		$web.Dispose()
	}
	$web.GetFile("default.aspx").CheckIn("Updated/Set property of webpart",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
	
}#foreach

$site.dispose()

Open in new window

0
 
vaderjCommented:
Hard to say, but when in my scripts, when i instanciate the GetLimitedWebPartManager, instead of 'default.aspx', i give it the full URL of the object, which I pull from my PubPage instance - for instance:
(btw, $config.SPWeb.PageTitle = the title of a page i was creating in this case)


$pubPage = [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($item)
$pubPage.title = "$config.SPWeb.PageTitle"
$pubPage.update()

$URLa = $pubpage.publishingweb.url -replace " ",""
$URLb = $pubpage.url -replace " ",""
$webpartmanager=$spWeb.GetLimitedWebPartManager("$URLa/$URLb",[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpart = $webpartmanager.webparts | Where-Object {$_.Title -eq "Search Box"}


$pubPage.CheckIn("")
$pubPage.ListItem.File.Publish("")

===============================

I dont see you accessing the publishing infrastructure - is your site a publishing site?
0
 
skipper68Application Development ManagerAuthor Commented:
It's not a publishing site which is why initially commented out the check-in\out parts.  No sense in having extra steps, right?  Also, the 'publish' function threw an error because the publishing features are not enabled.

I'll see if selecting the webpart the way your code shows has any effect.  

Thanks!!
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
vaderjCommented:
if anything else, another option might be to delete the existing webpart and readd it with all the properties you want
0
 
skipper68Application Development ManagerAuthor Commented:
The webpart is customized with a specific width, height, view, etc.  I've never programmatically added a webpart with all of these variables.  Do you have an example I can use?
0
 
vaderjCommented:
I sure do but I'm out enjoying the local beers of Portland an my code is in tfs
 at work - first thing in the morning I will get it posted - on a side note, we just finally solved our wrist content deployment failure that always through an operation timed out error - it boiled down to a specific NIC/driver. Content deployment is a fickle #!***
0
 
vaderjCommented:
So here is how we deploy our custom web parts via script, its really no different than other web parts - you just have to know the full qualified name (type name).
One way to get this if you dont know what it is, is to download SharePoint Manager (spm.codeplex.com), goto :

Farm\Services\SPWebService\Web Applications\[Your Web App]\Sites\[Your Site Collection]\Lists\Web Part Gallery\Items\[Your Custom Web Part]\Properties\Web Part Type Name




*** Company.Project.WebPart   = The full qualified name / type name is in the value of that property described above - you will want to replace Company.Project.WebPart with that


$webpart = new-object Company.Project.WebPart
$webpart.AllowClose = $false;
$webpart.AllowConnect = $false;
$webpart.AllowEdit = $false;
$webpart.AllowHide = $false;
$webpart.AllowMinimize = $false;
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::TitleOnly;
$webpart.Title = "My Custom Webpart"
$webpartmanager.AddWebPart($webpart,"TopRightRow",3)

Open in new window

0
 
skipper68Application Development ManagerAuthor Commented:
Awesome Vader!  Thanks.  I'll try this monday morning and let you know how it goes.
0
 
vaderjCommented:
How did it go?
0
 
skipper68Application Development ManagerAuthor Commented:
Well, it went. lol

I have a 300 line script that wrote and tested Sunday and yesterday.  What I ended up having to do was:
1. Export the webpart to a .webpart file
2. Delete the webpart
3. Create the webpart using the XML based .webpart file

It was awful and probably very inefficient but it works.  I'll post the code in an attachment here.  Thank you for the direction.  I'll give you the points because you offered moral support on top of the technical stuff too :-)  Thanks again.
0
 
skipper68Application Development ManagerAuthor Commented:
Maybe you can help me out with one more thing before I award you the points?  
     I need to make a modification to the .webpart XML file before I create the webpart from it.  

I know it's here where I'd need to make the change, but wondered if you had a way to modify an existing tag?
$xtr = New-Object System.Xml.XmlTextReader($localWebpartPath)
     [void] [Reflection.Assembly]::LoadWithPartialName("System.Text")
    $sb = new-object System.Text.StringBuilder
 
         while ($xtr.Read())
         {
            $tmpObj = $sb.AppendLine($xtr.ReadOuterXml());
         }
         $newXml =  $sb.ToString()
 
    if ($xtr -ne $null)
    {
        $xtr.Close()
    }

Open in new window

I need to add Show Always="TRUE" to the XMLDefinition property like this one has

        <property name="XmlDefinition" type="string">&lt;View Name="{B0FCAD0F-13FE-425D-9402-A588FA79BF6C}" MobileView="TRUE" Type="HTML" Hidden="TRUE" DisplayName="" Url="/sites/SubSite/default.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/images/dlicon.png"&gt;&lt;Query&gt;&lt;OrderBy&gt;&lt;FieldRef Name="FileLeafRef"/&gt;&lt;/OrderBy&gt;&lt;/Query&gt;&lt;ViewFields&gt;&lt;FieldRef Name="DocIcon"/&gt;&lt;FieldRef Name="LinkFilename"/&gt;&lt;FieldRef Name="Modified"/&gt;&lt;FieldRef Name="Editor"/&gt;&lt;/ViewFields&gt;&lt;RowLimit Paged="TRUE"&gt;30&lt;/RowLimit&gt;&lt;Toolbar Type="Standard" ShowAlways="TRUE"/&gt;&lt;/View&gt;</property>
0
 
vaderjCommented:
Well I don't know how to do it that way, but it does have the allow close, allow hide and allow minimize properties all disabled. After inserting the web part, are users able to hide it?
0
 
skipper68Application Development ManagerAuthor Commented:
Yes the users are able to hide the webpart
dropdown view
Do you think it may not be using this file to do the import?
0
 
skipper68Application Development ManagerAuthor Commented:
Here's the exported file with the toolbar type set to "No Toolbar" and the file with it set to "Show Toolbar" and the only difference is the "Show Always = "TRUE""
SiteLibrary-NoToolbar.txt
SiteLibrary-ShowToolbar.txt
0
 
vaderjCommented:
Try adding
$webpart.ShowToolbarWithRibbon = $false

 To your deployment script
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now