javaspring-bootsharepointsap-cloud-foundry

Use SharePoint as a kind of CMS using Spring Boot


I am using Spring Boot for storing files (of basically as many format as possible - but think along the lines of images, documents) in SharePoint for my application, which is deployed in SAP's Cloud Foundry. I have pretty much followed the steps mentioned in this post's answer (and copied all four files from there) and I got a github repo which pretty much implements the same thing. I've followed them but something is not working for me. (Please refer to the StackOverflow link for further reference of what I'm talking about)

The order of method calls in SharePointServiceCached.java is parseExecutionDateTime() -> receiveSecurityToken() -> getSignInCookies(securityToken) -> getFormDigestValue(cookies) I receive the security token (as I checked from my logs) but this is the error message I'm getting:

Document upload failed!org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: [403 FORBIDDEN]

Things I have tried for this:

  1. Added SSL part (see code at end of lists) to the request which only seemed to made it worse.
  2. Added user agent to headers which made no difference.
  3. Changed the endpoint domain from https://protected1.sharepoint.com/sites/protected2 to https://protected1.sharepoint.com/ which again, somehow made it worse. (Obviously, protected1 and protected2 are changed names)
  4. Made myself (the person whose credentials I'm passing) the owner of the site with all privileges.
  5. I checked the logs, the credentials are getting passed correctly.
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) 
    {
        CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier())
                .build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        RestTemplate rest = new RestTemplate(requestFactory);
        builder.configure(rest);
        return builder.build();
    }

Also, previously I was using a CMS for the same thing which returned a document ID associated with the uploaded file. I have enabled the option for document IDs in SharePoint and want to use it similarly, i.e. upload a file -> get a document ID and store it -> use that document ID to fetch the file when required. How can I do this?

Thank you in advance.


Solution

  • Got it. I just had to send the path where I want to store my files. https://protected1.sharepoint.com/sites/protected2/_api/web/GetFolderByServerRelativeUrl(%27Shared%20Documents/Another%20Folder%27)/files/add(url=%27" + documentID + "%27,overwrite=false)

    I am now generating the documentID randomly.