使用客户端对象模型将数据绑定到 Sharepoint 的下拉列表
Databind to dropdownlist from Sharepoint using client object model
情况:
我目前正在通过 .Net 页面将项目添加到共享点列表,但我无法让它们正确显示。
我从列表中获取了所有项目(打印机模型),但在下拉列表(模型)中显示它们并不顺利,列表仅显示 "Microsoft.Sharepoint.Client.ListItem".
如果没有注释掉 datavaluefield 和 datatextvalue,我会收到有关未找到字段的错误消息。尽管已重新创建该列(以确保内部名称正确)并在两个字段中尝试 "value" 和 "key",但结果相同。
坦率地说,我没有想法,用谷歌搜索问题也没有帮助,更何况因为大多数解决方案都引用 SPlist,这不是一个选项。
using (ClientContext context = new ClientContext("[mysite]"))
{
context.Credentials = new SharePointOnlineCredentials(username, password);
Web site = context.Web;
List list = site.Lists.GetByTitle("Printers");
CamlQuery query = CamlQuery.CreateAllItemsQuery();
ListItemCollection collection = list.GetItems(query);
context.Load(collection);
context.ExecuteQuery();
modell.DataSource = collection;
//modell.DataValueField = "Printermodell";
//modell.DataTextField = "Printermodell";
modell.DataBind();
}
感谢任何想法/建议!
示例代码供您参考。
using (ClientContext context = new ClientContext("https://domain.sharepoint.com/sites/Developer/"))
{
string userName = "user.onmicrosoft.com";
string password = "pw";
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials(userName, securePassword);
Web site = context.Web;
List list = site.Lists.GetByTitle("MyList2");
CamlQuery query = CamlQuery.CreateAllItemsQuery();
Microsoft.SharePoint.Client.ListItemCollection collection = list.GetItems(query);
context.Load(collection);
context.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem item in collection)
{
string text = string.Format("{0}",item.FieldValues["Title"]);
modell.Items.Add(new System.Web.UI.WebControls.ListItem(text, item.Id.ToString()));
}
//modell.DataSource = collection;
//modell.DataValueField = "Printermodell";
//modell.DataTextField = "Printermodell";
//modell.DataBind();
}
情况: 我目前正在通过 .Net 页面将项目添加到共享点列表,但我无法让它们正确显示。 我从列表中获取了所有项目(打印机模型),但在下拉列表(模型)中显示它们并不顺利,列表仅显示 "Microsoft.Sharepoint.Client.ListItem".
如果没有注释掉 datavaluefield 和 datatextvalue,我会收到有关未找到字段的错误消息。尽管已重新创建该列(以确保内部名称正确)并在两个字段中尝试 "value" 和 "key",但结果相同。
坦率地说,我没有想法,用谷歌搜索问题也没有帮助,更何况因为大多数解决方案都引用 SPlist,这不是一个选项。
using (ClientContext context = new ClientContext("[mysite]"))
{
context.Credentials = new SharePointOnlineCredentials(username, password);
Web site = context.Web;
List list = site.Lists.GetByTitle("Printers");
CamlQuery query = CamlQuery.CreateAllItemsQuery();
ListItemCollection collection = list.GetItems(query);
context.Load(collection);
context.ExecuteQuery();
modell.DataSource = collection;
//modell.DataValueField = "Printermodell";
//modell.DataTextField = "Printermodell";
modell.DataBind();
}
感谢任何想法/建议!
示例代码供您参考。
using (ClientContext context = new ClientContext("https://domain.sharepoint.com/sites/Developer/"))
{
string userName = "user.onmicrosoft.com";
string password = "pw";
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials(userName, securePassword);
Web site = context.Web;
List list = site.Lists.GetByTitle("MyList2");
CamlQuery query = CamlQuery.CreateAllItemsQuery();
Microsoft.SharePoint.Client.ListItemCollection collection = list.GetItems(query);
context.Load(collection);
context.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem item in collection)
{
string text = string.Format("{0}",item.FieldValues["Title"]);
modell.Items.Add(new System.Web.UI.WebControls.ListItem(text, item.Id.ToString()));
}
//modell.DataSource = collection;
//modell.DataValueField = "Printermodell";
//modell.DataTextField = "Printermodell";
//modell.DataBind();
}