Sometimes you need to change the properties of a Web Part without browsing to the page itself and set it. There could be numerous reasons as to why you would want or need to do this. A while back I created a really simple redirect web part for one of my clients. It had two custom properties, EnableRedirect and Url. If EnableRedirect was true, it would redirect the user to the specified Url. Obviously, if you want to change either of these properties while the EnableRedirect is true, that's going to be a problem.
Fast forward and the redirect web parts need to be changed and/or disabled. Obviously we can't navigate to the page and just change it, because the page will redirect you. But it can be done relatively easy using PowerShell.
Start out with opening the site and getting the file we want to work on. Also note that the site I'm working on is a publishing site, which is why we need to check out, check in and publish the page we want to change the web part on.
$site = new-object Microsoft.SharePoint.SPSite("http://sharepointsite")$web = $site.OpenWeb("RandomSite/AnotherSite/")$page = $web.GetFile("Pages/default.aspx")$page.CheckOut()$wpm = $web.GetLimitedWebPartManager("Pages/default.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
From here we can look up and see which web parts are currently active on the page.
$wpm.WebParts | ft
There are two web parts on this page currently, and we want to change the second one. Let's finish of the script by doing this:
$wp = $wpm.WebParts[1]$wp.EnableRedirect = $false$wp.Url = "http://www.google.com"$wpm.SaveChanges($wp)$page.CheckIn("Test")$page.Publish("Test")$web.Close()$site.Close()
And that's it. The redirect is turned off, and it's been changed to point to Google. It's not a work of art, and a little cruddier than what I usually like to make my scripts, but I needed something quick and dirty to do the job. And that's what this does.
Fast forward and the redirect web parts need to be changed and/or disabled. Obviously we can't navigate to the page and just change it, because the page will redirect you. But it can be done relatively easy using PowerShell.
Start out with opening the site and getting the file we want to work on. Also note that the site I'm working on is a publishing site, which is why we need to check out, check in and publish the page we want to change the web part on.
$site = new-object Microsoft.SharePoint.SPSite("http://sharepointsite")$web = $site.OpenWeb("RandomSite/AnotherSite/")$page = $web.GetFile("Pages/default.aspx")$page.CheckOut()$wpm = $web.GetLimitedWebPartManager("Pages/default.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
From here we can look up and see which web parts are currently active on the page.
$wpm.WebParts | ft
There are two web parts on this page currently, and we want to change the second one. Let's finish of the script by doing this:
$wp = $wpm.WebParts[1]$wp.EnableRedirect = $false$wp.Url = "http://www.google.com"$wpm.SaveChanges($wp)$page.CheckIn("Test")$page.Publish("Test")$web.Close()$site.Close()
And that's it. The redirect is turned off, and it's been changed to point to Google. It's not a work of art, and a little cruddier than what I usually like to make my scripts, but I needed something quick and dirty to do the job. And that's what this does.
No comments:
Post a Comment