user1088259
user1088259

Reputation: 355

Sharepoint return incorrect file version

In Sharepoint i have file with several history versions. I want delete all file version history, but api retrun me that we have 0 versions of file. Why so?

CamlQuery query = new CamlQuery();
query.ViewXml = "<View Scope='RecursiveAll'><RowLimit>50</RowLimit></View>";

List<ListItem> items = new List<ListItem>();
    do
      {
       ListItemCollection listItemCollection = lst.GetItems(query);
       ctx.Load(listItemCollection);
       ctx.ExecuteQuery();

       items.AddRange(listItemCollection);
       foreach (var item in items)
       {
          try
           {
            var file = ctx.Web.GetFileByServerRelativeUrl(item.FieldValues["FileRef"].ToString());
            ctx.Load(file);
            ctx.Load(file.Versions);
            ctx.ExecuteQuery();

Upvotes: 0

Views: 92

Answers (2)

Zain
Zain

Reputation: 1252

var file = ctx.Web.GetFileByServerRelativeUrl(item.FieldValues["FileRef"].ToString());

You're using GetFileByServerRelativeUrl but you're passing in the file name. Use ServerRelativeUrl and you should be ok.

Upvotes: 1

Adam
Adam

Reputation: 840

strange... I think the code seems fine :). Maybe just one small thing and I am not sure that it will do the trick but I would first init some variable to file.Versions and then after execute work on this variable. Something like:



    var versions = file.Versions;
    ctx.Load(file);
    ctx.Load(versions);
    ctx.ExecuteQuery();
    foreach (FileVersion fileVersion in versions)
    {
       // check
    }

I know... probably not this.. but it's always worth to check :).

Also be sure that the account that is running this code has the needed permissions to get versions. You may always define the account with network credentials like:



    ctx.Credentials = new NetworkCredential(_username, _password, _domain);

Upvotes: 1

Related Questions