Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
I think I didn't get what exactly is wrong or what are you trying to do... What is in collection that's returned? And what do you want to be in that collection? –  jumbo Oct 29 '11 at 16:02
    
Thanks for your reply @jumbo, the collection returns (in this instance) 37 images, that is how many images I have in my 'Image Library', thing is all 37 images are the same image. Not so in the library though. I am only trying to return a single image and display it. I am probably going wrong with the collection loop. Though I might need to filter collection with LINQ? Not sure. –  Frikz Nov 1 '11 at 21:59
add comment

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
thanks again. It now makes sense why the same image was showing up more then once. Thank you! With regards to the CamlQuery, the issue I am having is I can't seem to figure out how to bind the query to a FileCollection (_fileCollection). I can bind CamlQuery to a ListCollection easy enough with .GetItems(camlQuery). But, basically I don't know how to filter the FileCollection. That's where I thought LINQ would come in but I have no idea how to go about using LINQ. Or is there some other trick I am missing? –  Frikz Nov 4 '11 at 18:04
    
Well, I think we are getting to the point! :) Even if you need to work with files from the library, you can work with them as ListItems first. So instead of getting imageList.RootFolder.Files, you can get the items in standart way and filter it using CamlQuery. After you retrieve items you need, just access the item.File property on each item. There you will find info you need (url). –  jumbo Nov 7 '11 at 17:21
    
Thanks again for your reply jumbo...I have not forgotten about this just haven't had time to try out your solution. Will get round to it soon though. Cheers –  Frikz Nov 18 '11 at 11:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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