[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1189
  • Last Modified:

Getting list of files in a folder on SharePoint 2010 using Visual Studios 2010 asp.net

Is it possible for me to get a list of files that are in a sub folder on SharePoint without being on the server that SharePoint is on?
0
hscast
Asked:
hscast
  • 10
  • 7
7 Solutions
 
hscastAuthor Commented:
I cant test it until Monday, but that looks like what I was looking for.
0
 
hscastAuthor Commented:
In that example you linked, could you help me figure out what my "rootList" would be.

Below is a link to the folder I need to get the files from.  Customer Specs is the name of the folder

http://portal/sites/lab/Department%20Documents/Forms/AllItems.aspx?RootFolder=%2Fsites%2Flab%2FDepartment%20Documents%2FSpecifications%2FCustomer%20Specs
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
hscastAuthor Commented:
Okay so I'm getting this error.

List 'http://hsportal/sites/metlab/Department%20Documents/Specifications' does not exist at site with URL 'http://hsportal'.

Here's my code, can you see what I have wrong?

 
       ClientContext cc = new ClientContext("http://portal");
        List partsList = cc.Web.Lists.GetByTitle("http://portal/sites/lab/Department%20Documents/Specifications");
        CamlQuery camlQueryPartsList = new CamlQuery();
        camlQueryPartsList.ViewXml =
          @"<View>
        <Query>
        </Query>
      </View>";
        camlQueryPartsList.FolderServerRelativeUrl = "/Customer Specs";
        Microsoft.SharePoint.Client.ListItemCollection listItems = partsList.GetItems(camlQueryPartsList);
        cc.Load(listItems);
        cc.ExecuteQuery();

Open in new window

0
 
Daniel KlineCommented:
My typo on my phone.  Just Portal. Not hPortal.
0
 
hscastAuthor Commented:
oh yeah I had that part right. It's still giving me the error. Do you see anything else wrong?
0
 
hscastAuthor Commented:
I used this code I found online to return a list of all lists from the server

 string siteURL = "http://portal/sites/lab/";
        ClientContext context = new ClientContext(siteURL);

        // The SharePoint web at the URL.
        Web web = context.Web;
        
        // Retrieve all lists from the server. 
        context.Load(web.Lists,
                     lists => lists.Include(list => list.Title, // For each list, retrieve Title and Id. 
                                            list => list.Id));

        // Execute query. 
        context.ExecuteQuery();

        // Enumerate the web.Lists. 
        foreach (List list in web.Lists)
        {
            label1.Text = label1.Text + "<p> " + list.Title;
        } 

Open in new window


and in that list I find "Department Documents" which has a folder that has the folder I need. I get that list when its just the above url, but if I add "Department Documents" to the end there I get this error.

Cannot contact site at the specified URL http://portal/sites/lab/Department Documents. There is no Web named "/sites/lab/Department Documents/_vti_bin/sites.asmx".

Not sure if this will help you help me, but it's as far as I have gotten. Thanks for your help!
0
 
Daniel KlineCommented:
I think I'm seeing your confusion.   Sorry it took so long to get back to you.

Is the root of the site collection http://portal/sites and all the sites are sub sites?
And one of your sites is Lab
And it contains a Document Library named "Department Documents"
Which contains a subfolder named "Customer Specs"

And what you want is a listing of the files in the Customer Specs folder.
I'm trying to guess at the architecture of your farm.

The Sites.asmx services you are trying to reach should be at be at
    http://portal/sites/_vti_bin/sites.asmx
if http://portal/sites points to the root of the  site collection and the web application.

if you paste that url into you browser, it should return a page to the soap service endpoints.  You should see a listing of the listeners.  

Try setting the site url as "http://portal/sites/"  Then use the getsites service to return the lab site.

Then you continue to traverse the hierarchy through the lists to Department Documents and finally get the Customer Specs list which you iterate.

I usually use the rest services (oData) and the JavaScript client object model for these tasks.  I find it much easier.

Use your debugger to view the contents of the object as you are accessing them.  It will help expose the methods to retrieve the next level of the object model.  It's like peeling an onion.
0
 
hscastAuthor Commented:
So there doesn't seem to be a root collection of sites. http://portal is the root and to get to the lab it tacks on /sites/lab, but there is no just http://portal/sites.

file structure in web browser
http://portal   
                  |_   /sites/lab
                                |_   /Department Documents (folder)
                                                                      |_        /Specifications (sub folder)
                                                                                                 |_      /Customer Specs (sub sub folder and where the files i want to get are)


I can get into http://portal/sites/lab and see what is in there, but I cant seem to go any further in.

And I just want to make sure I made this clear. I'm developing on a different server than SharePoint and on an intranet page that will just retrieve the file names from a sub folder. I am also using the sharepoint client object model ddl. Thanks again for all your help.
0
 
Daniel KlineCommented:
Great.  The complexity of what you are trying to do is one of the reasons I avoid foldering unless absolutely necessary.

You say you get to "lab".   Is Sites a sub site and lab a sub site of lab?

I'm stretching here, but can you set the web context to "lab" and then traverse the lists in the sub site as if they were a root site?

You could also resolve the issue with a little re-architecting.  Export the folder contents and import them into the site at the root level.  Use metadata to accomplish what you are trying to do with foldering.   Folders really are evil.

Sorry.  I couldn't find an example of what you are trying to accomplish.
0
 
hscastAuthor Commented:
When I try to access http://portal/sites I get 404 error, but if I can go to  http://portal/sites/lab. It's my understanding that  http://portal/sites/lab is it's own site. So what my problem seems to be I can't access the sub directories of a site. When I do I get that error that I have posted before.
0
 
hscastAuthor Commented:
I think I found a work around. I'm not using the API anymore and just going straight to the directory and grabbing the file names from there.
0
 
Daniel KlineCommented:
Hmmm.  That intrigues me.  The directory is built in the Virtual Path provider.  How are you doing it.
0
 
hscastAuthor Commented:
 string[] filepaths = Directory.GetFiles("\\\\portal\\DavWWRoot\\sites\\lab\\Department Documents\\Specifications\\Customer Specs");
    protected void Page_Load(object sender, EventArgs e)
    {        

        for (int i = 0; i < filepaths.Length; i++)
        {
            int last = filepaths[i].LastIndexOf('\\');
            label1.Text = label1.Text + filepaths[i].Substring(last + 1);
        }
    }

Open in new window

0
 
Daniel KlineCommented:
Ahhh.  Clever.  I didn't think of WeDav.  Nice job.
0
 
hscastAuthor Commented:
I found a work around
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now