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