MSJ
MSJ

Reputation: 423

How can I download the list item versions

My question is about sharepoint online. In my project I want to download the list item with its versions. I am able to download the current list item version but not able to download its version. I have referred this answer which shows how to calculate the Canonical and Revision paths. But while fetching the data I am getting error as

The remote server returned an error: (403) Forbidden.

and in response header getting value as "Access denied. Before opening files in this location%2c you must first browse to the web site and select the option to login automatically."

 string url = "https://test.sharepoint.com/teams/Mycompany";
ScureString f_SecurePass = new SecureString();
foreach (char ch in password)
    f_SecurePass.AppendChar(ch);

clientcontext = new ClientContext(url);
var credentials = new SharePointOnlineCredentials(userid, f_SecurePass);                
clientcontext.Credentials = credentials;
Web web = clientcontext.Web;
clientcontext.Load(web, website => website.Lists);
clientcontext.ExecuteQuery();

CamlQuery camlQ = new CamlQuery();                            
camlQ.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +
                    "<Value Type='Number'>0</Value></Geq></Where></Query><RowLimit>100</RowLimit></View>";

var cq = _list.GetItems(camlQ);
clientcontext.Load(cq, items => items.Include(item => item.Id,
                item=>item.EffectiveBasePermissionsForUI,
                item=>item.EffectiveBasePermissions));

clientcontext.ExecuteQuery();

var itm  =  _list.GetItemById(itemid);
clientcontext.Load(itm, r => r.Id, r => r.DisplayName);
clientcontext.ExecuteQuery();


foreach (FileVersion itemVersion in itm.File.Versions)
{
  int size = itemVersion.Size;
  string versionlbl =  itemVersion.VersionLabel;
  string newversion = url + itemVersion.Url;
  System.WebClient client = new System.Net.WebClient();
  client.Credentials = new NetworkCredential(userid, f_SecurePass);
  System.IO.Stream Data = client.OpenRead(newversion);// Throws exception
}

How can I download the list item versions?

UPDATE: If I try to download file version by using

File.OpenBinaryDirect(clientcontext, newversion); it throws following error

Message = "Specified argument was out of the range of valid values.\r\nParameter name: serverRelativeUrl"

Upvotes: 0

Views: 860

Answers (1)

Related Questions