【问题标题】:Read And Write Data to SharePoint Site Pages读取和写入数据到 SharePoint 网站页面
【发布时间】:2016-06-04 12:24:51
【问题描述】:

我是 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 网站页面以在其中读取和写入数据?

【问题讨论】:

    标签: c# asp.net sharepoint


    【解决方案1】:

    您正在遍历 colList 的所有字段,但仅包括 IdTitle 中的字段

     clientContext.Web.Lists.Include(
            list => list.Title,
            list => list.Id));
    

    这就是为什么您会遇到异常。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-02
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      相关资源
      最近更新 更多