Gra*_*rne 2 c# sharepoint sharepoint-2010 csom
我目前正在尝试通过 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();
}
Run Code Online (Sandbox Code Playgroud)
每当我运行此代码时,它都会抛出一个异常,说明:
用户代码未处理服务器异常
值不在预期范围内
请注意:
这是我获取文档历史记录并保存在数据集中的代码
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7110 次 |
| 最近记录: |