7

I am trying to select all PowerPoint files in a list. How do I do that? Up until now I used this:

class UsedIn
{
private static NetworkCredential credential = new NetworkCredential("login", "pwd", "domain");

static String filename = "Engineering_Weekly_Report.pptx";

static void Main(string[] args)
{
    try
    {
        // Creating the Clientcontext
        ClientContext clientContext = new ClientContext("https://sharepoint.adress.com/subfolder/");
        clientContext.Credentials = credential;
        Web oWebsite = clientContext.Web;

        // Collect all lists from Sharepoint.
        ListCollection collList = oWebsite.Lists;
        clientContext.Load(collList);
        clientContext.ExecuteQuery();
        Console.WriteLine("Connected to Sharepoint.");

        // Getting the Presentation.
        Console.WriteLine("Working on " + filename + "..");
        Guid id = new Guid("CC37C3F4-97D0-4181-87A3-6143DD45004F"); // List ID for the Slide Library.
        List sharedDocumentsList = clientContext.Web.Lists.GetById(id);
        CamlQuery camlQuery = new CamlQuery();

        camlQuery.ViewXml = "<View Scope = 'RecursiveAll'></View>";
        ListItemCollection listItems = sharedDocumentsList.GetItems(camlQuery);
        clientContext.Credentials = credential;
        clientContext.Load(sharedDocumentsList);
        clientContext.ExecuteQuery();

        clientContext.Credentials = credential;
        clientContext.Load(listItems);
        clientContext.ExecuteQuery();
        Console.WriteLine("Opened Document.");

        foreach (var item in listItems)
        {
            clientContext.Credentials = credential;
            FileInformation fileInformation = ClientOM.File.OpenBinaryDirect(clientContext, (string)item["FileRef"]);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                PresentationDocument ppt = PresentationDocument.Open(memoryStream, true);
                Console.WriteLine("Found the Document.");
            }
        }
        Console.WriteLine("Done.");
        Console.Read();
    }

    catch (Exception ex) when (ex is ServerException || ex is InvalidCastException || ex is OpenXmlPackageException || ex is WebException)
    {

    }
}

But now I have to work with a list that is not clean and has some pdfs in it. Is there a way to only select .pptx with CAML?

2 Answers 2

2

Try:

<Where><Eq><FieldRef Name='FileExtension'><Value Type='text'>pptx</Value></FieldRef></Eq></Where>
2

You may use DocIcon to determine the type of file in your CAML Query

<Eq><FieldRef Name="DocIcon"/><Value Type="Computed">pptx</Value></Eq>

Source: Filter file types from list to NOT show certain file types

3
  • 1
    I'm not sure I'd rely on the DocIcon value for this since the FileExtension is more direct and intent is readable.
    – iOnline247
    Aug 11, 2016 at 13:39
  • 1
    Agree, but just another way, if needed. Aug 11, 2016 at 13:48
  • FileExtension is not working for me in SP 2016 while DocIcon does work. Jul 10, 2019 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.