## CONTACT INFO ## ------------------------------------------------ ## CREATED : 2013.03.30 ## MODIFIED : ## AUTHOR : Jason Lasby ## EMAIL : digitalslavery@gmail.com ## ## DESCRIPTION ## ------------------------------------------------ ## Script to loop through a set of SharePoint Subsites and create ## a new Document Library at each sub site named SitePages,& ## then add a page to this new SitePages Document Library ## named Dashboard.aspx ## ## EXPLAINATION ## ------------------------------------------------ ## This example illustrates how to dynamically loop ## through a set of sub sites and add a Document Library ## to each sub site and populate it with a web part page. ## ## BEGIN ## ------------------------------------------------ ## Load up the snapin so Powershell can work with SharePoint ## if the snapin was already loaded don't alert the user Add-PSSnapin microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue ## Clear any errors or output to the screen CLS ## Set variables ## This variable will be the site where I want to start $parentSite = "http://sp2010/dashboards" $webPartProperty_Title = "Title Banner Web Part" $webPartProperty_ZoneName = "Header" $webPartProperty_Height = "80" $webPartProperty_Width = "" $webPartProperty_Visible = $true ## All content editor web parts will point to this script $webPartProperty_Link = "http://sp2010/dashboards/scripts/spservicesTest.txt" Start-SPAssignment -Global $site = Get-SPWeb $parentSite $webSites = $site.Webs foreach($webSite in $webSites) { $order = $webSite.Name $page = $webSite.GetFile("/dashboards/$order/SitePages/Dashboard.aspx") $webPartManager = $webSite.GetLimitedWebPartManager($page, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) foreach($webPart in $webPartManager.WebParts) { $listOfAllWebParts = $webPart.Title } ## Check the page to see if the web part was already on it if($listOfAllWebParts -eq $webPartProperty_Title) { Write-Host "$webSite already had web part on it." } ## Create the web part and set some of it's properties else { $cewp = New-Object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart $cewp.ContentLink = $webPartProperty_Link $cewp.Title = $webPartProperty_Title $cewp.Height = $webPartProperty_Height $cewp.Width = $webPartProperty_Width $cewp.Visible = $webPartProperty_Visible $cewp.ChromeType = "None" $cewp.HorizontalAlign = "Center" ## The AddWebPart takes 3 arguments, first is the web part name, ## then the zone id ## and finally the order number where you want the web part to show in the zone $webPartManager.AddWebPart($cewp, $webPartProperty_ZoneName, 0) $webPartManager.SaveChanges($cewp) $webSite.Dispose() Write-Host "Webpart was created for Order $order" } } Stop-SPAssignment -Global
SharePoint 2007/2010 development using C# and Powershell. Mostly just things I have found that make my job easier but I had to search around to find answers to.
Saturday, March 30, 2013
Powershell dynamically add a web part and set it's properties SharePoint
Having some fun today getting a bit further along with my Powershell scripts. Today I needed to dynamically add a SharePoint Document Library to each of my dashboard sites. In the newly created SitePages Document Library I needed to also have a Web Part Page with a Header Web Part Zone and the page needed to be named Dashboard.aspx.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment