none
Get FileRef for filename CSOM RRS feed

  • Question

  • I am using the following code to try to retrieve the FileRef field for a file name so I can open the file

    This however always returns the first file in the document library. Is this CAML query incorrect?

    Tuesday, March 15, 2016 7:02 PM

Answers

  • Hi,

    The code below work for me, you can take it for a try:

    public static void getItemByFileName()
    { 
       var fileName = "temp.txt";
       //var fileName = "test.avi";
    
       ClientContext clientContext = new ClientContext("http://sps2013"); 
       Microsoft.SharePoint.Client.List spList = clientContext.Web.Lists.GetByTitle("LB0309"); 
       clientContext.Load(spList); 
       clientContext.ExecuteQuery(); 
    
       if (spList != null && spList.ItemCount > 0) 
       {
          Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery(); 
          camlQuery.ViewXml = 
             @"<View>  
                   <Query> 
                      <Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>"+fileName+@"</Value></Eq></Where> 
                   </Query> 
                    <ViewFields><FieldRef Name='FileRef' /><FieldRef Name='FileLeafRef' /></ViewFields> 
             </View>";  
    
          ListItemCollection listItems = spList.GetItems(camlQuery); 
          clientContext.Load(listItems); 
          clientContext.ExecuteQuery();
    
          var listItem = listItems.FirstOrDefault();
          Console.WriteLine(listItem["FileRef"].ToString());
    
       }
    }

    Best regards,

    Patrick

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Marked as answer by DapperDev_ Wednesday, March 16, 2016 8:47 AM
    Wednesday, March 16, 2016 2:03 AM
    Moderator
  • Replace your caml code with

    var query=CcamlQuery.CreateAllItemsQuery();

    query.ViewXml = "<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>" + filename + "</Value></Eq></Where>";


    To

    var query = new CamlQuery();
     query.ViewXml = "<View>"
                                       + "<Query>"
                                       + "<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>" + filename + "</Value></Eq></Where>"
                                       + "</Query>"
                                       + "</View>";


    • Marked as answer by DapperDev_ Wednesday, March 16, 2016 8:46 AM
    Wednesday, March 16, 2016 7:34 AM

All replies

  • Hi,

    The code below work for me, you can take it for a try:

    public static void getItemByFileName()
    { 
       var fileName = "temp.txt";
       //var fileName = "test.avi";
    
       ClientContext clientContext = new ClientContext("http://sps2013"); 
       Microsoft.SharePoint.Client.List spList = clientContext.Web.Lists.GetByTitle("LB0309"); 
       clientContext.Load(spList); 
       clientContext.ExecuteQuery(); 
    
       if (spList != null && spList.ItemCount > 0) 
       {
          Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery(); 
          camlQuery.ViewXml = 
             @"<View>  
                   <Query> 
                      <Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>"+fileName+@"</Value></Eq></Where> 
                   </Query> 
                    <ViewFields><FieldRef Name='FileRef' /><FieldRef Name='FileLeafRef' /></ViewFields> 
             </View>";  
    
          ListItemCollection listItems = spList.GetItems(camlQuery); 
          clientContext.Load(listItems); 
          clientContext.ExecuteQuery();
    
          var listItem = listItems.FirstOrDefault();
          Console.WriteLine(listItem["FileRef"].ToString());
    
       }
    }

    Best regards,

    Patrick

    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Marked as answer by DapperDev_ Wednesday, March 16, 2016 8:47 AM
    Wednesday, March 16, 2016 2:03 AM
    Moderator
  • Replace your caml code with

    var query=CcamlQuery.CreateAllItemsQuery();

    query.ViewXml = "<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>" + filename + "</Value></Eq></Where>";


    To

    var query = new CamlQuery();
     query.ViewXml = "<View>"
                                       + "<Query>"
                                       + "<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='File'>" + filename + "</Value></Eq></Where>"
                                       + "</Query>"
                                       + "</View>";


    • Marked as answer by DapperDev_ Wednesday, March 16, 2016 8:46 AM
    Wednesday, March 16, 2016 7:34 AM