我目前正在尝试通过CSOM提取SharePoint 2010网站集的文档历史记录。
我用来完成此任务的代码在这里:
using (var clientContext = new ClientContext("http://localhost/sites/mysite"))
{
File file = clientContext.Web.GetFileByServerRelativeUrl(url);
clientContext.Load(file, f => f.ListItemAllFields);
clientContext.ExecuteQuery();
}
每当我运行此代码时,它都会抛出一个异常声明:
用户代码无法解决服务器异常
价值不在预期范围内
请注意:
答案 0 :(得分:4)
以下是获取文档历史记录并保存在数据集中的代码
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;
}