Search code examples
c#azureremote-accessazure-webjobs

Store files from SharePoint Online list to Network Remote server using webjob


I have requirement where I wanted to save images from SharePoint online library to one of my remote servers located in Organisation network. This remote server is not SFTP server. This program should run every day How can I do it?

I have no idea how can I do it. I have tried below code which work on local machine.

 static string  filePath = @"\\remote server\share\temp\";
 public static void SaveImageFromBase64(string uriString, string fileName)
 {
     
     try
     {
         var base64Data = Regex.Match(uriString, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;

         byte[] imageBytes = Convert.FromBase64String(base64Data);
         using (MemoryStream ms = new MemoryStream(imageBytes))
         {
             Image image = Image.FromStream(ms);
             image.Save(filePath + fileName + ".jpg");
         }
     }
     catch (Exception ex)
     {

         Console.WriteLine(ex.ToString());

     }
 }

Solution

  • This remote server is not SFTP server. This program should run every day How can I do it?

    If you are running from Azure App Service (Azure webjobs), needs VNET Integration or Hybrid Connection to access \\remote-server\share\.

    • Use Microsoft Graph API to fetch images from the SharePoint Online document library.

    Grant Sites.Read.All permission in Microsoft Graph API for the WebJob’s identity.

    Hybrid Connection (If RNS is On-Prem Servers)

    • Navigate to App Service < Networking < Hybrid Connections.

    • Configure Hybrid Connection to access \\remote-server\share\temp\.

    enter image description here

    Then, Create a console application for the WebJob.

    using System;
    using System.IO;
    using System.Threading.Tasks;
    using Azure.Identity;
    using Microsoft.Graph;
    
    class Program
    {
        private static readonly string SiteId = "YOUR_SHAREPOINT_SITE_ID";  // Replace with your SharePoint Site ID
        private static readonly string LibraryId = "YOUR_SHAREPOINT_LIBRARY_ID"; // Replace with your SharePoint Library ID
        private static readonly string NetworkPath = @"\\remote-server\share\temp\"; // Replace with your network path
    
        private static GraphServiceClient graphClient;
    
        static async Task Main(string[] args)
        {
            AuthenticateGraphClient();
            await DownloadAndSaveFiles();
        }
    
        // Authenticate using Managed Identity
        private static void AuthenticateGraphClient()
        {
            var credential = new ManagedIdentityCredential(); // Uses WebJob's Managed Identity
            graphClient = new GraphServiceClient(credential);
        }
    
        // Download files from SharePoint and save them to the network share
        private static async Task DownloadAndSaveFiles()
        {
            try
            {
                var files = await graphClient.Sites[SiteId].Drives[LibraryId].Root.Children.Request().GetAsync();
    
                foreach (var file in files)
                {
                    if (file.File != null) // Ensure it's a file (not a folder)
                    {
                        string fileName = file.Name;
                        string filePath = Path.Combine(NetworkPath, fileName);
    
                        Console.WriteLine($"Downloading {fileName}...");
    
                        using (var stream = await graphClient.Sites[SiteId].Drives[LibraryId].Items[file.Id].Content.Request().GetAsync())
                        using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                        {
                            await stream.CopyToAsync(fileStream);
                        }
    
                        Console.WriteLine($"File saved: {filePath}");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
    }
    

    Run this above program daily. build and publish the webjob dotnet publish -c Release -o ./publish.

    Or

    Schedule it using Azure Scheduler or a CRON expression to run daily.