Thursday 26 January 2012

Update a Content Query Web Part using Powershell

Here is a quick little script you can use to change the properties of a content query web part using powershell. I have a whole load of them to do post-deployment and i'm dynamically building the query to do so.

I am using SharePoint 2010 Publishing with a subsite called "news" being deployed with every site. On the home page of each site we have a styled content qeury web part which is being populated from the "news" subsite. I did an export of the content query web part (along with several others) and because i'm doing everything in powershell to automate the deployment, i figured i'd dynamically update the web parts after they have been deployed. That allows me a litttle more control later.


#Set the Site Url and find our publishing page.
$SiteUrl = http://sharepointsite.sharepoint/subsite

$WpWeb = Get-SPWeb $SiteUrl
$PageFolder = $WpWeb.GetFolder("pages")
$Page = $PageFolder.Files Where-Object { $_.Name -eq "default.aspx" }

#Check the page out
$Page.CheckOut()

#Find our web part - the one i'm looking for is "Latest News" and it's visible to all.
$AllWebParts = $WpWeb.GetWebPartCollection($page,"Shared")
$MyWebPart = $AllWebParts Where-Object { $_.Title -eq "Latest News” }

In my case, want to update the WebUrl property to point at my $SiteUrl+"/news" -
just type $MyWebPart if you want to see all of the available properties.

#Update the property. I want to ALWAYS point to the subsite of my current site
$MyWebPart.WebUrl = $siteurl+"/news"
$MyWebPart.ItemLimit = "5"

#Save changes to the web part it's self & Check in and publish -
$AllWebParts.SaveChanges($MyWebPart.StorageKey)
$Page.Update()
$Page.CheckIn("")
$Page.Publish("")

#Because its a publishing page - we'll approve it.
$pweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($WpWeb)
$publishingPage = $pweb.GetPublishingPages() Where { $_.Name -eq "default.aspx"}
$publishingPage.ListItem.File.Approve("Page approved by ps")

#job done
$pweb.dispose()
$WpWeb.Dispose()




Right, thats all for today.
Have Fun!

Thursday 19 January 2012

Add a Publishing Page and Set it to Default Welcome Page with custom Page Layout (PowerShell)

As part of a rather extensive process, i am currently writing a site structure deployment script that gets around the problem of being unable to deploy Publishing Sites from a template.

Instead, I've built my publishing site templates in a dev environment, exported the pages to XML, and using that xml, I am deploying brand new sites from the out of box Publishing template, and carefully reconstructing each site to match my template. I am doing this via powershell (of course) and using an excel sheet with all of the site types pre-defined.

A big issue I had was applying a custom page layout to the default.aspx page produced by the out of box template - I decided that this wasn't best practice and instead - I create a "Home.aspx" file, set it as default welcome page and apply my custom page layout.

As a taster, here's how I am creating the new pages, applying the custom page layout and setting them as default, welcome page BEFORE importing all of the default site content, and most importantly, the page content.

function CreatePages([string]$SiteUrl, [string]$PageLayoutName)
{

$site = New-Object Microsoft.SharePoint.SPSite($SiteUrl)
$psite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($site)
$web = Get-SPWeb $SiteUrl
$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

#Create new Page(s)
$pl = $pubWeb.GetAvailablePageLayouts() Where { $_.Name -eq $PageLayoutName }
$newHomePage = $pubWeb.AddPublishingPage("home.aspx", $pl)
$newHomePage.Update()

# Check-in and publish page
$newHomePage.CheckIn("")
$newHomePage.ListItem.File.Publish("")

$newAboutPage = $pubWeb.AddPublishingPage("about-us.aspx", $pl)
$newAboutPage.Update()

# Check-in and publish page
$newAboutPage.CheckIn("")
$newAboutPage.ListItem.File.Publish("")


write-host "Published new page "

#Set new page to be the welcome (home page)
$assignment = Start-SPAssignment
$rootFolder = $web.RootFolder
$rootFolder.WelcomePage = "Pages/home.aspx"
$rootFolder.Update()
Stop-SPAssignment $assignment

write-host "Set as new default "

$site.Dispose()
$web.Dispose()

}

As you can see, I'm passing in just the "NAME" of the page layout, using [Microsoft.SharePoint.Publishing.PublishingWeb.GetAvailablePageLayouts() and a filter to bring back JUST that page layout.

Here we create the new page and apply the page layout.

$newHomePage = $pubWeb.AddPublishingPage("home.aspx", $pl)
$newHomePage.Update()


At some point, I will do a break down of the entire script. It does quite alot and is a pretty good alternative to a custom site defination. Although - given more time and resource, a custom site definition is the best way to solve this problem.


Have fun!
Ryan

Tuesday 10 January 2012

3 Ways to Save SharePoint 2010 Publishing Sites as a Template

Well first post of 2012 - last year was a fantastic year filled with all sorts of juicy SharePoint solutions. I have a few blog posts in Draft that I am looking forward to publishing about powershell, DPM and Property Bags, but for now heres a wee easy one that might save you some time!

I'm currently tasked with packaging a series of 2010 publishing sites, the challenge in doing so is not only that it's "not supported by Microsoft" but publishing makes it exceedingly difficult.

All of our sites for this particular part of the project are publishing sites and have all been pre-created on a lab. The requirement is that following the standard new-site creation practice, a fully customised publishing site (with subsites!) is deployed from site template.

To do this, I saved the top level Publishing Site as a template, imported it into Visual Studio 2010 and added the subsites etc. More about this later.

I discovered that you can't save a site as a template once publishing features are enabled. I found 3 ways of getting round this but be warned, it's not supported by MS. Infact, it's claimed that publishing sites saved as templates and redeployed won't upgrade later.

Anyways, first thing you can do is go to your site http://sharepoint.net/MyPublishingSite

you'll notice that "Save Site as Template" is missing from site actions. Here is 3 ways in which you can do this.

1. Go straight to the save template page

just add "/_layouts/savetmpl.aspx" to your url.
http://sharepoint.net/MyPublishingSite/_layouts/savetmpl.aspx
This will let you save "MyPublishingSite" as a template.

If you really want to knock SharePoint out of support (it's ok for labs or dev machines) –

2. Edit the PublishingSiteSettings.xml file in the hive.

C:\PROGRAM FILES\COMMON FILES\MICROSOFT SHARED\WEB SERVER EXTENSIONS\12\TEMPLATE\FEATURES\PUBLISHING

Open PublishingSiteSettings.xml in notepad.

At the bottom there’s a tag for HIDECUSTOMACTION.

Comment it out like so:

<!--<HideCustomAction Id="HideSaveAsTemplate" HideActionId=”SaveAsTemplate" GroupId="Customization" Location="Microsoft.SharePoint.SiteSettings" /> -->

Then add the custom action for saving as template:

<CustomAction Id="SaveAsTemplate" GroupId="Customization" Location="Microsoft.SharePoint.SiteSettings" Rights="AddAndCustomizePages,BrowseDirectories,ManagePermissions,ManageSubwebs,ManageWeb,UseRemoteAPIs,ViewFormPages" Sequence="60" Title="$Resources:SiteSettings_SaveAsTemplate_Title;"> <UrlAction Url="_layouts/savetmpl.aspx" /> </CustomAction>

3. Use PowerShell

$Web=Get-SPWeb http://sharepoint.net/MyPublishingSite
$Web.SaveAsTemplate(“Template Name”,”Template Title”,”Template Description”,1)

That’s it, have fun!