Home > SharePoint > Creating SharePoint list items with PHP

Creating SharePoint list items with PHP

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);
?>
  1. March 4, 2010 at 05:37

    Isn’t it better to use PHP5 built-in SOAP support instead of nusoap? I have measured performance of SOAP clients for PHP and nusoap is slowest of them.

    • March 4, 2010 at 20:00

      You’re absolutely right Gunnar, using PHP5′s built-in SOAP support is certainly faster.

      However, the point here was simply to show an example of a PHP/SharePoint interaction that worked, because there just doesn’t seem to be many available out there.

      That being said, I’ve added an example of PHP5′s built in SOAP support for those interested (Reading a SharePoint list): http://davidsit.wordpress.com/2010/02/23/reading-a-sharepoint-list-with-php

  2. April 26, 2010 at 20:02

    Is it possible to see an example using the built-in soap for updating a list item? I was able to read SharePoint lists with little effort, but for the life of me cannot figure out how to update a list.

    Thanks!

  3. David
    June 10, 2010 at 16:30

    David :In order to update your list you need his ID and place it in your xml with Update instead of New and the FIELD NAME=’ID’ and his value>

    • Mike
      September 10, 2011 at 02:59

      Where can you find the ID?

  4. Donald Allen
    July 27, 2010 at 23:00

    Awesome, thanks David.

    I haven’t tried this yet, but will this require cURL to be enabled? Or will this work with a basic PHP install?

  5. Trikks
    April 15, 2011 at 18:52

    Nice post, however this method is a bit static. You should read about the PHP Tools for SharePoint from Bendsoft. http://blog.bendsoft.com/2011/04/camelot-php-tools-1-1-for-sharepoint-released/

    With that library you are able to send queries like
    $SharePointNonQuery = new SharePointNonQuery(array(
    ‘sql’ => “INSERT INTO contactform (title,email,company,message) VALUES (‘John Doe’,’john.doe@example.com’,’Johns Company’,’A test message!’)”,
    ‘method’ => ‘ExecuteNonQuery’,
    ‘connString’ => ‘sharepoint_connection’,
    ‘sharedKey’ => constant(“WSDL_SHARED_KEY”)
    ));

    So, very dynamic and no need to download the schema or anything :)
    Thanks again.

  6. Yelisa Fernandez
    May 6, 2011 at 22:35

    Hi,
    Nice post, someone knows how created a library in sharepoint from php.

    Tnahks.

  7. September 26, 2011 at 19:31

    GREAT POST THANK YOU! This will lower my research a lot for this subject.

    Trikks:
    I looked at the Bendsoft product, before coming here, however it requires yearly subscription… I personally do not mind paying for code or software, but I do not like having to pay subscriptions for functionality, it is just annoying to think that a couple years down the road Bendsoft or whoever goes out of business and then their code stops working because you can no longer pay to update the subscription… or someone forgets to pay one year because the person who was in charge of that was let go… now your site just stops working randomly… Plus the Joomla! plugin is 3 major upgrades old (only v1.5 and updates were req. for 1.6 and 1.7 updates for it to work properly that they just didn’t do.)

    After I took all of this into consideration I figured it would be better to just write my own interface module for SharePoint rather than working with theirs. Which is mostly just an interpreter that converts SQL queries to the SP list queries syntax… not the hardest of tasks once you can do a SELECT and an UPDATE statically… This guy’s posts basically do the first step for you.

    Besides, for me anyhow, I have a few months before launch and even then this is just a “wishlist” item not really a launch requirement. luckily… :)

  8. December 15, 2011 at 20:49

    Hi, I am not able to create or read list items from sharepoint lists. Can you please help me in this.
    Let me explain the scenario what I am doing. I have a sharepoint site which is live. I nusoap library, create the exact file as above and just editted username and password without domain. and saved wsdl file on my local machine and placed wsdl file in c:/wamp/www/tool/ so the path to wsdl becomes http://localhost/tool/List.wsdl

    and then i updated the name of the list. and then tried to run my php file but it didn’t worked, it showed blank page everytime :(

    Can you please help me.

    Thanks,
    Kamran

  9. Derak
    June 22, 2012 at 19:50

    This guy made a simple abstraction layer for connecting to lists with sharepoint. I found it really useful along with your post.
    https://github.com/thybag/PHP-SharePoint-Lists-API/

  10. Romit
    November 6, 2012 at 08:07

    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) …..I could not understand this. how can i download Lists.samx and convert it to wsdl.

  1. March 3, 2010 at 18:21
  2. March 3, 2010 at 19:01
  3. March 4, 2010 at 08:41
  4. July 23, 2011 at 08:22

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: