Using PHP/cURL to Create News and Event Items in Sitefinity using WCF

Earlier in the year, my company had decided that we will be moving websites to a new provider, Sitefinity. I couldn’t be happier with the decision, as it is a very modern and robust Web CMS. My only issue is that it is built on a platform that I personally don’t have much experience, ASP.NET. The Sitefinity SDK and Documentation are very thorough, but I was unable to reuse many of the examples as I don’t have a .NET development environment and am only vaguely familiar with C#.

BUT, I have been developing in PHP as a hobby for a few years, and was determined to be able to migrate my old content to the new Sitefinity site programatically. So, I looked closely at Sitefinity‘s Web Services. The web service API is based on the Windows Communication Foundation (WCF) REST approach, which are two technologies that I had just learned earlier this year while working on the RAMCO API.

So, after many hours of debugging WireShark traces and http responses, I was finally able to create some sample content in Sitefinity using their Web Services, PHP, and cURL.

I’ve created a public repository for this sample code on github here:

https://github.com/dconroy/PHPSifefinityWebServices/

This sample code allows you to to Login, Create a News Item, Create a Event Item, Print all News Items, Print all Event Items…all in PHP!

Here is a snippet of my Create News Function:

 
function CreateNews($sitefinityHost,$newsServiceUrl, $cookie) {
 
$title ="The Title";
$newurl="The-URL";
$author="Web Service";
$sourceurl="http://www.daveconroy.com";
$sourcename="Dave Conroy";
$content="Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
 
//build News Service URL
$url = $sitefinityHost.$newsServiceUrl."00000000-0000-0000-0000-000000000000/";	
 
//build the News JSON
 
$objDateTime = new DateTime('NOW');
$date=$objDateTime->format('U');
$date=$date."000"; //add milliseconds required by sitefinity
$myDate= date("U");
$newsStory = array("Item"=>array(
                   "Title"=>array("PersistedValue"=>"$title",
                                  "Value"=>"$title"),
                   "UrlName"=>array("PersistedValue"=>"$newurl",
                                    "Value"=>"$newurl"),
                    "ItemDefaultUrl"=>array("PersistedValue"=>"$newurl",
                                            "Value"=>"$newurl"),
                    "Author"=>array("PersistedValue"=>"$author",
                                     "Value"=>"$author"),
                    "DefaultUrl"=>"$newurl",
                    "SourceName"=>"$sourcename",
                    "SourceSite"=>"$sourceurl",
                    "Content"=>array("PersistedValue"=>"$content",
                                     "Value"=>"$content"),
                    "PublicationDate"=>"/Date($date)/")
     );
 
    //encode news statement
    $newsStory = json_encode($newsStory);
 
    //start HTTP Command session
    $headers = array(
              'Content-Type: application/json',
              'Expect: 100-continue',
              'Accept:'
 
    );	
 
    $session = curl_init($url);
    curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($session, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($session, CURLOPT_COOKIEFILE, $cookie);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($session, CURLOPT_POSTFIELDS, $newsStory);
    curl_setopt($session, CURLOPT_COOKIEJAR, $cookie );
    $response = curl_exec($session);
    echo $response;
    curl_close($session);
 
}

After I was able to use the Web Services to create place holders(content id guid’s) for each news item, injecting the HTML from my old site into the new one via direct SQL queries was a breeze.

I know there are other developers out there who are not experienced with .NET, and I wanted them to know that you can still use this great product.

If you find a problem, incorrect comment, obsolete or improper code, please me know on github or by replying to this blog post. Thanks for reading!

Read More