PHP App for SharePoint sample
This sample will help you get started building an app for SharePoint that uses PHP.
SharePoint add-in model is considered as a legacy option for extending SharePoint user interface. Please see SharePoint Framework documentation and the SharePoint Framework samples for the future proven option to extend SharePoint Online. Possible backend services should be using Azure Active Directly based registration and related app models.
Table of Contents
The app model in SharePoint lets you build apps without tying you to a particular technology. You can use the language and tools of your preference to work on the components and pages that constitute your app.
We built this sample to get you started in building an app for SharePoint that uses PHP.
This sample is a provider-hosted app for SharePoint that gets an access token by using the OAuth 2.0 client library for PHP. A PHP page uses the token to issue an authenticated request to a REST endpoint in SharePoint.
Simple, but it illustrates the basic concepts that you need to know to start building apps for SharePoint using your favorite technology, PHP.
This sample requires the following:
- Web server with PHP version 5.3 or higher.
- SharePoint site on Office 365. Join the Office 365 Developer Program and get a free 1 year subscription to Office 365.
Configure the sample
You must complete the following tasks to configure the sample:
- Get the dependencies using composer
- Create a web application in your web server
- Register your app in SharePoint
- Update the config.php file
- Update the client_id in the app manifest
- Deploy the app to the SharePoint site
Get the dependencies
The sample uses the OAuth 2.0 client library for PHP. You can use Composer to get the library. Follow this procedure to get the dependencies:
- In a command prompt, go to the folder where you cloned or downloaded this project.
- Go to the PHP folder.
Composer downloads the dependencies required by the project.
Create a web application
The sample is configured to use a web application that resolves to PHPAppWeb. To make it easier to configure your app we recommend that you use PHPAppWeb as your web application name. Follow this procedure to create the web application:
- Create a web application on your web server with a name of PHPAppWeb.
- Map the web application physical path to the PHP folder included in the sample.
Note: The web application must be configured to use SSL.
Register your app in SharePoint
Since you're not installing the app from the Office Store, you must register your app in SharePoint before deploying it. Follow this procedure to register the app:
- Open a browser and go to https://yoursharepointsite/_layouts/15/appregnew.aspx
- Fill the following fields in the page:
- Client Id - Click the Generate button to let SharePoint create a value for you.
- Client Secret - Click the Generate button to let SharePoint create a value for you.
- Title - PHPAppforSharePoint
- App Domain - localhost
- Redirect URI - https://localhost/phpappweb/index.php
- Copy the values and store for future reference. You'll need them to update the configuration and app manifest files.
- Click Create.
Note: If you specify a title other than PHPAppforSharePoint you must update the Name attribute of the App element in the app manifest accordingly.
Update the configuration file
The sample requires the client_id, and client_secret values from the app registration page in the previous task. To update the values in the config.php file, follow this procedure:
Open the config.php file provided in this sample in a text editor.
Copy the client_id, and client_secret values that you got in the previous task to the placeholders in the file.
$client_id = "<your client_id value>"; $client_secret = "<your client_secret value>";
Save the config.php file.
Update the app manifest
The AppTemplate.app file is an app package that contains the app manifest file. You have to update the app manifest file with the client_id value obtained in the Register your app in SharePoint task. Follow this procedure to update the app manifest file:
Go to the root folder of the sample.
Rename the AppPackage.app file provided in this sample to AppPackage.zip
Open the zip file and extract the AppManifest.xml file.
Edit the AppManfest. xml file. Replace with the Client Id value from the app registration page.
<AppPrincipal> <RemoteWebApplication ClientId="<your client_id value>"/> </AppPrincipal>
Replace the AppManifest.xml file in the zip file.
Rename the AppPackage.zip file back to AppPackage.app
Note: The updated AppPackage.app file must keep the same file structure as the original. Moving files or folders can invalidate your package.
Deploy the app to the SharePoint site
You can deploy the app using the app catalog that lets administrators deploy business apps to SharePoint sites in the tenant.
- If you don't have an app catalog site in your tenant, create one. For more information, see Create an App Catalog site collection.
- Add the AppTemplate.app file to the app catalog. For more information, see Add custom apps to the App Catalog site.
- Go to the SharePoint site where you want to deploy the app. From the Settings menu (the gear in the top right corner of the page) choose Add an app.
- Choose the PHP App for SharePoint app. In the consent page, choose Trust It.
Run the sample
To run the sample, simply click on the PHP App for SharePoint app in the SharePoint site contents page. To find the contents page go to your site, click on the Settings menu (the gear in the top right corner of the page), and choose Site contents.
The sample includes the following components that help you configure and test the app in your environment.
This is a PHP class that inherits from the AbstractProvider class. The AbstractProvider class exposes a function to get a token that you can use to issue authorized requests.
The class initializes the values required to get the access token. The constructor requires the client id, client secret, SharePoint site URL, and a context token as parameters. The constructor extracts and formats the following values from the context token:
- Refresh token
- Token service URI
After creating a SharePoint provider object, PHP pages can call the getAccessToken method of the AbstractProvider class to get a token and use it in HTTP requests.
Index.php is an example that shows you how to use the SharePoint provider class. The page does the following:
- Checks that the request is using the POST method. If the request is using GET instead it tells the user to start the app from the SharePoint site contents page.
- Initializes a SharePoint provider instance.
- Gets an access token using the getAccessToken function of the AbstractProvider class.
- Issues an authenticated request to a REST endpoint exposed by the SharePoint site.
- Prints the result to the browser.
The index.php page reads the following configuration values from the config.php file:
You can get these values from the app registration page in SharePoint. For more information, see Update the configuration file.
Example app package
The AppPackage.app file is an app for SharePoint package that you can use to deploy and test your app.
Copyright (c) Microsoft. All rights reserved.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact firstname.lastname@example.org with any additional questions or comments.