Creating and editing publishing pages in SharePoint is a task that can be automated using the SharePoint Object Model.
A common scenario for this is a content migration tool that picks content items from any legacy content management system and automatically creates or updates the corresponding pages in a SharePoint publishing site.
Is this example, the migration tool reads a Microsoft Access Database containing a table named ContentItems and creates a new page for each record, mapping the database table columns into page fields in the publishing page.
The resulting pages should look like this...
Migration Utility (command line)
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\..\\Database\\Content.accdb;Persist Security Info=False";
string siteUrl = "http://www.myPublishingSite.local";
string pageLayoutFile = "ArticleLeft.aspx";
string pageNamePrefix = "NewPage";
ContentMigrationLib.ContentDatabase instance = new ContentMigrationLib.ContentDatabase(connectionString);
instance.UploadContent(siteUrl, pageLayoutFile, pageNamePrefix);
}
}
The ContentDatabase class implementation is:
ContentMigrationLib
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Fields;
namespace ContentMigrationLib
{
public class ContentDatabase
{
// references
// http://msdn2.microsoft.com/en-us/library/ms493244.aspx
// http://mindsharpblogs.com/aaron/
// http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingpage.fields.aspx
private string m_connectionString;
public ContentDatabase(string connectionString)
{
m_connectionString = connectionString;
}
public void UploadContent(string siteUrl, string pageLayoutFile, string pageNamePrefix)
{
//get content items from database
OleDbConnection connection = new OleDbConnection(m_connectionString);
connection.Open();
OleDbCommand command = new OleDbCommand("SELECT ID, Title, ByLine, [Page Content] FROM ContentItems", connection);
OleDbDataReader reader = command.ExecuteReader();
using (SPSite site = new SPSite(siteUrl))
{
//pick the page layout to use
PublishingSite publishingSite = new PublishingSite(site);
PageLayoutCollection pageLayouts = publishingSite.GetPageLayouts(false);
string serverRelativeUrl = "/_catalogs/masterpage/" + pageLayoutFile;
PageLayout layout = pageLayouts[serverRelativeUrl];
//for each content item
while (reader.Read())
{
int id = reader.GetInt32(0);
Dictionary<string,string> fields = new Dictionary<string,string>();
fields.Add("Title", reader.GetString(1));
fields.Add("Byline", reader.GetString(2));
fields.Add("Page Content", reader.GetString(3));
string pageName = String.Format("{0}{1}.aspx", pageNamePrefix, id.ToString());
//create the publishing page
CreateNewPage(site.RootWeb, layout, pageName, String.Empty, fields);
}
}
connection.Close();
}
public void CreateNewPage(SPWeb web, PageLayout pageLayout, string newPageName, string checkInComment, Dictionary<string, string> fields)
{
// Validate the input parameters.
if (null == web)
{
throw new System.ArgumentNullException("web");
}
if (null == pageLayout)
{
throw new System.ArgumentNullException("pageLayout");
}
// Get the PublishingWeb wrapper for the SPWeb that was passed in.
PublishingWeb publishingWeb = null;
if (PublishingWeb.IsPublishingWeb(web))
{
publishingWeb = PublishingWeb.GetPublishingWeb(web);
}
else
{
throw new System.ArgumentException("The SPWeb must be a PublishingWeb", "web");
}
// Create the new page in the PublishingWeb.
PublishingPageCollection pages = publishingWeb.GetPublishingPages();
PublishingPage newPage = pages.Add(newPageName, pageLayout);
foreach (string fieldName in fields.Keys)
newPage.ListItem[fieldName] = fields[fieldName];
newPage.Title = fields["Title"];
newPage.Update();
// Check in the new page so that others can work on it.
newPage.CheckIn(checkInComment);
}
}
}

No comments:
Post a Comment