Using SharePoint Lists and Powershell to populate content pages.   Leave a comment

   This article assumes you can create script and get to the point where you have a sharepoint site and a list object to work with.  It is not a complete script, but contains complete SharePoint API element functions you need to use to complete your own script with very little extra work on your part.

The Issue:

Needing to create 600 ish pages of content, from a CSV file of links descriptions and images.


Page layouts are identical, doesn;t require hundreds of columns of data to describe the page.  Each set of links on each page are different, but critically, all links, image paths etc are in the CSV datafile. 

Then…the requirement to update the content became one that needed some controls…lacking in a CSV file! 

I suggested using a SharePoint list – custom columns etc, – secure by default – , and then wrote this little beauty to go create all the pages.   I REALLY Love PowerShell! – Oh and the SharePiont API!

Script requirements:

  • Import Content to create pages from SharePoint List.
  • Create Web Part Page – do a loop of your choice to create each page, but perform the following ops on each page once you have created it.
  • Clear Title Image and change description in the web page title bar webpart (part of page creation).
  • Create Construct content and populate a CEWP (Content Editor Web Part).
  • Populate pages and commit to SharePoint Document library.

Script bits:

Import required page contents from SharePoint list in a simple array (I like simple:)):

# Create SimpleListArray

$inputDataArray = @()

$SPList = $web.Lists[$iDataSiteList]

$SPItems = $SPList.Items

I cheat and use the following construct to create an empty array Item:

foreach ($item in $SPListItems){

$arrayItem = @()
$arrayItem = “” | select <FieldName1>, <FieldName2>, …etc, etc…
$arrayItem.<FieldName> = $item.[“<FieldName1>”]
# etc…
# Populate into DataArray object.
$inputDataArray += $arrayItem

The result should be a nice simple array that you can use when creating your pages…

Create Web Part Page:

So for each page array Item we need to go create the page alter the header and set description and add CEWP and populate.

This should be a function btw, that also calls all the other functions, so you have a loop: (Create page -> Create CEWP Content -> add CEWP and content) – rinse and repeat for the next array item.  Hope that makes sense, email me if not!

$Global:list = $iWeb.Lists[“$library”];

$Global:iDataArray = $iDataArray

foreach ($PageItem in $iDataArray)

$title = $iDataArray.<TitleField>

# Batch XML required to create page and update TitleBar Web Part, in this case not setting an image.

“<?xml version=`”1.0`” encoding=`”UTF-8`”?>
<SetList Scope=`”Request`”>$($list.ID.GUID)</SetList>
<SetVar Name=`”Cmd`”>NewWebPage</SetVar>
<SetVar Name=`”ID`”>New</SetVar>
<SetVar Name=`”Type`”>WebPartPage</SetVar>
<SetVar Name=`”WebPartPageTemplate`”>2</SetVar>
<SetVar Name=`”Title`”>$title</SetVar>
<SetVar Name=`”HeaderImage`”></SetVar>
<SetVar Name=`”Overwrite`”>true</SetVar>
# Create Page.


# Cast result so we can parse for result, for logging of further action.

[xml]$xCreatePageResult = $createPageResult

# Now check for errors, and do what you need to do with them.

if( $xCreatePageResult.Results.FirstChild.Code -ne 0 )

{ # Do your “I went pear shaped” code for the error – I handle it by writing to a log and continuing.}

Create and populate a CEWP to the page:

# Create content based on business rules and content of array item field properties.

$content1 = “<Some HTML snippet. with $<variable for your field entry>>”
if ($<fieldName>) {$content2 = “<Some HTML Snippet”} 
$finalContent = $content1 + $content2, etc…

Edit the pages Title Bar Content and remove the default image:
Function Edit-Title-Bar-Content ($iPageURL)
# Get Web part manager and set the web part id to fiddle with
Open-Site $SiteURL
$webpartmanager=$web.GetLimitedWebPartManager($iPageURL, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

for ( $i=0; $i -lt $webpartmanager.WebParts.count; $i++ )
if ($webpartmanager.WebParts[$i].Gettype() -eq [Microsoft.SharePoint.WebPartPages.TitleBarWebPart])
$webpart = $webpartmanager.webparts[$i]
$webpart.Image = “”
$webpart.Title = $title
$webpart.HeaderTitle = $title
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
# Cleanup

} # End Function.

Populate page – Add Web Part  and Content to page in SharePoint Document library:
Function Add-ContentEditorWebPart($SiteURL, $pageUrl, $webpartzone, $index, $title, $content)
$webpartzone = "Header"
# Open-Site $SiteURL
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$webpart = new-object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
$webpart.Title = $title

$docXml = New-Object System.Xml.XmlDocument
$contentXml = $docXml.CreateElement(“Content”);

$webpart.Content = $contentXml;
$webpartmanager.AddWebPart($webpart, $webpartzone, $index);

} # End Function.


Posted September 30, 2011 by stormwalker255 in SharePoint

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: