Copy pdf file from Salesforce case to SharePoint Online Document Library

gokulnath palani 106 Reputation points
2024-04-08T14:06:12.9333333+00:00

Hi all,

Good day. I need to copy the pdf file from Salesforce case to SharePoint Online Document Library. We have setup a service account to access the SharePoint Online. We are planning to use basic authentication and create a pdf file into SPO library using Graph API. Can anyone help me for authentication and create a pdf file into SPO using Graph API ?

Thank-you,

Gokul.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,041 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,713 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 35,556 Reputation points Microsoft Vendor
    2024-04-09T06:28:29.93+00:00

    Hi @gokulnath palani,

    This Microsoft Learn module demonstrates how to upload both small and large files to SharePoint Online using Microsoft Graph.

    Access Files with Microsoft Graph

    The code to upload a small file is relatively simple.

    var fileName = "smallfile.txt";  
    var filePath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), fileName);  
    Console.WriteLine("Uploading file: " + fileName);  
      
    FileStream fileStream = new FileStream(filePath, FileMode.Open);  
    var uploadedFile = client.Me.Drive.Root  
                                  .ItemWithPath("smallfile.txt")  
                                  .Content  
                                  .Request()  
                                  .PutAsync<DriveItem>(fileStream)  
                                  .Result;  
    Console.WriteLine("File uploaded to: " + uploadedFile.WebUrl);  
    

    The code to upload a larger file requires chunking.

    var fileName = "largefile.zip";  
    var filePath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), fileName);  
    Console.WriteLine("Uploading file: " + fileName);  
      
    // load resource as a stream  
    using (Stream stream = new FileStream(filePath, FileMode.Open))  
    {  
        var uploadSession = client.Me.Drive.Root  
                                        .ItemWithPath(fileName)  
                                        .CreateUploadSession()  
                                        .Request()  
                                        .PostAsync()  
                                        .Result;  
      
        // create upload task  
        var maxChunkSize = 320 * 1024;  
        var largeUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession, stream, maxChunkSize);  
      
        // create progress implementation  
        IProgress<long> uploadProgress = new Progress<long>(uploadBytes =>  
        {  
            Console.WriteLine($"Uploaded {uploadBytes} bytes of {stream.Length} bytes");  
        });  
      
        // upload file  
        UploadResult<DriveItem> uploadResult = largeUploadTask.UploadAsync(uploadProgress).Result;  
        if (uploadResult.UploadSucceeded)  
        {  
            Console.WriteLine("File uploaded to user's OneDrive root folder.");  
        }  
    }  
    
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. gokulnath palani 106 Reputation points
    2024-04-10T07:36:51.2466667+00:00

    Hi @RaytheonXie_MSFT ,

    Thank you for your response. I am not looking for PowerShell script. I am looking for Graph API like below '**https://graph.microsoft.com/v1.0/drives/{{DriveID}}/root:/BC.png:/content.**' Here how to pass the file content in request body ?

    Thank-you,


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.