Search code examples
c#sharepointsharepoint-2010

How to programatically summarize existing documents in Sharepoint


eveyone.

I don't know exactly if it's a possible action, but it would help me a lot.

Here's the scenario: we have a "teamplace" made in Sharepoint, where we have a lot of technical documents in different folders. The problem is that we lost the control of it, and now we need to create a summary of the existing files. So, hoping that we don't need to go in each folder and subfolder and write down each file name, I was wondering if it's possible to do this programatically in C#.

In short terms, is there a way to connect in Sharepoint (I could use my network account, that has access granted for it) via C# application and then retrieve the list of the existing files, or at least get their paths? If so, is it too complex?

Thank you in advance.


Solution

  • Thanks for the answer, Isaac an ProjectedJetpacks, but I could figure it out in another way, though. I've used the Microsoft.Sharepoint.Client.dll assembly (you need to install the Sharepoint Client SDK) and went through all the lists inside the website. Then, inside each one, I searched for the folders (and used a recursive method to go until the last leaf of the folder tree) and for the files in each one. For last, put all the files information I needed inside a CSV file. Some examples of the work can be seen below:

    ClientContext context = new ClientContext("<websiteurl>");
    Web web = context.Web;
    context.Load(web);
    context.ExecuteQuery();
    var q = from list in web.Lists
                    where <put condition here>
                    select list;
    var r = context.LoadQuery(q);
    context.ExecuteQuery();
    
    foreach (var list in r)
    {
       context.Load(list.RootFolder);
       context.ExecuteQuery();
       ...
       //To get folders
       GetFolders(context, list.Root.Folders);
       //To get files
       GetFiles(context, list.RootFolder.Files);
     }
    

    The important thing is remember to put each unloaded object inside "context.Load()" and right after call "context.ExecuteQuery()".

    I hope this can be helpful for someone else.

    Best regards.