Forum Discussion
Getting SharePoint List items with full history version?
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
Source3 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!
- Anonymous4 years agoNot applicable
please share how do we apply schedule refresh on this dynamic data source,
- mzzwtr4 years agoRegular Visitor
Hi Anonymous,
I suggest you to read these articles:
- Setting a scheduled refresh on a Dynamic Data Source in Power BI
- Web.Contents(), M Functions And Dataset Refresh Errors In Power BI
- Using The RelativePath And Query Options With Web.Contents() In Power Query And Power BI M Code
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 Source3 parameters are required for the function:
- VersionsRelevantSharePointLocation => The subdirectory of your SharePoint site (is what come after ...sites/)
- VersionsRelevantSharePointListName => Your SharePoint List Name
- VersionsRelevantItemID => SharePoint list item ID
Then, once you have published your report into Power BI service, I raccomend to read these articles:
- [Unable to combine data] accessing data sources that have privacy levels
- Information is needed in order to combine data
Hope this may help you.
Bye!
- Anonymous4 years agoNot applicable
THANKS A LOT, ❤️ YOU SAVED MY DAY 🙂 YOUR ABOVE CODE WORS FINE
- bibinthomas3 years agoFrequent 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.
- tdaskalakis6 years agoNew Member
How do you expand this function to get the complete version history for all rows in a column?
- JensG6 years agoAdvocate 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"- Anonymous6 years agoNot applicable
Awsome! You made my day. Thanks a lot. 🙂
- Anonymous6 years agoNot applicable
JensG cool that you posted a function/query. Unfortunately, it does not work for me. Any ideal how I can fix that without too much effort. The error says Expression.Error: The access to the ressourc is prohibites (translated from my version: "Der Zugriff auf die Ressource ist untersagt."). I checked that I am properly logged in and check the list that there are no permission restrictions. At least I cannot see any. I am assuming that the account I am logged into PowerBI must also have access the SharePoint list or is there anything else I need to consider?
Are you sure that the links in your function are still valid?
http://schemas.microsoft.com/ado/2007/08/dataservices/metadata
http://schemas.microsoft.com/ado/2007/08/dataservices
Appreciate, if someone has a clue what is wrong with it?
Thank you so much
- JensG6 years agoAdvocate II
Hi udobausc,
sorry I don´t have seen this error on my end yet.
I´m logged in with my O365 Users in Power BI, same user who has access to the SP stuff.
Anyhow sometimes Power BI is promting to enter credentials where I can select differrent methods. But I always use the same user.
regarding the old links, it is when I expand some columns this is returned as field name and I have just kept it as it comes from the SP API :-)
it might be worth to check if the underlying link is working in your browser when you replace the values in <>:<VersionsRelevantSharePointLocation>/_api/web/Lists/getbytitle('<VersionsRelevantSharePointListName>')/items(<VersionsRelevantItemID>)/versionshave fun
- ImkeF6 years agoCommunity Champion
Hi Anonymous
I had to add my SharePoint URL to this list of trusted sites in my Internet Options to overcome that error-message.
- Anonymous6 years agoNot applicable
Awsome! You made my day. Thanks a lot. 🙂
- NickTT5 years agoHelper III
This is awesome! I just got a request if this was possible!