Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-06-13
15
Medium Priority
?
2,228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 9

Author Comment

by:skipper68
ID: 39245951
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 9

Expert Comment

by:vaderj
ID: 39246076
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
ID: 39246102
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 9

Expert Comment

by:vaderj
ID: 39246103
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
ID: 39246584
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 9

Expert Comment

by:vaderj
ID: 39246601
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 9

Accepted Solution

by:
vaderj earned 2000 total points
ID: 39248212
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
 
LVL 9

Author Comment

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

Expert Comment

by:vaderj
ID: 39254798
How did it go?
0
 
LVL 9

Author Comment

by:skipper68
ID: 39256795
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
ID: 39257215
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 9

Expert Comment

by:vaderj
ID: 39257324
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
ID: 39257350
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
ID: 39257365
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 9

Expert Comment

by:vaderj
ID: 39257497
Try adding
$webpart.ShowToolbarWithRibbon = $false

 To your deployment script
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

598 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