Frage

I'm currently trying to pull the document history of a SharePoint 2010 site collection via CSOM.

The code that I am using to accomplish this is here:

using (var clientContext = new ClientContext("http://localhost/sites/mysite"))
{
    File file = clientContext.Web.GetFileByServerRelativeUrl(url);
                clientContext.Load(file, f => f.ListItemAllFields);
                clientContext.ExecuteQuery();
}

Whenever I run this code it throws an exception stating:

Server Exception was Unhanded by User Code

Value does not fall within the expected range

Please Note that:

  • Version History is Turned On
  • Changing f.ListItemAllFields to f.Versions does not fix it either.
War es hilfreich?

Lösung

Here is my code to get document history and save in a dataset

    public DataSet GetDoucmentHistory(string siteUrl, string listName, int id)
    {
        using (ClientContext ctx = new ClientContext(siteUrl))            {
            ctx.Credentials = new NetworkCredential(_username, _password, _domain);
            var file = ctx.Web.Lists.GetByTitle(listName).GetItemById(id).File;
            var versions = file.Versions;
            ctx.Load(file);

            ctx.Load(versions);
            ctx.Load(versions, vs=>vs.Include(v=>v.CreatedBy));

            ctx.ExecuteQuery();

            var ds = CreatHistoryDataSet();
            foreach (FileVersion fileVersion in versions)
            {
                var row = ds.Tables[0].NewRow();
                row["CreatedBy"] = fileVersion.CreatedBy.Title;
                row["Comments"] = fileVersion.CheckInComment;
                row["Created"] = fileVersion.Created.ToShortDateString() + " " +
                                 fileVersion.Created.ToShortTimeString();
                row["Title"] = file.Title;
                row["VersionLabel"] = fileVersion.VersionLabel;
                row["IsCurrentVersion"] = fileVersion.IsCurrentVersion;
                ds.Tables[0].Rows.Add(row);
            }

            return ds;
        }

    }

    private static DataSet CreatHistoryDataSet()
    {
        DataSet ds = new DataSet();
        DataTable table = new DataTable();
        table.Columns.Add("Title");
        table.Columns.Add("Created");
        table.Columns.Add("CreatedBy");
        table.Columns.Add("EncodedAbsUrl");
        table.Columns.Add("VersionLabel");
        table.Columns.Add("Comments");
        table.Columns.Add("IsCurrentVersion");
        ds.Tables.Add(table);
        return ds;
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top