Archive

Posts Tagged ‘documentation’

Creating SharePoint list items with PHP

March 3, 2010 17 comments

As requested, here’s an example of creating a SharePoint list item with PHP. If you read my previous post (Reading a SharePoint list with PHP) you’ll notice that the code is very similar. In fact, only the CAML query (which is contained in the SOAP request) and the Lists method has changed. Once again, I recommend following this simple guideline when coding a SharePoint application from PHP or Java :

  • Use a local copy of your SharePoints Lists WSDL file (or any other SharePoint Web Service WSDL file you’ll be using).  You’ll avoid the painful task of having to deal with Microsoft’s NTLM authentication protocol. By doing this you’ll be using basic authentication, unless your server has a special security configuration. This means less code, less maintenance, and quicker deployment. Your Lists WSDL file must be downloaded from your own SharePoint server (at the URL: sharepointdomain.com/subsite/_vti_bin/Lists.asmx?WSDL).

Here are resources that will help you construct your CAML query:

To get the code to work, you’ll need the NuSOAP library, your own local Lists WSDL file,  and of course your own personalized authentication/list variables in the code below. This code has been tested with SharePoint Online and PHP 5.3, but should work with MOSS 2007.

<?php

// Requires the NuSOAP library
require_once('lib/nusoap.php');

$username = 'yourUsername';
$password = 'yourPassword';
$rowLimit = '150';

/* A string that contains either the display name or the GUID for the list.
 * It is recommended that you use the GUID, which must be surrounded by curly
 * braces ({}).
 */
$listName = "TempList";

/*
 * Example field (aka columns) names and values, that will be used in the
 * CAML query. The values are the attributes of a single list item here.
 * If the field name contains a space in SharePoint, replace it
 * here with _x0020_ (including underscores).
 */
$field1Name = "Title";
$field2Name = "Address";
$field3Name = "Premium_x0020_customer";

$field1Value = "John Smith";
$field2Value = "USA";
$field3Value = "1";

/* Local path to the Lists.asmx WSDL file (localhost). You must first download
 * it manually from your SharePoint site (which should be available at
 * yoursharepointsite.com/subsite/_vti_bin/Lists.asmx?WSDL)
 */
$wsdl = "http://localhost/phpsp/Lists.wsdl";

//Basic authentication is normally used when using a local copy a the WSDL. Using UTF-8 to allow special characters.
$client = new nusoap_client($wsdl, true);
$client->setCredentials($username,$password);
$client->soap_defencoding='UTF-8';

//CAML query (request), add extra Fields as necessary
$xml ="
 <UpdateListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
 <listName>$listName</listName>
 <updates>
 <Batch ListVersion='1' OnError='Continue'>
 <Method Cmd='New' ID='1'>
 <Field Name='$field1Name'>$field1Value</Field>
 <Field Name='$field2Name'>$field2Value</Field>
 <Field Name='$field3Name'>$field3Value</Field>
 </Method>
 </Batch>
 </updates>
 </UpdateListItems>
";

//Invoke the Web Service
$result = $client->call('UpdateListItems', $xml);

//Error check
if(isset($fault)) {
 echo("<h2>Error</h2>". $fault);
}

//extracting the XML data from the SOAP response
$responseContent = utf8_decode(htmlspecialchars(substr($client->response,strpos($client->response, "<"),strlen($client->response)-1), ENT_QUOTES));

echo "<h2>Request</h2><pre>" . utf8_decode(htmlspecialchars($client->request, ENT_QUOTES)) . "</pre>";
echo "<h2>Response header</h2><pre>" . utf8_decode(htmlspecialchars(substr($client->response,0,strpos($client->response, "<")))) . "</pre>";
echo "<h2>Response content</h2><pre>".$responseContent."</pre>";

//Debugging info:
//echo("<h2>Debug</h2><pre>" . htmlspecialchars($client->debug_str, ENT_QUOTES) . "</pre>");
unset($client);
?>

SharePoint Web Services: 3 things to consider before coding in Java

February 3, 2010 1 comment

Before starting the adventure of coding an application in Java for SharePoint Online, make sure you’re well prepared for the following points:

  • Documentation is rare. No code examples exist on MSDN other than for C# or VB.NET (and some SOAP signatures). Moreover, translating MSDN’s C# examples into Java is tough, as they use language specific imports. The best info I could find was a webcast with a java coding demo for SharePoint (in French: MSDN Webcast by Stève Sfartz). The SharePoint Online Developer Guide and the MSDN documentation will be your best friends though.
  • Microsoft’s WSDLs won’t generate easy to use POJOs. This is essentially because SharePoint Online’s SOAP Web Services are resource-orientated. This means you can send big and complex requests to SharePoint’s Web Services, but also that you’ll have to manipulate your objects like XML documents rather than objects. And the “XAML” structure used in the requests/responses isn’t always fully documented with examples on MSDN either.
  • Consuming SharePoint Online’s WSDLs in your favorite Java IDE won’t necessarily work. I had to use the Axis 1.4 from the command line to generate my Java classes for Netbeans (the generated classes couldn’t be compiled when imported from Netbeans wizard). Your mileage may vary…

Therefore, if you have the possibility to use C# instead of Java, don’t think twice: use C#. You’ll save a lot of development and maintenance time. I wouldn’t recommend developing an application in Java if its size goes beyond the one of a small application or proof-of-concept.