1

I am using C# Winforms and CSOM - am attempting to return the version history with this method. I have verified that all variables being passed in hold an appropriate and correct value. But once the ExecuteQuery() line is hit the error is thrown

Public static DataSet Gethistory(string siteurl, string listName, int Id)
{
  Using (ClientContext ctx = new ClientContext(siteUrl))
  {
    Var file = ctx.Web.Lists.GetByTitle(listName).GetItemId(Id).File;
    Var versions = file.Versions;
    Ctx.Load(file);
    Ctx.Load(versions);
    Ctx.Load(versions, vs => vs.Include(v => v.CreatedBy))
    Ctx.ExecuteQuery();
  }
  For each (FileVersion fv in versions)
  {
      Console.writeline(fv.createdby.title);
       Console.writeline(fv.checkincomment);
      Console.writeline(fv.iscurrentversion);
   }
}

Sorry for any typos this was posted mobile!

Edit to show updated code:

Public static DataSet GetHistory(string siteUrl, string listName, isn’t id)
{
    Using (ClientContext ctx = new ClientContext(siteUrl))
    {
      Web web = ctx.Web;
      ctx.Load(web);
      ctx.Load(web.Lists);
      ctx.ExecuteQuery();
      var list = web.Lists.GetByTitle(listName);
      var item = list.GetItemById(id);
      ctx.Load(item);
      ctx.Load(item.File);
      ctx.Load(item.File.Versions);
      ctx.ExecuteQuery();
      var versions = item.File.Versions;
      foreach (FileVersion fileVersion in versions)
      {

      }
    }
1

Try the below code snippet to get the file versions

    siteUrl= "https://server";
    Using (ClientContext ctx = new ClientContext(siteUrl))
    {
      ctx.Credentials = new NetworkCredential("user", "password", "domain");
      Web web = ctx.Web;
      ctx.Load(web);
      ctx.Load(web.Lists);
      ctx.ExecuteQuery();
      var list = web.Lists.GetByTitle(listName);
      var item = list.GetItemById(id);
      ctx.Load(item);
      ctx.Load(item.File);
      ctx.Load(item.File.Versions);
      ctx.ExecuteQuery();
      var versions = item.File.Versions;
      foreach (FileVersion fileVersion in versions)
      {

      }
}
  • I alter syntax to be var file = ctx.Web.Lists.Getbytitle(list name).Getitembyid(is).file; ctx.load(file); ctx.executequery(); and have same error thrown. Object returned by the following call stack is null. “File getitembyid getbytitle” – user2676140 Apr 16 '18 at 17:56
  • Ctx. Load(ctx. Web) before initialize var file im writing from cell phone so revise the code – A.Saéed Apr 16 '18 at 18:07
  • I add line ctx.load(ctx.Web); before var file... and same error – user2676140 Apr 16 '18 at 18:10
  • @user2676140 please check my update – A.Saéed Apr 16 '18 at 19:53
  • That gets me to the foreach loop and throws the error - the collection has not been initialized. It has not been requested or the request has not been executed. – user2676140 Apr 17 '18 at 14:15
0

The error is basically for the properties which you are trying to load as part of the version collection

Console.writeline(fv.checkincomment);
Console.writeline(fv.iscurrentversion);

Since these are not being initialized in as part of your include statement. Please try below code and check if it works:

var list = context.Web.Lists.GetByTitle(listName);
var versions = list.GetItemById(2).File.Versions;
context.Load(list, li => li.Title, li => li.EnableVersioning);
context.Load(versions, vc => vc.Include(v => v.CreatedBy, v => v.IsCurrentVersion, v => v.CheckInComment));
context.ExecuteQuery();
Console.WriteLine("List Title: {0}, List Version Control Status: {1}", list.Title, list.EnableVersioning);
Console.WriteLine("Version count: {0}", versions.Count);

foreach (var version in versions)
{
    Console.WriteLine("This version was created by: {0}", version.CreatedBy.Title);
    Console.WriteLine(version.IsCurrentVersion);
    Console.WriteLine(version.CheckInComment);
 }

Hope this helps !

  • I get debug error on ctx.ExecuteQuery(); of can not invoke method or retrieve property from null object. Object returned by the following call stack is null. file GetItemById GetByTitke – user2676140 Apr 17 '18 at 19:09
  • The error has something to do with the ctx object. Try passing the credentials context.Credentials = new SharePointOnlineCredentials(O365_UserName, SecureString); (Secure String here is your O365 password) If you are using On-Prem then you might have to use NetworkCredentials – Anupam Jagatdeo Apr 17 '18 at 19:22
  • I get compile error of can not convert string to system.security.securestring – user2676140 Apr 17 '18 at 20:26
  • I get a 401 Unathorized error? – user2676140 Apr 18 '18 at 0:28
  • Please follow this link to connect console application to O365. Make sure you use the O365_UserName as (abc@<tenant>.onmicrosoft.com). The 401 suggests that you do not have access to read the list. Please check if you have access to the site/list. – Anupam Jagatdeo Apr 18 '18 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.