Sharepoint:将 GridView 与 SharePoint 列表(客户端对象模型)绑定
Sharepoint: Binding GridView with SharePoint List (Client Object Model)
我想在 .aspx 网站上显示一个列表。因此我必须使用 SP 客户端对象模型。
我找到了以下教程,但这没有使用客户端库:
http://social.technet.microsoft.com/wiki/contents/articles/30287.binding-gridview-with-sharepoint-list.aspx
到目前为止,我的代码如下所示:
ClientContext clientContext = GetContext(accessToken);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
// Get the email input list.
List inboxList = web.Lists.GetByTitle("InboxList");
Microsoft.SharePoint.Client.ListItemCollection items = inboxList.GetItems(new CamlQuery());
clientContext.Load(inboxList);
clientContext.Load(items, ic => ic.Include(i => i["DisplayName"], i => i["Client_Title"], i => i["HasUniqueRoleAssignments"]));
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem i in items)
{
clientContext.Load(i);
}
clientContext.ExecuteQuery();
oGrid.DataSource = items;
oGrid.DataBind();
但这只显示了列表项集合中的一些 "meta data",请看截图:
如果我使用 oGrid.DataSource = inboxList;
,我会得到一个 InvalidOperationException
,因为数据源不是 IListSource、IEnumerable 或 IDataSource 类型。
如果我使用 oGrid.DataSource = inboxList.DataSource;
我得到一个 PropertyOrFieldNotInitializedException
,但我不知道如何加载这个属性(通过 clientContext.Load 它不起作用)?!
我明白了 - 使用以下代码:
protected void Page_Load(object sender, EventArgs e)
{
...
ClientContext clientContext = GetContext(accessToken);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
// Get the email input list.
List inboxList = web.Lists.GetByTitle("InboxList");
Microsoft.SharePoint.Client.ListItemCollection items = inboxList.GetItems(new CamlQuery());
clientContext.Load(inboxList);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem i in items)
{
clientContext.Load(i);
}
clientContext.ExecuteQuery();
oGrid.DataSource = GetInboxListData(inboxList, items);
oGrid.DataBind();
}
else if (!IsPostBack)
{
Response.Write("Could not find a context token.");
return;
}
}
private DataTable GetInboxListData(List inboxList, Microsoft.SharePoint.Client.ListItemCollection items)
{
DataTable dt = new DataTable();
dt.Columns.Add("From");
dt.Columns.Add("To");
dt.Columns.Add("Subject");
dt.Columns.Add("Body");
dt.Columns.Add("Attachments");
dt.Columns.Add("Sent");
DataRow row;
foreach(Microsoft.SharePoint.Client.ListItem item in items)
{
row = dt.Rows.Add();
row["From"] = item["From1"].ToString();
row["To"] = item["To"].ToString();
row["Subject"] = item["Subject1"].ToString();
row["Body"] = item["Body1"].ToString();
row["Attachments"] = item["Attachments"].ToString();
row["Sent"] = item["Sent"].ToString();
}
return dt;
}
这与 Retrieve the values from a list to Gridview in SharePoint Webpart? 类似,但具有客户端对象模型方法和对象。
我想在 .aspx 网站上显示一个列表。因此我必须使用 SP 客户端对象模型。
我找到了以下教程,但这没有使用客户端库: http://social.technet.microsoft.com/wiki/contents/articles/30287.binding-gridview-with-sharepoint-list.aspx
到目前为止,我的代码如下所示:
ClientContext clientContext = GetContext(accessToken);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
// Get the email input list.
List inboxList = web.Lists.GetByTitle("InboxList");
Microsoft.SharePoint.Client.ListItemCollection items = inboxList.GetItems(new CamlQuery());
clientContext.Load(inboxList);
clientContext.Load(items, ic => ic.Include(i => i["DisplayName"], i => i["Client_Title"], i => i["HasUniqueRoleAssignments"]));
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem i in items)
{
clientContext.Load(i);
}
clientContext.ExecuteQuery();
oGrid.DataSource = items;
oGrid.DataBind();
但这只显示了列表项集合中的一些 "meta data",请看截图:
如果我使用 oGrid.DataSource = inboxList;
,我会得到一个 InvalidOperationException
,因为数据源不是 IListSource、IEnumerable 或 IDataSource 类型。
如果我使用 oGrid.DataSource = inboxList.DataSource;
我得到一个 PropertyOrFieldNotInitializedException
,但我不知道如何加载这个属性(通过 clientContext.Load 它不起作用)?!
我明白了 - 使用以下代码:
protected void Page_Load(object sender, EventArgs e)
{
...
ClientContext clientContext = GetContext(accessToken);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
// Get the email input list.
List inboxList = web.Lists.GetByTitle("InboxList");
Microsoft.SharePoint.Client.ListItemCollection items = inboxList.GetItems(new CamlQuery());
clientContext.Load(inboxList);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem i in items)
{
clientContext.Load(i);
}
clientContext.ExecuteQuery();
oGrid.DataSource = GetInboxListData(inboxList, items);
oGrid.DataBind();
}
else if (!IsPostBack)
{
Response.Write("Could not find a context token.");
return;
}
}
private DataTable GetInboxListData(List inboxList, Microsoft.SharePoint.Client.ListItemCollection items)
{
DataTable dt = new DataTable();
dt.Columns.Add("From");
dt.Columns.Add("To");
dt.Columns.Add("Subject");
dt.Columns.Add("Body");
dt.Columns.Add("Attachments");
dt.Columns.Add("Sent");
DataRow row;
foreach(Microsoft.SharePoint.Client.ListItem item in items)
{
row = dt.Rows.Add();
row["From"] = item["From1"].ToString();
row["To"] = item["To"].ToString();
row["Subject"] = item["Subject1"].ToString();
row["Body"] = item["Body1"].ToString();
row["Attachments"] = item["Attachments"].ToString();
row["Sent"] = item["Sent"].ToString();
}
return dt;
}
这与 Retrieve the values from a list to Gridview in SharePoint Webpart? 类似,但具有客户端对象模型方法和对象。