Solved

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

Posted on 2013-06-13
15
2,074 Views
Last Modified: 2013-06-19
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
Comment
Question by:skipper68
  • 8
  • 7
15 Comments
 
LVL 9

Author Comment

by:skipper68
Comment Utility
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
 
LVL 8

Expert Comment

by:vaderj
Comment Utility
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
 
LVL 9

Author Comment

by:skipper68
Comment Utility
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
 
LVL 8

Expert Comment

by:vaderj
Comment Utility
if anything else, another option might be to delete the existing webpart and readd it with all the properties you want
0
 
LVL 9

Author Comment

by:skipper68
Comment Utility
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
 
LVL 8

Expert Comment

by:vaderj
Comment Utility
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
 
LVL 8

Accepted Solution

by:
vaderj earned 500 total points
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 9

Author Comment

by:skipper68
Comment Utility
Awesome Vader!  Thanks.  I'll try this monday morning and let you know how it goes.
0
 
LVL 8

Expert Comment

by:vaderj
Comment Utility
How did it go?
0
 
LVL 9

Author Comment

by:skipper68
Comment Utility
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
 
LVL 9

Author Comment

by:skipper68
Comment Utility
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
 
LVL 8

Expert Comment

by:vaderj
Comment Utility
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
 
LVL 9

Author Comment

by:skipper68
Comment Utility
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
 
LVL 9

Author Comment

by:skipper68
Comment Utility
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
 
LVL 8

Expert Comment

by:vaderj
Comment Utility
Try adding
$webpart.ShowToolbarWithRibbon = $false

 To your deployment script
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Set OWA language and time zone in Exchange for individuals, all users or per database.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now