Posts Tagged ‘lists’

Read and write to SharePoint lists using PHP

Its been a while since I last blogged so I thought I may as well post an update. The main thing I plan to present is my, hopefully useful, PHP SharePoint API Class.

The aim of the class is essentially to make interacting with the SharePoint Lists API a whole lot more straight forward and allow PHP developers to talk with the Lists API without having to get there hands dirty with SOAP.

As with most stuff I create, its free under MIT licence and is being maintained on my Github account. At current the tool is fairly basic, although if I end up working on a SharePoint based project again I may take the opportunity to expand it further. If anyone spots any bugs or wants to add some features feel free to send em my way or just fork the github repo. Currently the SharePointAPI code is only capable of interacting with an existing SharePoint list and does not support creating new ones programmatically at this time.

You can grab the full source from: https://github.com/thybag/PHP-SharePoint-Lists-API

Setting up the PHP SharePointAPI Object

Using the SharePointAPI to read a SharePoint list is pretty straight forward. The first step is to create a new instance of the SharePointAPI object which requires a username and password (which it will authenticate with in order to interact with the lists) as well as the path to a local copy of the SharePoint Lists WSDL file.

You can normally grab the WSDL from: sharepoint.url/subsite/_vti_bin/Lists.asmx?WSDL

$sp = new SharePointAPI('<username>','<password>','<path_to_WSDL>');

Reading from a Sharepoint List

Once the SharePointAPI Object has been created, you can read from a list simply by calling:

$sp->read('<list_name>');

More advanced filtering is also possible, for example if you wanted to filter on surname = Smith and only return 5 results you would call:

$sp->read('<list_name>', 5, array('surname'=>'Smith'));

You can filter on multiple parameters simply by adding extra values to the filter array. (currently only AND ing is possible)

All results are returned as associative arrays, calling print_r on the results of a $sp->read(‘<list>’); is probably the best way to understand exactly what data is being returned.

Writing to a SharePoint List

Writing to a SharePoint list (or inserting a new list item) is equally straight forward. The data to be inserted is simply provided as an associative array to the function. For example to add someone called bob to a list you may use:

$sp->write('<list_name>', array('forename'=>'Bob','surname'=>'Brown','pet' =>'cat'));

Editing a SharePoint List Item

To edit an existing SharePoint item the syntax is fairly similar to the above examples. The primary difference being that the ID of the record you wish to edit is also required. Assuming the ID for Bob’s record was 20, you could change his pet to dog with the following code:

$sp->update('<list_name>','20', array('pet'=>'dog'));

Deleting a SharePoint List Item

Deleting a SharePoint list item also requires the ID of the record you wish to remove.

$sp->delete('<list_name>','<ID>');

 

Thanks for reading 🙂

P.S.  I’m now only one week away from starting my new job on the Kent Web Development team. Woo 😀