Archive

Posts Tagged ‘php’

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);
?>

Reading a SharePoint list with PHP (updated)

February 23, 2010 65 comments

Updated (March 4, 2010): Added an example that uses the PHP5 built-in SOAP support instead of the NuSOAP library.

In my previous posts I showed a way to interact with SharePoint’s Lists Web Service through a Java application. Today I’d like to show how to do similar basic tricks with PHP. Once again, toying with SharePoint Web Services from a non-Microsoft language isn’t exactly a walk in the park.

I tried to do some experiments lately: I managed to read a SharePoint Online List from PHP, thanks to the nusoap library and a blog post written by Craige Thomas. As usual, a lot of trouble comes from the consumption of a WSDL that lies behind an NTLM/SSL server (in the case of SharePoint Online anyways). Once again, the easiest way to avoid such problems is to download and parse the WSDL file locally (Lists.asmx). Using basic authentication is the way to go when building a simple lists reading application in PHP, solving the NTLM issue implies too many intricacies in my opinion. If you manage to do it with a simple and straightforward solution, please let me know.

The code sample below will basically display the Lists Web Service request and response (see the screenshot above). Before running the code, you’ll have to set the username, password, and the local path to your Lists WSDL file. The logical next step, if you want to manipulate the results, would be to cast the response into a DOM document and parse it for the information you’re looking for.

<?php

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

$username = 'yourLogin';
$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 between curly
 * braces ({}).
 */
$listName = "yourSharePointListName";

/* 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. Using UTF-8 to allow special characters.
$client = new nusoap_client($wsdl, true);
$client->setCredentials($username,$password);
$client->soap_defencoding='UTF-8';

//XML for the request. Add extra fields as necessary
$xml ='
<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<listName>'.$listName.'</listName>
<rowLimit>'.$rowLimit.'</rowLimit>
</GetListItems>
';

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

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

//Extracting and preparing the Web Service response for display
$responseContent = utf8_decode(htmlspecialchars(substr($client->response,strpos($client->response, "<"),strlen($client->response)-1)));

//Displaying the request and the response, broken down by header and XML content
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>";

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

Read more…

Using external business data with SharePoint Online Part 1: Scenario and conception of a solution

January 17, 2010 2 comments

The first project I would like to put forward on this blog concerns the integration of external business data with Microsoft SharePoint Online. This theme concerned a part of my Bachelor’s thesis that I did last summer which was named “Electronic Documents Management Optimization In A Consulting Firm”.  I will share the parts that I found the most interesting and challenging, in my opinion.

Context

First of all I would like to put some context on the topic that I will cover. The company I worked for was a small “deskless” RH consulting business that managed all of its information (addresses, meetings, projects, etc) through the web, on a custom built PHP-MySQL CRM (Customer Relationship Management) server. It was hosted by a local ISP on a linux server.

It soon became apparent that such a CRM wouldn’t be able to support a centralized document management system, which the company increasingly needed. Consequently, they had to find a solution available on the market.

The company finally settled for Microsoft’s cloud based solution, SharePoint Online, which is part of the Business Productivity Online Suite, to manage their documents. It turned out to be perfectly suited for such a small deskless company. It offered great functionality fora very low cost (thanks to the Software As A Service model, where you pay only for what you use), and low maintenance once set up.

Conception of a solution

One of the challenges of SharePoint Online is that there isn’t any obvious way to integrate external business data from a non-Microsoft source.

Abstract schema of a CRM, Connector and SharePoint Online interacting

This was however a very important point in my project, because alot of the company’s documents had to be organized by client’s names. And guess what: the clients names had to be the same in the CRM’s MySQL database and a SharePoint Online’s list.

But thanks to SharePoint Online’s Web Services, it was possible to build a connector in Java that that made the link between the CRM and SharePoint.

It is important to note that the connector can’t be actioned from within SharePoint Online, as it is impossible at this time to deploy custom code on SharePoint’s cloud-based server. Therefore, clients information would need to be entirely manipulated from within the CRM (and therefore the connector), and the SharePoint clients list protected from users manual access.

Java was chosen because it could directly be deployed on the company’s rented LAMP server.

The point here is to show you that if don’t have a Microsoft infrastructure (Access database, Windows OS that can deploy C# code), but would like to integrate existent business data with a low-priced SharePoint Online solution, it is possible. I’ll use PHP and Java as examples.

How it works

The objective was to have the same names of the clients in the CRM and in a SharePoint Online’s clients list. The designed solution worked like this:

SharePoint Online Connector interaction with external business data

  1. A collaborator types in a new client’s information in an HTML form on the CRM, from his computer.
  2. The Apache server, which is running PHP, adds the client’s information in the local MySQL database, while sending the same info to a “Java to SharePoint connector” running on Tomcat (this is done with a PHP-Java Bridge).
  3. The Java connector processes the received data and encapsulates it into an XAML request that is sent SharePoint Online’s ASMX 2.0 Lists Web Service.
  4. The data is added in the list (which name was detailed in a properties file used by the connector). The list in SharePoint Online is the most restricted as possible for SharePoint Users: the whole point of building a connector is lost if clients information is manipulated from more than one place, for the simple reason that SharePoint Online can’t push those information outside of its walls!
  5. SharePoint Online sends the result back to the connector, which is then passed to PHP, which in turn displays the result to the end user.

The Java code used to build the connector and the implementation of the bridge with PHP will be described in further posts. Your comments are welcome!

Welcome to my new blog

January 13, 2010 Leave a comment

Dear cyber visitor,

Thanks for stopping by! Time has come for me to contribute some of my IT projects and experiments to the almighty web!

Firstly, my posts will focus on the interaction possibilities between Microsoft SharePoint Online and non-Microsoft languages like Java, PHP, and JavaScript. I think this topic is pretty interesting because, as of today use cases, documentation and examples remain pretty rare. We’ll see after that!

The content will be split in multiple posts. I’ll upload relevant code and resources as well (yay!).

Here’s what I plan to post (the topics might change):

Using external business data with SharePoint Online

  • Part 1: Scenario and conception of a solution
  • Part 2: Java and SharePoint Online’s Web Services
  • Part 3: Bridging PHP and Java
  • Part 4: Testing and conclusion

Extending SharePoint Online’s functionality with jQuery

  • Part 1: Why a JavaScript plugin?
  • Part 2: Coding the plugin, its loading scheme and its UI
  • Part 3: Copy operations

Thank you for visiting my blog. I’ll be happy hear your feedback on those posts.

David Dudok de Wit

Creativity & Innovation Blogs - Blog Catalog Blog Directory