Ok now we have the main structure of our Portal with the new Areas we wanted and we need to start adding some content.
As the Area's we have created are Publishing sites we're going to start using the new Publishing API's in WSS to add content.
The first function to help us with this is called Add-Content. This function takes a Site Collection URL, Area URL, Contents Title, Content Text in HTML format and a check-in comment. This function adds the given text and title to the default content page in the Publishing site.With a few changes you could modify it to add to a specific content page.
# Function: Add-Content# Description: Add the given text and title to the default publishing page in the publishing web# Parameters: SiteCollectionURL URL for the root of the Site Collection# Area Relative URL to the site/subweb/area# Title Title string for the page# Text Content to publish# Comment Checkin commentfunction Add-Content($SiteCollectionURL, $Area, $Title, $Text, $Comment){$url = $SiteCollectionURL + "/" + $Areawrite-host "Adding content to " $url$site = new-object Microsoft.SharePoint.SPSite($url)$web = $site.OpenWeb()$pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)$pp=$pubweb.DefaultPage$pp.CheckOut()# Set the properties$item=$pp.Item$item.set_Item("Title",$Title)$item.set_Item("PublishingPageContent",$Text)$item.Update()# Checkin, Approve and Publish$pp.CheckIn( $Comment )$pp.Approve( $Comment )$pp.Publish( $Comment )}
Note the set_Item syntax, the SharePoint item property seems to conflict with PowerShell's built in properties.This code assumes Moderation is turned on the Publishing Site.
So we have a handy function to add content but again I want to source that content from an external source. In this case it's an XML file called content.xml the start of which looks like this:<contents><content><area>Divisions/Finance</area><title>Finance Division</title><text><b>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</b><hr/><br/> Nullam hendrerit lacinia purus. Proin vulputate porta nisl. Aliquam commodo lobortis lacus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed quis leo imperdiet nisl ultrices fringilla. Praesent enim est, commodo sed, pharetra mattis, condimentum ut, arcu. Morbi aliquet lacus vel elit. In quis nibh. Vestibulum tincidunt. Sed quis sem. Maecenas lobortis convallis dolor. Nunc rutrum, nunc ac elementum tempor, velit est vehicula libero, eu fermentum lacus lacus ac diam. Curabitur risus quam, dignissim ut, mollis vel, nonummy at, metus. Mauris elit libero, interdum sit amet, sollicitudin nec, eleifend vel, magna. Aliquam at ipsum. Ut rutrum convallis turpis. Quisque urna quam, tincidunt id, pretium a, dignissim ac, neque. Donec ipsum. Sed ornare pretium diam. Phasellus massa. Morbi porttitor purus eget turpis. Sed vel lectus. Etiam egestas nibh vitae augue. Ut felis arcu, fermentum sed, venenatis eget, tincidunt et, lorem. Etiam commodo nisi ut nisl. Morbi augue enim, accumsan eu, dignissim a, scelerisque non, nunc. Praesent mauris ante, pretium eget, volutpat quis, congue eget, odio.<br/> Praesent sed orci nec sapien molestie viverra. Aenean pulvinar dictum mauris. Quisque ligula est, vestibulum id, consequat vel, ullamcorper id, libero. In tristique. Duis turpis augue, egestas sed, semper eget, dignissim sed, nisl. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Curabitur feugiat dolor cursus lacus. Curabitur dui augue, tempus id, sollicitudin vel, pharetra a, augue. Donec vestibulum dictum ligula. Ut velit dui, dignissim at, interdum quis, convallis a, arcu. Vivamus neque lorem, sollicitudin et, malesuada sed, rhoncus eu, pede. Aliquam hendrerit imperdiet lacus. Duis iaculis fringilla risus. Aliquam pretium ante quis arcu. Nam erat est, porttitor non, venenatis at, auctor sed, libero. Sed condimentum, ante vitae dignissim egestas, dolor nisl rutrum elit, nec ultrices lacus ipsum et pede. Duis dolor turpis, lacinia eu, facilisis ut, fermentum sit amet, nisl. Proin imperdiet mauris a lacus. Nullam suscipit imperdiet tellus.</text> ........
Its a simple format that has the relative URL of the destination area, the content title and the contents in encoded HTML in its XML nodes. I've encoded the HTML as support for CDATA sections is not as easy as it should be in PowerShell at the moment.
Now I was going to use the import-xml cmdlet that was available in Monad but that seems to have disappeared in recent builds so to import this file I'm going to use the get-content cmdlet, create a variable from it (the $() syntax) and cast it to XML
$xml=[XML]$(get-content Content.xml)
The [XML] is a shorthand way to load the XML into an XMLdocument class so
$xml | get-member
shows the typename as being the XmlDocument class. If you're used to using this in C# or VB.Net then you have access to the class as normal but the cool thing about PowerShell is the $xml variable now has extra properties that match the nodes in the XML file
$xml.contents.content
will list the content nodes and
$xml.contents.content[0].area
will show Divisions/Finance
The import-content function will do all the hard work.I'm just piping the list of content nodes into the add-content function and referencing the node name as properties
# Function: import-content# Description: Use the XMl import file to import some sample content into the Publishing Web# Parameters: SiteCollectionURL URL for the root of the Site Collection# ImportFile XML file containing the sample datafunction import-content($SiteCollectionUrl,$ImportFile){$xml=[XML]$(get-content $ImportFile)# Loop through the xml items$xml.Contents.Content | foreach-object { Add-Content $SiteCollectionUrl $_.Area $_.Title $_.Text }}
So with a few lines of code this function is loading the contents of the XML file and piping the values of the nodes into our add-content function.
Lets have a look at what the Portal looks like so far
All this and we have not had to use the MOSS UI at all!
Now all those imported content pages will need to be approved. Do it by hand maybe? nope, thats our next PowerShell powered step.
Powered by: newtelligence dasBlog 2.0.7226.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Colin Byrne
E-mail