Thursday, May 24, 2012

The holy grail for SharePoint developers just getting started

While recently looking for some code samples for creating a custom web part I came across this little gem from Microsoft, the SharePoint 2010 Code Samples 101 download located here: http://code.msdn.microsoft.com/SharePoint-2010-101-Code-da251182

This is a great collection of Visual Studio Solutions written in C# to get you started on your next custom project for SharePoint 2010.

Tuesday, May 22, 2012

Create a SharePoint sub site from a site export

Our issue was some 3rd party web parts were not allowing us to save a site as a template through the normal way of going into the Site Actions > Site Settings > Save Site as a template.

Our solution involved using Powershell to do an export of the site we wanted to use as a template. The main challenge with this is that you need to first create a SharePoint site, then do an Import-SPWeb to restore the site that was exported, then set the title and description. (This works with both http and https sites.)

# Define root site where the shell of the sub site will be created under
$site = "https://portal/"
   
Write-Host "Please enter the Title of the site to create"       
$webTitle = Read-Host "Please enter the title of the site you wish to create"    
$newSiteUrl = "https://portal/$webTitle"     
# Create empty subsite
$web = New-SPWeb $newSiteUrl -name $webTitle -UseParentTopNav
# Use the base SharePoint template of STS#1
$web.ApplyWebTemplate("STS#1")
# Give the site a name
$web.Name = $webTitle
# Apply changes
$web.Update()
# Since we are using a custom template for the sites we will want to set some variables
$templateName = "https://portal/$webTitle"
$templateLocation = "C:\Temp\Template\site.bak"
# Restore the site from the back up
Import-SPWeb $templateName -Path $templateLocation -IncludeUserSecurity
# We need to update some properties of the restored site, so we set the site as a variable
$web = (Get-SPWeb -Identity "https://portal/$webTitle")
# Once the site is restored this will overwrite the title and description which we set here
$web.title =$webTitle
$web.description = "Template Site"
# Update the site
$web.update()
# Clean it up
$web.dispose()
  
Write-Host "The Installation is complete please visit the site at" $web.URL

Monday, April 16, 2012

SharePoint 2010 Table of Contents web part using javascript

One of the things I love about SharePoint is the ability to quickly create solutions utilizing the html web part or the content editor web part, lately I have been using the html web part as it is just a bit friendlier to use when adding client side code to handle tasks.

Today I have been trying to find an easy way to list all the sub sites in order to create a Table of Contents for our users, and while there are several ways you can do this without having to use code the Tree view and the actual Table of Contents web parts just were not meeting my needs, so a little javascript magic to the rescue!

Add a html web part to your page and then click the edit web part
Click the source editor button in the properties dialog box on the right
and paste in this script:


This script will loop through your sub sites and display a list of sites, I took it one step further and made each item a link that will take you to the site listed.

Tuesday, April 3, 2012

Powershell error when creating a new SharePoint Site

I ran into another odd problem with my SharePoint 2010 deployment yesterday.  I was trying to create a new site using the New-SPWeb PowerShell command and I kept getting the following error message:

PS C:\Users\rsmw> new-spweb http://sharepoint.local/newspweb
New-SPWeb : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:1 char:10 + new-spweb <<<<  http://sp2010.mps.k12.mi.us/warrensm2
 + CategoryInfo          : InvalidData:
(Microsoft.Share....SPCmdletNewWeb:SPCmdletNewWeb) [New-SPWeb],
UnauthorizedAccessException
 + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletNewWeb
 
The account I was logged in with was a farm administrator and I could create sites through the web interface.   Apparently, I needed to add the farm administrator account to the Owners group of the parent site in order to create a new site. After that, all was good to go.

Sunday, April 1, 2012

Using PowerShell to set SharePoint 2010 MasterPage Recursively

During a recent development project to create a custom branded MasterPage for our 2010 portal, the page I had created had an EventReceiver that set the MasterPage to my custom MasterPage, however I forgot to enclose one of my vars with a semi colon and the page deployed but would not render, I kept getting a SharePoint error saying File not found!!! Even after retracting the solution with Visual Studio I kept getting the same error. I needed some way to reset the MasterPage to v4.master, enter PowerShell, my favorite way to work with admin task. Thanks to Todd Lindt's quick post found here:
http://www.toddklindt.com/blog/Lists/Posts/Post.aspx?ID=226

The master page setting is scoped at the web level, so the first thing I did was use Get-SPWeb to get a variable for the web I want to change. There are two properties that control the master page settings; MasterUrl and CustomMasterUrl. The former controls the master page used to render the System pages (the ones that start with /_layouts) and the latter controls the master page used to render the content pages.


To alter those settings for a publishing site at http://sharepoint/, use the following PowerShell script:

$web = Get-SPWeb http://sharepointurl/
$web.CustomMasterUrl = "/_catalogs/masterpage/customMasterPage.master"
$web.MasterUrl = "/_catalogs/masterpage/customMasterPage.master"
$web.Update()
This changes the master page for both settings and content to a customMasterPage.master. You can also set it to the default v4.master if you just need to reset it to the default masterpage. Since we're doing this with PowerShell it's easy to loop through a group of webs and set their master pages to whatever you like

Friday, January 22, 2010

jQuery - How screen scrape a table from another web page

Lately at work we have needed to add additional content to our SharePoint Portal, one of the ways to implement quick and easy functionality is to use a Content Editor Webpart and some jquery. Everyday I find new and interesting ways to utilize this amazing javascript library to automate and execute redundant and boring tasks, thereby making end users lives easier and making myself into something of hometown hero.

This post will demonstrate using jquery to retrieve an html table from a weather website and pull it into your portal page. You will need to have the small jquery.js library uploaded into a shared top level site, there is nothing to install, just download the jquery library file and put it into a document library, as you will need to reference the path to the file when using jquery on your pages.

Also create a text file and add the following script to it,



save it as a .txt file and upload it to the same document library your jquery.js file is in.

Now go to the page you want the scraped item to show up on and put your page into Edit Mode and insert a Content Editor Webpart, once it is on the page then you will need to edit the properties and have it pointed to our text file that contains this script, this is way faster to troubleshoot and debug, no more having to save the page to see your changes, then put it back into edit mode to adjust the script.
This is so simple and yet so powerful! First I am looking to extract a table from the target webpage, so we use the html tag #table, (you could target any html element), in this case the target web page table tags does not contain IDs which would make it extremely easy to find the specific table I need, so I need to use the nth-child() to grab the table I want. You may have to experiment to find which table you want, just keep changing the number. Once you have your html element you can then use jquery to dress it up anyway you want or perform additional functions.

Monday, November 23, 2009

SP2010 PDF Icon - Quick Install for x64 bit systems

This script is updated to install the PDF Icon and re-set IIS for x64 bit systems. It backs up the original file DOCICON.XML to DOCION.old in case you need to recover it. PDFIcon v4 You will still need to download the iFilter for indexing/searching PDF files Adobe PDF iFilter 9 for 64-bit platforms