Monday, October 22, 2012

Removing a web part from a page recursively with Powershell

This week we were making some changes to the content on our orders pages, we decided to remove a web part that was showing some static content and replace it with an SSRS report that I blogged about earlier. I needed this to quickly remove a web part off of each of my 500+ orders sites, so Powershell was looking like the tool of choice for this operation, and let me just take a moment to say I am stoked that at last I am spending more time in Powershell to do even simple things that are now too slow going through the SharePoint UI, removing or adding web parts via Powershell is an awesome way to do it. I was able to pick at a few samples I found online and create something that worked for me.

 

Friday, October 19, 2012

Using Powershell to add a file from a shared drive to a SharePoint Document Library

One of our older systems was outputting reports to a shared folder on our network, we needed an automated way to grab the file and add it to a SharePoint Document Library. So time to break out the Powershell ISE and see if we can do this with out breaking a sweat. First make sure the service account has access to both the network share and the SharePoint Document Library, otherwise you will spend a bit of time trying to troubleshoot your script. The script is pretty well commented so no need to further explain.

Monday, October 1, 2012

Add/Update SharePoint Webpart with Powershell

I recently had a request to add a banner with the Site Title - Site Description on a yellow background to everyone of my 500 SharePoint Orders Dashboard sites front pages I had created. For jobs this big I always break out Powershell!

I decided to use a Content Editor web part that pointed to a script located in a shared Document Library at the root of my site collection. By doing this I can add an empty Content Editor Web Part to each of my 500 dashboard pages, programatically setting it's properties and placement on the page. I will call this web part Title Banner Web Part and it needs to be placed into the Header zone and it had to be the 1st one in that zone.

How this is going to work depends on using a web part page, which I am already doing, that has the out of the box masterpage so we can target a specific class named s4-pagedescription and a div with an id of ctl00_PlaceHolderSiteName_onetidProjectPropertyTitle to get the information we want. I found these by going to my page that I needed the banner on and using F12 to open the IE developer tools and then locating the 2 items I needed in the html.







Once I had my banner looking the way I wanted I saved the file as TitleBannerWebPart.txt and uploaded it to my a scripts document library located on the root site of my site collection that has domain users with read permissions. You can test out the script by pasting it into a content editor web part and saving the page. I keep my scripts like this so that if a system wide change is required I can update one or two lines of code and be good to go.

Now that I have my script ready we are going to log into our server and open up the Powershell ISE and get to work using a content editor web part and set it's properties to point to our banner script stored in the root document library called scripts and set the height to 80 and the Chrome Style to None so we do not see the title or border of the web part.

## CONTACT INFO
## ------------------------------------------------
## CREATED   : 2013.03.30
## MODIFIED  :
## AUTHOR    : Jason Lasby 
## EMAIL     : digitalslavery@gmail.com 
##
## DESCRIPTION
## ------------------------------------------------
## Script to loop through a set of Subsites and  
## add a content editor web part to a page stored in 
## the SitePages Document Library named Dashboard.aspx
## 
## EXPLANATION
## ------------------------------------------------
## This example illustrates how to dynamically loop
## through a set of sub sites and add a web part 
## to a page in a set of sites that use the same page layout
## and are configured with the same web parts, think about 
## a company dashboard site where each department may 
## report standard data like who is in who is out, 
## work related accidents, finance data, shipping
## information, etc... We can loop through our sites
## and add/update or remove web parts for all sites at once.
##
## 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



Showing an SSRS Report in a SharePoint Modal Dialog

One of the challenges I have had recently was trying to integrate the awesome features of SQL Server Reporting Services and SharePoint. Our installation of SSRS is in SharePoint Integrated mode so if you are following along you will need to make sure you are running the same setup.

My objective with this project was to add a Content Editor web part to all 500 of our dashboard sub sites that pointed to a single file that use some javascript to open a SharePoint modal dialog window with the report in it. This report had open in a  window that was 1024x800 in order to see the whole report. Also for this particular project I was not worried about refreshing my dashboard page once the modal window was closed, so that is not included in this script.

I created a document library called scripts that lives in the root site of my site collection, inside I created a text file called reports.txt, every single content editor webpart points to this file. The final thing to note here is that I am getting the order number from the url, which also happens to be my sub sites name, I need this number to pass to my report so it will generate no matter which Order dashboard you go to.


// Here I centered the button to the middle of the web part