Created
January 24, 2019 14:09
-
-
Save OlafD/04ee6962b55aad961c508d39532b05b2 to your computer and use it in GitHub Desktop.
A program.cs for a simple console application that shows the version history for a listitem form a SharePoint Online list. This code comes from a simple test application and needs some rework to get a better performance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Security; | |
using Microsoft.SharePoint.Client; | |
namespace VersioningTester | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var url = "https://{tenant}.sharepoint.com/sites/my-tester"; | |
var user = "admin@{tenant}.onmicrosoft.com"; | |
var pw = "<put the correct password here>"; | |
var password = new SecureString(); | |
foreach (char c in pw.ToCharArray()) password.AppendChar(c); | |
using (var ctx = new ClientContext(url)) | |
{ | |
ctx.Credentials = new SharePointOnlineCredentials(user, password); | |
// get v5.0 and higher | |
int fromVersionId = 512 * 5; | |
Web web = ctx.Web; | |
List list = web.Lists.GetByTitle("Index Fields"); | |
ListItem item = list.GetItemById(1); | |
ctx.Load(web); | |
ctx.Load(list, l => l.Fields); | |
ctx.Load(item, i => i.Versions.Where(v => v.VersionId >= fromVersionId)); | |
ctx.ExecuteQueryRetry(); | |
FieldCollection fields = list.Fields; | |
ListItemVersionCollection versions = item.Versions; | |
// versions[0] is the current version | |
// versions[n] is a older version | |
// the last one is the version, we need to compare the changes to | |
// iterate from n-1 to 0 | |
int n = versions.Count - 1; | |
for (int i = n - 1; i >= 0; i--) | |
{ | |
string versionNumber = versions[i].VersionLabel; | |
Console.ForegroundColor = ConsoleColor.Magenta; | |
Console.WriteLine("Version: {0}", versionNumber); | |
Console.ResetColor(); | |
ListItemVersion workingVersion = versions[i]; | |
ListItemVersion compareToVersion = versions[i + 1]; | |
Dictionary<string, object> workingVersionFields = workingVersion.FieldValues; | |
Dictionary<string, object> compareToVersionFields = compareToVersion.FieldValues; | |
using (Dictionary<string, object>.Enumerator e = workingVersionFields.GetEnumerator()) | |
{ | |
while (e.MoveNext()) | |
{ | |
string fieldName = e.Current.Key; | |
Field field = fields.GetFieldByInternalName(fieldName); | |
if ((field != null) && (field.Hidden == false)) | |
{ | |
if (compareToVersionFields.ContainsKey(fieldName) == true) | |
{ | |
object currentValue = e.Current.Value; | |
object oldValue = compareToVersionFields[fieldName]; | |
if ((currentValue != null) && (oldValue != null)) | |
{ | |
if (currentValue.ToString() != oldValue.ToString()) | |
{ | |
Console.WriteLine("{0} : {1}", fieldName, currentValue.ToString()); | |
} | |
} | |
else if ((currentValue == null) && (oldValue != null)) | |
{ | |
Console.WriteLine("{0} : ", fieldName); | |
} | |
else if ((currentValue != null) && (oldValue == null)) | |
{ | |
Console.WriteLine("{0} : {1}", fieldName, currentValue.ToString()); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment