Visual Studio 2015 ASP.NET Web Application Framework 4.6.1 Project that needs to add folders to SharePoint Online, then copy files, Excel, Word, to newly created SharePoint folder

john carpentier 1 Reputation point
2021-05-13T18:43:24.697+00:00

I have a web app that creates folders as needed on a network drive. I then copy template files from a server to the created folder. I update these files with information from a database.

I need to update this application and swap the network drive with SharePoint Online. I have no experience with SharePoint, but starting to learn.

Is there a way to update my existing application and have it work with SharePoint Online as described?

Does anyone have some simple VB code examples that solves this or is similar that uses a web application to update a SharePoint folder, files?

Thanks, John

Developer technologies .NET .NET Runtime
Microsoft 365 and Office SharePoint Server Development
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. MichaelHan-MSFT 18,126 Reputation points
    2021-05-14T06:03:06.08+00:00

    Hi @john carpentier ,

    You can use SharePoint lient object model (CSOM) to add folders and copy files in SharePoint Online. You need to install the package Microsoft.SharePointOnline.CSOM in your app: https://www.nuget.org/packages/Microsoft.SharePointOnline.CSOM/

    There are some sample codes written with C# in this official article: https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code . This also works for VB. Hope it helps you.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.

    2 people found this answer helpful.

  2. MichaelHan-MSFT 18,126 Reputation points
    2021-05-24T07:27:24.657+00:00

    Hi @john carpentier ,

    Yes, this is due to the MFA. This way to authenticate sharepoint site is not worked for MFA.

    To solve this, you could install the package SharePointPnPCoreOnline: https://www.nuget.org/packages/SharePointPnPCoreOnline

    Then use the below code:

        Dim authManager = New OfficeDevPnP.Core.AuthenticationManager()  
        Dim clientContext = authManager.GetWebLoginClientContext(siteUrl)  
        Dim web As Web = clientContext.Web  
        clientContext.Load(web)  
    
        clientContext.ExecuteQuery()  
        Console.WriteLine(web.Title)  
    

    Reference: https://www.c-sharpcorner.com/blogs/using-csom-to-connect-to-a-sharepoint-site-with-multi-factor-authentication-enabled


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.

    1 person found this answer helpful.

  3. MichaelHan-MSFT 18,126 Reputation points
    2021-05-28T06:48:20.113+00:00

    Hello, john

    To copy files between folders, you could use the methods of MoveCopyUtil class to copy/move folders and files.

    Below is my demo for you:

        'source file  
        Dim srcUrl As String = "https://michael1.sharepoint.com/sites/test/Shared%20Documents/folder1/Document.docx"  
        'destination file  
        Dim destUrl As String = "https://michael1.sharepoint.com/sites/test/Shared%20Documents/folder2/Document.docx"  
        Dim overwrite As Boolean = True  
        Dim options As MoveCopyOptions = New MoveCopyOptions()  
        options.KeepBoth = True  
        MoveCopyUtil.CopyFile(clientContext, srcUrl, destUrl, overwrite, options)  
        clientContext.ExecuteQuery()  
    

    And try to upload file into sub folder like this:

        Dim filepath As String = "C:\test.docx"  
        Dim fileName As String = "test.docx"  
        Dim web As Web = clientContext.Web  
        Dim folder As Folder = web.GetFolderByServerRelativeUrl("/sites/test/Shared Documents/folder2")  
        Dim fileCreateInfo As FileCreationInformation = New FileCreationInformation()  
        fileCreateInfo.Overwrite = True  
        fileCreateInfo.Url = fileName  
        fileCreateInfo.Content = System.IO.File.ReadAllBytes(filepath)  
        Dim uploadFile As File = folder.Files.Add(fileCreateInfo)  
    
    
        clientContext.Load(uploadFile)  
        clientContext.ExecuteQuery()  
    
    1 person found this answer helpful.

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.