Upload session, bad request error in dynamics 365 F&O x++

Hy everyOne,i use this code x++ to get the uploadsessionurl ,but always he give me a bad request from sharepoint ,and all parameters are correct :

var graphApiUrl = “https://graph.microsoft.com/v1.0/sites/{siteid}/drives/{driveid}/root:/Folder1/Folder2/kk.txt:/createUploadSession”;

    System.Net.HttpWebRequest request;
    System.Net.HttpWebResponse response;
    System.Exception ex;
    str responseText;
   
    System.IO.StreamReader     streamReader =null;
    System.IO.Stream        requestStream =null, responseStream=null;
    System.Net.WebHeaderCollection httpHeader;
    str uploadUrl;

    
    try
    {
        new InteropPermission(InteropKind::ClrInterop).assert();
        httpHeader = new System.Net.WebHeaderCollection();

        
        request = System.Net.WebRequest::Create(graphApiUrl);
       
       
        str requestBody = "{\"item\":{\"@microsoft.graph.conflictBehavior\":\"replace\",\"name\":\"kk.txt\"},\"deferCommit\":true}";
        System.Byte[] requestBodyBytes = System.Text.Encoding::UTF8.GetBytes(requestBody);
        request.set_KeepAlive(true);
        request.set_ContentType("*/*");
        httpHeader.Add("Authorization", "Bearer " + accessToken);
        request.set_ContentType("*/*");
        request.set_ContentLength(requestBodyBytes.get_Length());
        request.set_Method("POST");
        request.set_Headers(httpHeader);
        requestStream = request.GetRequestStream();
        requestStream.Write(requestBodyBytes, 0, requestBodyBytes.get_Length());
        requestStream.Close();

        // Send the request and get the response
        response = request.GetResponse();
        responseStream = response.GetResponseStream();
        streamReader = new System.IO.StreamReader(responseStream);
        str responseXml = streamReader.ReadToEnd();
        streamReader.Close(); // Close stream reader after reading
        responseStream.Close();
        info(responseXml);

        
      
        Newtonsoft.Json.Linq.JObject jsonResponse = Newtonsoft.Json.Linq.JObject::Parse(responseXml);
        uploadUrl = jsonResponse.GetValue("uploadUrl").ToString();

        Info(strFmt("Upload URL: %1", uploadUrl));
        return uploadUrl;

If someone can help me in that i will be appreciated(by the way i don’t have the itemid or parentid, or other parameters , i just have the driveid and siteid )

2 Likes

I’m assuming that you’re getting a WebException raised by GetResponse(). If so, can’t you get more details from its Response property?

3 Likes

i success to get the uploadSession to sharepoint , and the prb was in content-type where i was need to pass the value : “application/json”, but if you can told me how can i use this uploadsession to upload file to sharepoint using x++, i tried to use that, but this System.Byte[chunkSize]; is not supported in x++, i found this in c# :

public str uploadFileUsingSession(str accessToken, str filePath, str fileName)
{
Class1 data = new Class1 ();
str uploadUrl;
System.Net.HttpWebRequest request;
System.Net.HttpWebResponse response;
System.IO.Stream requestStream, fileStream;
System.Byte buffer;
int bytesRead;
int chunkSize = 320 * 1024; // 320 KB
int startOffset = 0;
int endOffset;
str uploadSessionUrl;
System.Exception ex;

try
{
    uploadSessionUrl = data.createUploadSession(accessToken, fileName);
    if (uploadSessionUrl == "")
    {
        error("Failed to create upload session.");
        return "failed to create upload session";
    }

    fileStream = new System.IO.FileStream(filePath, System.IO.FileMode::Open);
  
    buffer = new System.Byte[chunkSize];
    
    while ((bytesRead = fileStream.Read(buffer, 0, buffer.get_Length())) > 0)
    {
        endOffset = startOffset + bytesRead - 1;
        request = System.Net.WebRequest::Create(uploadSessionUrl);
        request.set_Method("PUT");
        request.set_ContentLength(bytesRead);
        request.Headers.Add("Content-Range", strFmt("bytes %1-%2/*", startOffset, endOffset));
    
        requestStream = request.GetRequestStream();
        requestStream.Write(buffer, 0, bytesRead);
        requestStream.Close();
    
        response = request.GetResponse();
        response.GetResponseStream().Close();
        response.Close();

        startOffset = endOffset + 1;
    }

    fileStream.Close();
    Info("File upload completed.");

    return "file upload completed";
}
catch (Exception::CLRError)
{
    ex = CLRInterop::getLastException().GetBaseException();
    error(ex.get_Message());
    return "error in uploadfile";
}

}

Use new System.Byte[chunkSize]() to create the array.