Eugene
Eugene

Reputation: 319

How do I populate the File member of a ListItem using the client object model Sharepoint 2013 API?

When you execute a query on a List that is a Document Library, ListItems have a File member that is not populated. Obviously the query doesn't pull in all data by default for performance reasons, but I can't figure out a way to tell the query to populate the File Member, since the query syntax is generic for all Lists and only Document Libraries contain files in them.

Upvotes: 1

Views: 958

Answers (2)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

In order to retrieve ListItem with File property initialized it should be specified explicitly via ClientRuntimeContext.Load Method:

private static ListItem GetListItem(string url, ICredentials creds, string listTitle, int listItemId)
{
    using (var clientContext = new ClientContext(url))
    {
        clientContext.Credentials = creds;

        var list = clientContext.Web.Lists.GetByTitle(listTitle);
        var listItem = list.GetItemById(listItemId);
        clientContext.Load(list);
        clientContext.Load(listItem, i => i.File); //specify File property 
        clientContext.ExecuteQuery();
        return listItem;    
    }
}

Upvotes: 1

Vardhaman Deshpande
Vardhaman Deshpande

Reputation: 204

This sample code should help you out:

    var ctx = SP.ClientContext.get_current();

    var itemFile = ctx.get_web().get_lists().getByTitle("Shared Documents").getItemById(1).get_file();

    ctx.load(itemFile);

    ctx.executeQueryAsync(function(){

    console.log(itemFile.get_title());

    },function(sender,args){

    console.log(args.get_message());

    });

Upvotes: 0

Related Questions