One of our Sitefinity Partners had a project that required them to migrate content from a legacy homegrown CMS to Sitefinity CMS. To address this challenge they built a small WinForms application that was able to retrieve and parse information from the old web site.
However, after retrieving information from the old web site, the remaining challenge was to use this information to create pages programmatically in Sitefinity.
Sitefinity Pages API + Web Services
Any task that can be done using Sitefinity’s UI can also be done using Sitefinity’s API [webinar]. However, this API is only available within the scope of the Sitefinity web site. Consequently, Sitefinity’s API cannot be used directly from a WinForms application; doing so will result in errors as the API attempts to find missing resources.
Thankfully it’s relatively easy to build a bridge to Sitefinity by creating a custom Web Service. This Web Service will accept incoming requests and then utilize the Sitefinity API to create content.
To get started, create a new AddPage web service in the Sitefinity installation directory.
Then modify the AddPage Web Service code-behind (found in ~/App_Code) to contain the following WebMethod.
~/App_Code/AddPage.cs
using System;
using System.Web.Services;
using Telerik.Cms;
using Telerik.Cms.Engine.WebControls;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class AddPage : System.Web.Services.WebService {
[WebMethod]
public string CreatePage (string title, string url, string parent, string body) {
// The CmsManager is used to interact with Sitefinity pages.
CmsManager manager = new CmsManager();
// Create a new Sitefinity page.
ICmsPage page = manager.CreatePage(url);
// Set properties for the new page
page.MenuName = title;
page.Navigable = true;
page.DenyAnonymous = false;
page.ParentID = ConvertToGuid(parent);
// Checkout the content
IStagedPage staged = page.Staged.CheckOut();
// Set the Template & Theme
IPageTemplate template = manager.GetTemplate("Inner");
staged.SetTemplate(template.ID, "Emerald-Blue");
// Create a new Generic Content control and add the body as content
GenericContent control1 = new GenericContent();
control1.ID = "genericContent1";
control1.Content = body;
// GetPage() must be executed each time changes are made to the page
page = (ICmsPage)manager.GetPage(page.ID, true);
// Add the Generic Content control to the "Content" PlaceHolder.
page.Staged.AddControl("Content", control1);
// GetPage() must be executed each time changes are made to the page
page = (ICmsPage)manager.GetPage(page.ID, true);
// CheckIn and Publish the page
page.Staged.CheckIn();
page.Publish();
// Return the ID for the newly created page
return page.ID.ToString();
}
private Guid ConvertToGuid(object possibleGuid)
{
try
{
return new Guid(possibleGuid.ToString());
}
catch
{
return Guid.Empty;
}
}
}
The code above installs a web service in the Sitefinity web site that can be used from a WinForms application.
Using the Web Service from the WinForms Application
The Sitefinity API can only be used within the scope of the Sitefinity web site. However, a web service [example shown above] can be used to bridge the gap between external applications and Sitefinity.
A web service can be used in a WinForms project by right-clicking the References folder in the Solution Explorer. Then click Add Service Reference:
For Address, enter the URL to the web service then click the Go button.
This web-based API can now be used directly from your Winforms code to create Sitefinity pages:
var client = new AddPageServiceReference.AddPageSoapClient();
client.CreatePage(
"My Test Page",
"~/TestPage.aspx",
Guid.Empty.ToString(),
"The content of my page."
);
Additional Thoughts
This web service has no security. This is probably okay for temporary data migration. However, the web service should be removed after the data migration is complete. Alternately, you can add security to this web service.
Sitefinity 4.0 (coming later this year) will come included with dozens of RESTful web services. These web services are used by the Sitefinity Admin UI to provide fast AJAXified content management. In addition, the web services will be used between Sitefinity web sites to handle data migration or synchronization (staged content, multi-server deployments, etc).