How to get/load File versions for custom list item using csom? RRS feed

  • Question

  • Hi,

    I have sp application i am loading listitem properties with File versions using csom, but when try load/get data about File Versions for custom list item(BaseType.GenericList) getting below error

    item.File.Versions.Count' threw an exception of type 'Microsoft.SharePoint.Client.CollectionNotInitializedException'

    below code snippet to File and other properties to load

    ClientContext.Load<ListItem>(listitem, i => i.DisplayName, i => i, i => i.Folder, i => i.Folder.Name, i => i.Folder.ServerRelativeUrl, i => i.File, i => i.File.Versions, i => i.File.CheckedOutByUser, i => i.File.CheckOutType, i => i.File.ServerRelativeUrl,
                        ,i => i.ParentList, i => i.ParentList.Id, i => i.ParentList.DefaultViewUrl, i=>i.ParentList.DefaultDisplayFormUrl, i => i.ParentList.Views, i => i.ParentList.Fields, i => i.ParentList.RootFolder, i => i.ParentList.RootFolder.Name, i => i.ParentList.RootFolder.ServerRelativeUrl, i => i.ParentList.ContentTypes.Include(c => c, c => c.Fields),                                        i => i.ContentType, i => i.ContentType.Fields, i => i.ContentType.Parent, i => i.ContentType.Parent.Id,
                        i => i.ParentList.ParentWeb, i => i.ParentList.ParentWeb.Id, i => i.ParentList.ParentWeb.SiteUsers, i => i.ParentList.ParentWeb.Language, i => i.ParentList.ParentWeb.RegionalSettings.TimeZone,
                        i => i.ParentList.IsApplicationList, i => i.ParentList.IsCatalog, i => i.ParentList.IsPrivate, i => i.ParentList.IsSiteAssetsLibrary, i => i.ParentList.Hidden, i => i.ParentList.BaseTemplate,                    
                        i => i["FileDirRef"],);

    This code works for normal document item (BaseType.DocumentLibrary)

    If anyone having idea on this error please help me.



    • Edited by ravinalla Thursday, November 28, 2019 6:26 AM
    Thursday, November 28, 2019 6:04 AM

All replies

  • Hi ravinalla,

    This is by design action.

    Custom List can't used to store the files in Item Level, so both item.File and item.File.Versions won't return data.

    In SharePoint, the file should be stored in the Document Library, so the code snippet above will work for Document Library, this make sense.

    So for Custom List item, SharePoint CSOM didn't provide VersionsCollection for ListItem object yet, we need to call Lists.asmx to get versions instead, please refer the following demo:

    How To Get All SharePoint List Item Versions Using CSOM For Custom List And Document Library 


    Best Regards

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, November 29, 2019 2:30 AM