Sunday, March 31, 2013

Custom SharePoint Site Title Bar using jquery


One of the new items we wanted to add to our dashboard front pages was a site name and description bar across the top of the pages for our dashboards. Each of our dashboard sites was created using a Powershell export of a site we configured exactly the way we wanted, making it our template site, and then importing the site with a new name.

This gives us 100s of sites with the same configuration, page layout, and webparts named the same, but content is specific to each site such as financial data and other reports.

The approach to solving this issue was to use a bit of css, jquery, and a content editor web part. The content editor web parts will allow you to either enter free text and decorate it any way you like, or you can create your layout and copy the script and paste it into notepad and then save the file to a document library at the root of your site that has read access for any of your visitors. We also keep a copy of jquery 1.8.3 in this same document library called scripts. The newest version of jQuery breaks some of our functionality, so be sure to use the earlier version.

One requirement in building this was it had to be easy to update, so by keeping our code for the content editor web part in a single text file stored at the root of our site and then configuring each web part to point to this file we can now update hundreds of web parts with a single bit of code.

The end results were that I needed to have this yellow Title banner web part on each of my sites, the title banner is basically using jQuery to find some class tags on the page that contain the current site's title and description, two items already on the page, but not very easy to read.


You can see when we view the web part properties that the values have been set by our script, note the Content Link field, it points to my root document library where the web part reads the script that all the web parts will read, if we need to make a change to the web part this is where it will be done.


Each of these sites in the drop down need this dynamic web part added to it's Dashboard.aspx web part page.


Another example of the web part on a different site










Now that all that is working we need a way to automatically inject this web part onto all of our sub sites at once, this is where Powershell will prove its self once more. My previous post of Add/Update a web part with Powershell should be all you need to sort out the automation part.

Saturday, March 30, 2013

Powershell Delete a webpart from a SharePoint webpart page loop

Today I find that when I create new things, often times I create too many things during my testing of say dynamically adding a webpart to a SharePoint webpart page. So I need to have a way to quickly loop through my hundereds of sites and delete the webpart.


## CONTACT INFO
## ------------------------------------------------
## CREATED   : 2013.03.30
## MODIFIED  :
## AUTHOR    : Jason Lasby 
## EMAIL     : digitalslavery@gmail.com 
##
## DESCRIPTION
## ------------------------------------------------
## Script to loop through a set of Subsites and  
## Delete a web part from a page stored in the SitePages 
## Document Library named Dashboard.aspx
## 
## EXPLAINATION
## ------------------------------------------------
## This example illustrates how to delete a webpart 
## from a set of sites that use the same page layout
## and 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 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"

function RemoveWebPart 
{
 foreach($wp in $wpm.WebParts | ?{$_.Title -like "* Banner *"})
 {
  if($wp)
  {
   $listOfWebParts = $wp.ID
   foreach($matchFound in $listOfWebParts)
   {
    $wpm.DeleteWebPart($wpm.WebParts[$matchFound])
    Write-Host "$matchFound was deleted for $webSite"
   }
  }
     
 }
}

Start-SPAssignment -Global
## Create a variable to hold the SharePoint site, invoke the connection
## to the site we defined in the $parentSite variable
$web = Get-SPWeb $parentSite
## Check to make sure that url exists
if($web)
{
 ## Loop through all the sub sites of the site url we got
 foreach($webSite in $web.Webs)
 {
  $webSiteName = $webSite.Name
  $pageUrl = "SitePages/dashboard.aspx"
  $page = $webSite.GetFile($pageUrl) 
  $wpm = $webSite.GetLimitedWebPartManager($page, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
  if($wpm)
  {
   RemoveWebPart 
  }
  else
  {
   Write-Host "Web part not found"
  }
   
 }
}
 else
 {
  ## Gracefully let the user know that the site they requested
  ## was not available or was not found.
  Write-Host "$webSite not found, check the url and try again."
 }

Stop-SPAssignment -Global


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.


## 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

Monday, March 4, 2013

SharePoint dynamically load content into an iframe

For some weird reason I have had this fascination with trying to use the iFrame with a CEWP and some javascript to basically load in other content found within the site collection into a static page. I guess one of the things I really don't like about SharePoint is all the button clicking required to use it on a daily basis, also I have to often find a no code type solution, so this will be ideal for a quick prototype.

This time I finally succeeded in getting this to work pretty close to the way I wanted it to. This working examples uses a few things, I have a site that has several dashboard type sites under it. Each dashboard sub site will have a Pages Library and about 10 web part pages in it. These pages are standard web part pages and as such can be used for showing a set of related bits of data on my main page on as their own independent pages, reports, or other generalized groups of information.

My goal was to have the main page for each sub site have a either a HTML Form Web Part or a  Content Editor Web Part on it. The CEWP will point back to a centrally managed script file called dashboardIframe.txt that I stored in the root site in a document library called scripts and set domain users to have read permissions. To get things going I am going to use the HTML Form Web Part and edit the code right on the page.

The script has a set of buttons with pre-defined urls, that when clicked with run a bit of javascript that basically dynamically sets the src attribute of the iframe and forces it to reload, giving us the additional content we would like to display.

?isdlg=1 needs to be appended to each url to hide the ribbon just like when the modal dialog window opens.
My example below loads the main page from each one of my dashboard sub sites, there is only the description of the site and its name showing right now on each page.
Each button click loads a page from a  different SharePoint site into our iframe
This has potential to be pretty useful for when you have too much stuff on one page and you want to break it apart and centralize where you go to see it or a wide range of content from pretty much anywhere in  your portal. This works great for displaying views of libraries and lists too, plus you only load what your users want to see.