1

I need to upload a file to a list of a custom site in SharePoint. I have used graph Rest API according to the following URL.

https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http

But I'm getting following error when I'm uploading the file.

HTTP response:  {
  "error": {
    "code": "BadRequest",
    "message": "Resource not found for the segment 'FileB.txt'.",
    "innerError": {
      "request-id": "30ccddb7-17b2-4b60-9943-39ce65eb301f",
      "date": "2020-05-18T13:57:36"
    }
  }
}

This is my sample java code.

public void uploadFilesToList() throws MalformedURLException, IOException{

    String tenantId = "48e2d3d3-5602-4db8-b9e4-xxxxxxxxxxxx";
    String clientId = "f08b652a-6e3c-448f-b98c-xxxxxxxxxxxx";
    String clientSecret = "KHII03L.77f6IP2zCm~v~hUXD.a.xxxxx";
    clientSecret=java.net.URLEncoder.encode(clientSecret,"UTF-8");
    String access_token = getaccetAuth(tenantId, clientId, clientSecret);

    String siteId = "xxxxxxxx.sharepoint.com,ce2ae416-288b-41d4-8d58-af491571867a,976efac4-37b3-4515-858a-cdfddc936a39";
    String listId = "8c47c9c5-88cb-4ad9-a94a-57b6dceec2a4";
    String formattedUrl = String.format("https://graph.microsoft.com/v1.0/sites/%s/lists/%s/%s/content",
            siteId,listId,"FileB.txt");

    HttpPut put = null;
    try {

        File file = new File(("C:\\Users\\user_name\\Desktop"+"\\FileB.txt"));
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("file", new FileBody(file));

        HttpClient client = HttpClientBuilder.create().build();
        put = new HttpPut(formattedUrl);
        // add header
        put.setHeader("Content-Type", "text/plain");
        put.setHeader("Authorization", "Bearer " + access_token);
        put.setEntity(entity);

        HttpResponse response = client.execute(put);
        System.out.println("Response Code : "
                + response.getStatusLine().getStatusCode());
        System.out.println("HTTP response:  "+response.toString());
        String json = EntityUtils.toString(response.getEntity());
        System.out.println("HTTP response:  "+json);

    } finally {
        put.releaseConnection();
    }
}

this is my get auth_token method.

 public String getaccetAuth(String tnID, String clId, String cliSec) throws IOException {
    String endpoint = String.format("https://login.microsoftonline.com/%s/oauth2/token",tnID);
    String postBody = String.format("grant_type=password&client_id=%s&client_secret=%s&resource=%s&userName=%s&passWord=%s&scope=%s",
            clId, cliSec, "https://graph.microsoft.com","admin@xxxxxx.onmicrosoft.com","xxxxxxx","https://graph.microsoft.com/.default");
    HttpsURLConnection conn = (HttpsURLConnection) new URL(endpoint).openConnection();
    conn.setRequestMethod("POST");
    conn.addRequestProperty("Content_Type","application/x-www-form-urlencoded");
    conn.setDoOutput(true);
    conn.getOutputStream().write(postBody.getBytes());
    conn.connect();
    JsonFactory factory = new JsonFactory();
    JsonParser parser = factory.createParser(conn.getInputStream());
    String accessToken = null;
    while (parser.nextToken() != JsonToken.END_OBJECT) {
        String name = parser.getCurrentName();
        if ("access_token".equals(name)) {
            parser.nextToken();
            accessToken = parser.getText();
        }
    }
    return accessToken;
}

I'm not sure about what went wrong here. Could anyone find a way to get this right? Note: I have used hard coded values here.

Nishant
  • 623
  • 4
  • 10
C.Ellawala
  • 51
  • 1
  • 2
  • 9

1 Answers1

0

Based on the document, I didn't find such api

https://graph.microsoft.com/v1.0/sites/%s/lists/%s/%s/content

I tried with

https://graph.microsoft.com/v1.0/sites/%s/drive/items/%s:/FileB.txt:/content

and it worked fine.

enter image description here

Tony Ju
  • 14,891
  • 3
  • 17
  • 31
  • can i know what is exact id you have used after "drive/itmes"? – C.Ellawala May 19 '20 at 10:32
  • 1
    @C.Ellawala You can find the id by using 'items/root/children' – Tony Ju May 19 '20 at 10:44
  • can i know how to get the parent id of the custom list i have created. because that's the mistake i have done in this. – C.Ellawala May 19 '20 at 12:37
  • @C.Ellawala If you access sites/siteId/lists, then the parentReference of the list is siteId. You can find it in the response. – Tony Ju May 19 '20 at 12:52
  • I know how to get the siteId. What i need to know is that the id which you have entered after the "drive/items". It should be a parent id of a list created. We are already providing the siteId in the endpoint. Can i know how you get the other id, you are passing after "drive/item". – C.Ellawala May 19 '20 at 14:19