3

I have created a web interface to allow external users to upload files to our sharepoint library using ajax and the phpSPO library. It all works really well, however, I am looking to optimise the upload function alot more.

Currently the all the selected files are sent together in one request to PHP then uploaded one at a time into sharepoint.

I am looking to upload multiple files concurrently.

I know how to set up the client side with ajax and the deferred/promise objects and send individual requests to the server for each file.

The bit I dont know, or even if the phpSPO library can handle it, is to Authenticate at the beginning of the process then use the token with subsequent requests to upload the files into sharepoint.

Does anyone have/know of any code snippets that show how to use the token with a phpSPO request?

EDIT:

Here is my class that initiates the connection to sharepoint:

class SPOInstance
{
    public $authCtx;
    public $ctx;
    public $clientfolder;
    public $log;

    function __construct()
    {
        try {
            $this->authCtx = new AuthenticationContext(app()->webUrl);
            $this->authCtx->acquireTokenForUser(app()->username, app()->password); //authenticate
            $this->ctx = new ClientContext(app()->webUrl, $this->authCtx);
            $token = $this->authCtx->getAccessToken();
            file_put_contents(__DIR__ .'/accesstoken.txt', $token."\r\n",  FILE_APPEND);
        }
        catch(Exception $e) {
            echo 'Authentication failed: ', $e->getMessage(), "\n";
            p2($e->getTrace());

        }
    }

The $token variable is empty.

So once I have worked out how to get the token, how do I use that to recreate the ClientContext object for subsequent requests?

Cheers

Greg J

2

If you do wish to do this purely client side using Ajax. The following snippet would assist. You simply need to get he token and create an Authorization Header of Bearer type and add the token. I forget whether the token (as returned by phpSPO ) is already Base64 Encoded. Please refer to the beforeSend Callback.

 $.ajax({
  url: "http://localhost:33460/api/Account/userinfo",
  dataType: 'json',
  data: {
    foo: 'bar'
  },
  success: function(data, status) {
    return console.log("The returned data", data);
  },
  beforeSend: function(xhr, settings) { xhr.setRequestHeader('Authorization','Bearer ' + tokenString ); } //set tokenString before send
});

I would consider performing the subsequent request server side from phpSPO using either ClientContext or ClientRequest objects. Below is example PHP code that will establish an authenticated context.

$authCtx = new AuthenticationContext($Url);
$authCtx->acquireTokenForUser($UserName,$Password); //authenticate

$ctx = new ClientContext($Url,$authCtx); //initialize REST client  
  • The connection to sharepoint is between the phpSPO library on the server and sharepoint. So I would be looking at how to use the token for subsequent requests in the phpSPO library, not the client. – Greg J Jul 11 '18 at 5:46
  • i see my post was a misleading with the last sentence, I have updated it – Greg J Jul 11 '18 at 5:54
  • ah ok, so with both those objects, the token should persist between calls. Is that not the behavior your encountering? – James Randal Jul 11 '18 at 6:18
  • i tried to get the access token but it appears to be empty. Then even still, im not sure how to create the ClientContext object using the token. – Greg J Jul 11 '18 at 6:51
  • 1
    You shouldn't have to interact with the token itself. Please see updated answer (and mark if it helps thanks) – James Randal Jul 11 '18 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.