Results 1 to 17 of 17

Thread: [RESOLVED] Get a list of files using SharePoint.Client

  1. #1

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Resolved [RESOLVED] Get a list of files using SharePoint.Client

    I uploaded thousands of files for a client but some were not allowed and skipped. I want to determine those files now. I was hoping to write a simple app to get a list of all the files on the SharePoint site. However so far all the examples I've found online have been server side and I need to do it using Microsoft.SharePoint.Client in a WinForm program. Can anyone point me to a quick and dirty example online or maybe have a quick one to post here?

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Get a list of files using SharePoint.Client

    Are you looking for all the files in a specific SharePoint Library or across the entire site?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: Get a list of files using SharePoint.Client

    A specific library.

  4. #4

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: Get a list of files using SharePoint.Client

    I found a PowerShell script but it needs a SP plugin that I can't seem to get to install.

  5. #5

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: Get a list of files using SharePoint.Client

    I discovered I can map a network drive to a SP library. I'll use my SizeExplorer Pro program to generate a list from there. Thanks.

  6. #6
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    Here you go, try this:

    Code:
        Public Function GetFiles(folderUri As Uri) As List(Of String)
            Dim out As New List(Of String)
    
            Dim server As String = folderUri.AbsoluteUri.Replace(folderUri.AbsolutePath, "")
            Dim serverrelative As String = folderUri.AbsolutePath
    
            Dim clientContext As ClientContext = New ClientContext(server)
            Dim web As Web = clientContext.Web
            Dim folder As Folder = web.GetFolderByServerRelativeUrl(serverrelative)
    
            clientContext.Load(folder, Function(f2) f2.Files)
            clientContext.ExecuteQuery()
    
            For Each f As Microsoft.SharePoint.Client.File In folder.Files
                out.Add(f.Name)
            Next
    
            Return out
        End Function
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  7. #7

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    I think it will work but I'm getting an exception on the ExecuteQuery. 403, forbidden. So I'm thinking we need to add some credentials in here someplace?

  8. #8

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    For the URI I used "https://[MyCompanyName].sharepoint.com/[SiteName]/"

  9. #9
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    Add this line in there right after Dim clientContext....
    Code:
     clientContext.Credentials = New Net.NetworkCredential("user", "password", "domain")
    Of course, replace the user, password and domain strings with your information, or more securely, the information to a service account you can use to interact with the SharePoint site.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  10. #10

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    Looks good but I don't know what the domain value should be. I tried to leave it blank and "MicrosoftAccount" but I'm still getting 403.

  11. #11
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    I don't know. We run Sharepoint on a local server, we don't go through sharepoint.com. Typically, it's whatever the domain your computer is attached to on a typical, Windows Server network. Try what you use for [MyCompanyName].

    Try using the URL of the actual library for the function, like: https://MyCompanyName.sharepoint.com/SiteName/LibraryName
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  12. #12

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    Still 403. I was working with one piece of code before that had this and I couldn't find an example anywhere online describing what I should put here. I found a utility called SPFileZilla that has a field for it but i just leave it blank.

  13. #13

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    In SPFileZilla I have fields that mimic what we have here and I believe he's using the same library. So I entered a known good set from my client and it's still 403. But in the SPFileZilla there's a checkbox labeled "Is SharePoint Online". I wonder, is there another parameter I should be setting for SP on the web. I'll look into the ClientContext and see if I can find something like that.

  14. #14

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    I found to be out on the Internet it needs to be SharePointOnlineCredentials. But it doesn't take a string for a password. I need to figure out how to use "Secure String".

  15. #15

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    I worked out the credentials. Must use SharePointOnlineCredentials. Odd fellow. It seems one has to load it one character at a time. Like this:
    vb.net Code:
    1. Dim strPassword As String = "MyPassword"
    2. For Each chrTemp As Char In strPassword
    3.     ssPassword.AppendChar(chrTemp)
    4. Next
    5. 'Then adding the password is...
    6. clientContext.Credentials = New SharePointOnlineCredentials("[email protected]", ssPassword)
    Now there are no exceptions but the list of string is empty. I'll work on that more in a bit.

  16. #16
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    Ahh gotcha. Well, if you're not typing things in, you can use this:
    Code:
        Public Function StringToSecure(str As String) As System.Security.SecureString
            Dim c() As Char = str.ToCharArray()
            Dim sec As New System.Security.SecureString
            For Each letter As Char In c
                sec.AppendChar(letter)
            Next
            Return sec
        End Function
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  17. #17

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,145

    Re: [RESOLVED] Get a list of files using SharePoint.Client

    I think that's basically what I did. Maybe our messages passed in the ether.

    Any ideas on the zero list items? What about making it recursive? IE sub-directories.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width