读取和写入SharePoint网站页面的数据

时间:2016-06-03 19:48:08

标签: c# asp.net sharepoint

我是Sharepoint的新手,目前我想写一个函数,我可以在其中读取和写入Sharepoint页面的数据。这是我想要连接到读取和写入数据的URL的结构:https://mycompany.sharepoint.com/sites/SubSite/SubSite1/SitePages/Home.aspx
然后,我使用以下方式打开与它的连接:

ClientContext clientContext = new ClientContext("https://mycompany.sharepoint.com/sites/SubSite/SubSite1/");
clientContext.Credentials = new SharePointOnlineCredentials("user@mycompany.com", password);

我使用SecureString作为密码
然后,我将网站页面加载到列表中并读取每个字段以检查:

List colList = clientContext.Web.Lists.GetByTitle("Site Pages");
FieldCollection colfield = colList.Fields;
IEnumerable<Microsoft.SharePoint.Client.List> resultCollection = clientContext.LoadQuery(
        clientContext.Web.Lists.Include(
        list => list.Title,
        list => list.Id));
clientContext.Load(colList);
clientContext.Load(colfield);
clientContext.ExecuteQuery();

然后我循环遍历每个字段以检查

for(int i = 1; i <= 4; i++){  
    ListItem lst = colList.GetItemById(i);
    foreach (Field field in colfield){
        clientContext.Load(field);
        clientContext.ExecuteQuery();
        var val = lst[field.Title];
    }
}

我发现colList中有一些字段,例如:DataSource等包含此异常:

The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested

我的问题是如何正确连接到Sharepoint网站页面以读取和写入数据?

1 个答案:

答案 0 :(得分:0)

您正在遍历colList的所有字段,但仅包括Id&amp;

中的Title个字段
 clientContext.Web.Lists.Include(
        list => list.Title,
        list => list.Id));

这就是为什么你会超越异常。

您必须Include要检索的所有字段,或者只需加载列表clientContext.Load(colList);,如果要检索所有字段,请不要包含任何内容。