Question

I can't seem to find any direction when trying to filter a FileCollection (Picture Library) by a specific value. I am 'trying' to use the Silverlight Client Object Model to access list data in Sharepoint 2010. The code below works but returns a collection of Files that are all the same image/file. I can kind of see why but can't seem to bind the Caml Query to FileCollection.

What would be the best way to Filter this collection. I have thought about LINQ but not that clued up on it.

private void MyMethod()
{
   CamlQuery camlQuery = new CamlQuery();
   camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='VersionIssueID' /><Value Type='Text'>" + _versionIssueID + "</Value></Eq></Where></Query></View>";
   List imageList = Global.GetClientContext().Web.Lists.GetByTitle("Image Library");
   _fileCollection = imageList.RootFolder.Files;
   Global.GetClientContext().Load(_fileCollection);
   Global.GetClientContext().ExecuteQueryAsync(OnQuerySucceededListenerGetImages, onQueryFailed);
}

[Other bits...]

foreach (File li in _fileCollection)
{
   string fileName = _fileCollection[0].Name;
   string fileUrl = Global.GetClientContext().Url + _fileCollection[0].ServerRelativeUrl;

   if ((fileName.EndsWith(".png")) || (fileName.EndsWith(".jpg")))
   {
      Image img = new Image();
      BitmapImage bitMap = new BitmapImage(new Uri(fileUrl, UriKind.Absolute));
      img.Source = bitMap;

      ImagePanel.Children.Add(img);
   }
}

Could I use a LINQ like so:

imageList.RootFolder.Files.Where(...)

Not to sure what my where clause would be or if this would work.

Anyway if anyone could help me out or point me in the right direction, I would be grateful.

Was it helpful?

Solution

Ok, I see a problem in your code snippet - in your foreach loop, you iterate through all pictures returned, but you reference only the first picture (_fileCollection[0]) in every iteration:

foreach (File li in _fileCollection) // <--- you are iterating through whole collection
{
    // but in every iteration,
    string fileName = _fileCollection[0].Name;  // you are referencing just the FIRST picture!!!
    string fileUrl = Global.GetClientContext().Url + _fileCollection[0].ServerRelativeUrl; // same problem here

    // should be
    string fileName = li.Name;  // each time NEW picture!!!
    string fileUrl = Global.GetClientContext().Url + li.ServerRelativeUrl; // same here


    if ((fileName.EndsWith(".png")) || (fileName.EndsWith(".jpg")))
    {
        Image img = new Image();
        BitmapImage bitMap = new BitmapImage(new Uri(fileUrl, UriKind.Absolute));
        img.Source = bitMap;

        ImagePanel.Children.Add(img);
    }
}

But if you need only the first picture, you can use <RowLimit>1</RowLimit> inside <View> tag of your CamlQuery. Or maybe add another condition about filenmae to your CamlQuery...
CamlBuilder is invaluable tool for creating those queries.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top