Forum Discussion

Nick's avatar
Nick
Frequent Visitor
9 years ago

Getting SharePoint List items with full history version?

Hi,

 

We do have a SharePoint list to manage Fleet inventory of asset: mainly their attribution (who own it) and their status (active, lost, broken, etc.).

We did activated the SharePoint Version Control and I would like to use this information to run histroical report overtime around such assets status and ownership.

Right now, the only solution I found is to eitheir take a snapshot of the full list on a regular basis into another datasource.

Is there anyway to get such history/version data directly avoiding wruting such separate datasource?

 

Thansk in advance,

Nick

46 Replies

  • JensG's avatar
    JensG
    Advocate II

    Hi

     

    I like to share a function/query which will help to get the Version details via SharePoint API for a list item.

     

    Function/Query:
    let
        Source = (VersionsRelevantSharePointListName as text, VersionsRelevantSharePointLocation as text, VersionsRelevantItemID as number) => let
            Source = Xml.Tables(Web.Contents(Text.Combine({
        VersionsRelevantSharePointLocation,
        "/_api/web/Lists/getbytitle('",
        VersionsRelevantSharePointListName ,
        "')/items(",
        Text.From(VersionsRelevantItemID),
        ")/versions"}
        ))),
            entry = Source{0}[entry],
            #"Removed Other Columns2" = Table.SelectColumns(entry,{"content"}),
            #"Expanded content" = Table.ExpandTableColumn(#"Removed Other Columns2", "content", {"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"}, {"content"}),
            #"Expanded content1" = Table.ExpandTableColumn(#"Expanded content", "content", {"properties"}, {"properties"}),
            #"Expanded properties" = Table.ExpandTableColumn(#"Expanded content1", "properties", {"http://schemas.microsoft.com/ado/2007/08/dataservices"}, {"properties"})
        in
            #"Expanded properties"
    in
        Source

    3 parameters are required for the function:

    VersionsRelevantSharePointLocation => https://<yourAddress>.sharepoint.com/sites/<yourSite>

    VersionsRelevantSharePointListName => Your SharePoint List Name

    VersionsRelevantItemID => SharePoint list item ID

    when you invoke this Function to an SharePoint list you get the table with all related versions for that item.
    This can be further expanded to get to the full set of columns from that list.

     

    Hope this helps. 

     

    Have Fun!

    • Anonymous's avatar
      Anonymous
      Not applicable

      please share how do we apply schedule refresh on this dynamic data source, 

      • mzzwtr's avatar
        mzzwtr
        Regular Visitor

        Hi Anonymous,

         

        I suggest you to read these articles:

         

        To set a scheduled refresh using dynamic data source, I've edit the function of JensG in this way:

         

         

        let
            Source = (VersionsRelevantSharePointListName as text, VersionsRelevantSharePointLocation as text, VersionsRelevantItemID as number) => let
                Source = Xml.Tables(Web.Contents(
            "https://YourAddress.sharepoint.com/sites/",
            [RelativePath = VersionsRelevantSharePointLocation & "/_api/web/Lists/getbytitle('" & VersionsRelevantSharePointListName & "')/items(" & Text.From(VersionsRelevantItemID) & ")/versions"]
            )),
                entry = Source{0}[entry],
                #"Removed Other Columns2" = Table.SelectColumns(entry,{"content"}),
                #"Expanded content" = Table.ExpandTableColumn(#"Removed Other Columns2", "content", {"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"}, {"content"}),
                #"Expanded content1" = Table.ExpandTableColumn(#"Expanded content", "content", {"properties"}, {"properties"}),
                #"Expanded properties" = Table.ExpandTableColumn(#"Expanded content1", "properties", {"http://schemas.microsoft.com/ado/2007/08/dataservices"}, {"properties"})
            in
                #"Expanded properties"
        in
            Source

         

         

         

        3 parameters are required for the function:

        1. VersionsRelevantSharePointLocation => The subdirectory of your SharePoint site (is what come after ...sites/)
        2. VersionsRelevantSharePointListName => Your SharePoint List Name
        3. VersionsRelevantItemID => SharePoint list item ID

         

         

        Then, once you have published your report into Power BI service, I raccomend to read these articles:

         

         

        Hope this may help you.

         

        Bye!

    • bibinthomas's avatar
      bibinthomas
      Frequent Visitor

      Hi JensG,

       

      The query is to get the version history of a single List Item (ID). How do we get the version history of all the list items.

    • tdaskalakis's avatar
      tdaskalakis
      New Member

      How do you expand this function to get the complete version history for all rows in a column?

      • JensG's avatar
        JensG
        Advocate II

        Hi tdaskalakis,

         

        here is an short example how to use the function:

        1) Add an Query to your list where you want to see the version history:

         

        	let
        	    Source = SharePoint.Tables("https://<yourDomain>.sharepoint.com/sites/<YourSPSite>", [ApiVersion = 15]),
        	    #"Filtered Rows" = Table.SelectRows(Source, each ([Title] = "SampleList")),
        	    #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Items", "Title"}),
        	    #"Expanded Items" = Table.ExpandTableColumn(#"Removed Other Columns", "Items", {"Id"}, {"Items.Id"})
        	in
        	    #"Expanded Items"

        2) Invoke the Custom Function "GetVersionHistoryFromSharePointList" (from post above)

         

         

        3.) after the invoke your table will have the column version which contains an table which contains a table per version of the item.



        4) expand the Table.



        5) now you have all possible items in a list, now you can further expand specific columns as you need.



        Example code after those steps:

        	let
        	    Source = SharePoint.Tables("<Your SP Site Link>", [ApiVersion = 15]),
        	    #"Filtered Rows" = Table.SelectRows(Source, each ([Title] = "SampleList")),
        	    #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Items", "Title"}),
        	    #"Expanded Items" = Table.ExpandTableColumn(#"Removed Other Columns", "Items", {"Id"}, {"Items.Id"}),
        	    #"Invoked Custom Function" = Table.AddColumn(#"Expanded Items", "Versions", each GetVersionHistoryFromSharePointList([Title], "<Your SharePoint Site>", [Items.Id])),
        	    #"Expanded Versions" = Table.ExpandTableColumn(#"Invoked Custom Function", "Versions", {"properties"}, {"Versions.properties"}),
        	    #"Expanded Versions.properties" = Table.ExpandTableColumn(#"Expanded Versions", "Versions.properties", {"IsCurrentVersion", "VersionId", "VersionLabel", "Title", "ExampleText"}, {"Versions.properties.IsCurrentVersion", "Versions.properties.VersionId", "Versions.properties.VersionLabel", "Versions.properties.Title", "Versions.properties.ExampleText"})
        	in
        	    #"Expanded Versions.properties"



    • s-roberts's avatar
      s-roberts
      Helper I

      Is there any update for this solution, considering this was a post from 2016? 

      These solutions work well in PBI Desktop but when publishing to the service I'm unable to refresh the dataset.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi, s-roberts  ,

         

        Try the solution suggested by Chris Webb here.

        As I understand it, you need to hide away part of the API call's string from Power Query and append it during the run, so the code would look like this:

        Web.Contents("<your-webpage>",[RelativePath = "<continuation of the API call>"])

        I was able to pull in full version history and be able to set auto-refresh on the Service. 

         
        Thanks 
        Evan
    • Nick's avatar
      Nick
      Frequent Visitor

      Greg_Deckler

      Thanks but it looks I can only extract one version of a specific item. However thanks for sharing URLs: a solution may be around.

      • thakks's avatar
        thakks
        Helper I

        Guys did you had any luck extracting all version from the sharepoint list by any chances..

  • v-ljerr-msft's avatar
    v-ljerr-msft
    Microsoft Employee

    Nick


    Right now, the only solution I found is to eitheir take a snapshot of the full list on a regular basis into another datasource.

    Is there anyway to get such history/version data directly avoiding wruting such separate datasource?


    You can export version history of SharePoint List Items to Microsoft Excel first. Then export that Excel table to your SharePoint List. So that the version history and  other data will be in the same datasource.

     

    Regards

    • Nick's avatar
      Nick
      Frequent Visitor

      v-ljerr-msft

      Thanks but I can't execute PS scrript right now due to my company policies.

      In addition I also wanted to avoid data multipliciation: maybe SharePoint list may not be good for what I'm looking for and I may ave to swicth to a real DB model.

  • llaumans's avatar
    llaumans
    Frequent Visitor

    Hi Nick,

     

    did you ever figure out how to retrieve the history for items in a SharePoint list so you could run historical reports on your data? I would be very interested to learn how you managed to get this done!

     

    Thanks in advance!

     

    Kind Regards,

     

    Lucas

     

    • Nick's avatar
      Nick
      Frequent Visitor

      Hi Lucas,

       

      The current workaround I'm currently using since I don't have access to SharePoint DB/Framework is via R script:

       

      Here I'm taking snapshot of Group, Status, Id, and ExtractDate data into a local CSV file, everytime I'm refreshing the Data Source.

       

      Not fancy but it does the job for now.

      Hope that will help.

       

      Nick

      • Anonymous's avatar
        Anonymous
        Not applicable

        This one worked for me:

        https://sharepoint.stackexchange.com/questions/190021/exporting-item-list-version-history

        :manhappy:

         

        Short explaination:

        The "&IncludeVersions=TRUE" gives all changes form all versions (form all fields)

        Make a Sharepoint List which only contains the columns you need. (or split it up in several views and repeat this trick)

         

        Open the XML in Excel en there you can create a PivotTable and if you include the version field in SharePoint view.

        You can see the version number in the Pivot :smileyvery-happy: Just sort on this version number and you will see the latest and all previous versions.

  • Anonymous's avatar
    Anonymous
    Not applicable

    This one worked for me:

    https://sharepoint.stackexchange.com/questions/190021/exporting-item-list-version-history

    :manhappy:

     

    Short explaination:

    The "&IncludeVersions=TRUE" gives all changes form all versions (form all fields)

    Make a Sharepoint List which only contains the columns you need. (or split it up in several views and repeat this trick)

     

    Open the XML in Excel en there you can create a PivotTable and if you include the version field in SharePoint view.

    You can see the version number in the Pivot :smileyvery-happy: Just sort on this version number and you will see the latest and all previous versions.

    • madkins420's avatar
      madkins420
      New Member

      There appears to be a 30 column limit on the &IncludeVersions=TRUE approach

    • madkins420's avatar
      madkins420
      New Member

      There appears to be a 30 column limit on the &IncludeVersions=TRUE approach

  • how can we get full version history of all items? I am trying to create report based on the no of days an item stayed in a particular status, any help will be appreciated.

  • MD1's avatar
    MD1
    New Member

    Hello all,

    I am trying to run this query but I am getting below error. Do you know what can be the reason of it?

  • Is there a way to limit the amount of version returned by adding a date range into the api code? If so, do you know what code to add to include a date range? 

    • Anonymous's avatar
      Anonymous
      Not applicable

      Did you ever get this resolved?