1
foreach (SPListItem item in listitems)
{
    String value = item.File.Name;    
}    

The above sever side code shows how to retrieve the name of a document which is saved in a SharePoint document library by using item.File.Name. I have done this completely, but how do I do the same thing using the Client Object Model. What should I use instead of item.File.Name?

2

May be this is the code you are looking for :

var fileName = '';  
var fileTitle = '';
_spBodyOnLoadFunctionNames.push("LoadAction");
function LoadAction() 
{
GetFileDetails(fileID);

// File Title   
if(fileTitle != undefined)
    document.getElementById("txtTitle").innerHTML = fileTitle;
// File Name
if(fileName != undefined)
    document.getElementById("txtName").innerHTML = fileName;    
   }


function GetFileDetails(itemID)
{
    var temp_fileName = '';
    var temp_title ='';
    var query = "<Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + itemID + "</Value></Eq></Where></Query>";
    $().SPServices({
        operation: "GetListItems",
        listName: "Project Files",
        CAMLViewFields: "<ViewFields>" +
                            "<FieldRef Name='FileLeafRef' />"+
                            "<FieldRef Name='Title' />" +
                        "</ViewFields>",
        CAMLQuery: query,
        async: false,
        completefunc: function (xData, Status) {
            var xmlDoc = $.parseXML(xData.responseText);
            //$(xmlDoc).find("z\\:row").each(function(){
            $(xmlDoc).find("[nodeName='z:row']").each(function(){
                temp_fileName = $(this).attr("ows_FileLeafRef");
                temp_title = $(this).attr("ows_Title");    
            });
        }
    });

    fileName = temp_fileName == '' ? '': temp_fileName.split(';#')[1];
    fileTitle = temp_title; 
} 
1

You can still use item.File.Name. The difference being that this list item is a Microsoft.SharePoint.Client.ListItem type, and the .File here is Microsoft.SharePoint.Client.File.

If you're looking to learn more about how to use the CSOM, this should help get you started:

How to: Complete basic operations using SharePoint 2013 client library code

  • thank you for your time and consideration, but i need to get the url of the particular document by using jquery. how would i ?? – Nera Jul 9 '13 at 13:58
  • 1
    If you have another question, you should post it as a new thread. Please refer to sharepoint.stackexchange.com/help/how-to-ask for further clarification. – RJ Cuthbertson Jul 9 '13 at 14:38
  • Thanks, that worked for me. (FYI, you will have to load the File and File.Name property but they can all be retrieved in the same call; e.g. ctx.Load(items, i => i.Include(x => x.DisplayName, x => x.File, x => x.File.Name));.) – BrainSlugs83 Mar 21 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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