1. Home
  2. Data
  3. File Storage Services
  4. Storage – SharePoint Implementation

Storage – SharePoint Implementation

SharePoint Implementation supports the SharePoint storage type which uses the Microsoft Graph API. To setup a connection to this service you need a Site Name, Drive Name, Folder Name, Client ID, Client Secret, and Tenant ID from Microsoft on the Azure portal.

Implementation

Save

public static async Task SaveBLOB(StorageInformation storageInformation, IDatabase db)
{
    try
    {
        Tuple<GraphServiceClient, Site, Drive, DriveItem> sharePointStorage = await GetSharePointDrive(storageInformation, db);

        if (storageInformation.ErrorMessage != "")
        {
            return;
        }
        else if (sharePointStorage.Item1 == null ||
                    sharePointStorage.Item2 == null ||
                    sharePointStorage.Item3 == null ||
                    sharePointStorage.Item4 == null)
        {
            storageInformation.ErrorMessage = "SharePointStorage.SaveBLOB - Unable to obtain SharePoint configuration values: MSGraphClientId, MSGraphClientSecret & MSGraphTenantId";
            return;
        }

        GraphServiceClient graphServiceClient = sharePointStorage.Item1;
        Site site = sharePointStorage.Item2;
        Drive drive = sharePointStorage.Item3;
        DriveItem folder = sharePointStorage.Item4;

        // Prepare to upload file

        // Use properties to specify the conflict behavior
        // in this case, replace the file if it already exists
        DriveItemUploadableProperties uploadProps = new()
        {
            ODataType = null,
            AdditionalData = new Dictionary<string, object>
            {
                { "@microsoft.graph.conflictBehavior", "replace" }
            }
        };

        // Create the upload session
        UploadSession uploadSession = await graphServiceClient.Sites[site.Id].Drives[drive.Id].Items[folder.Id]
                                                                                                .ItemWithPath(storageInformation.FileName + "." + storageInformation.FileType)
                                                                                                .CreateUploadSession(uploadProps)
                                                                                                .Request()
                                                                                                .PostAsync();

        // Max slice size must be a multiple of 320 KiB
        // 320 * 1024 is the value that Microsoft provided as an example
        int maxSliceSize = 320 * 1024;

        // Set file's memory stream position index to 0
        storageInformation.MemoryStream.Position = 0;

        // Create an upload task of a drive item object
        LargeFileUploadTask<DriveItem> fileUploadTask = new(uploadSession, storageInformation.MemoryStream, maxSliceSize);

        // Upload the file
        UploadResult<DriveItem> uploadResult = await fileUploadTask.UploadAsync();

        if (!uploadResult.UploadSucceeded)
        {
            storageInformation.ErrorMessage = $"SharePointStorage.SaveBLOB - Unable to upload file";
        }
    }
    catch (Exception)
    {
        storageInformation.ErrorMessage = "SharePointStorage.SaveBLOB - Unknown error";
    }
}

Read

public static async Task ReadBLOB(StorageInformation storageInformation, IDatabase db)
{
    try
    {
        Tuple<GraphServiceClient, Site, Drive, DriveItem> sharePointStorage = await GetSharePointDrive(storageInformation, db);

        if (storageInformation.ErrorMessage != "")
        {
            return;
        }
        else if (sharePointStorage.Item1 == null ||
                    sharePointStorage.Item2 == null ||
                    sharePointStorage.Item3 == null ||
                    sharePointStorage.Item4 == null)
        {
            storageInformation.ErrorMessage = "SharePointStorage.ReadBLOB - Unable to obtain SharePoint configuration values: MSGraphClientId, MSGraphClientSecret & MSGraphTenantId";
            return;
        }

        GraphServiceClient graphServiceClient = sharePointStorage.Item1;
        Site site = sharePointStorage.Item2;
        Drive drive = sharePointStorage.Item3;
        DriveItem folder = sharePointStorage.Item4;

        Stream stream = await graphServiceClient.Sites[site.Id].Drives[drive.Id].Items[folder.Id]
                                                                                .ItemWithPath(storageInformation.FileName + "." + storageInformation.FileType)
                                                                                .Content
                                                                                .Request()
                                                                                .GetAsync();

        stream.CopyTo(storageInformation.MemoryStream);
    }
    catch (ServiceException ex) when (ex.Error.Code == "itemNotFound")
    {
        storageInformation.ErrorMessage = $"SharePointStorage.ReadBLOB - The file name \"{storageInformation.FileName + "." + storageInformation.FileType}\" does not exist";
    }
    catch (Exception)
    {
        storageInformation.ErrorMessage = "SharePointStorage.ReadBLOB - Unknown error";
    }
}
Updated on February 2, 2023

Was this article helpful?

Related Articles

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help!
Contact Support

Leave a Comment