2

我正在使用 Sharepoint 2010 对象模型。我正在尝试检索自定义列表的内容。一切正常,除非我尝试检索选择字段。

当我尝试检索选择字段时,出现 PropertyOrFieldNotInitializedException 异常...

这是我正在使用的代码:

            ClientContext clientContext = new ClientContext("https://mysite");
            clientContext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("aaa", bbb");
            clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;

            List list = clientContext.Web.Lists.GetByTitle("mylist");
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View/>";

            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();

            foreach (ListItem listItem in listItems)
            {

                listBoxControl1.Items.Add(listItem["Assigned_x0020_Company"]);

            }

谢谢你的帮助!

埃里克

4

2 回答 2

3
var list = clientContext.Web.Lists.GetByTitle(listName);
clientContext.ExecuteQuery();

clientContext.Load(list.Fields, fields => fields.Include(field => field.Title));
clientContext.ExecuteQuery();

foreach (var field in list.Fields)
{
    if (field.Title == "YourChoiceFieldName")
    {
        clientContext.Load(field);
        clientContext.ExecuteQuery();
        return ((FieldChoice) field).Choices;
    }
}
于 2012-11-08T12:26:38.287 回答
0

当您在代码中读取 ChoiceField 时,它将返回一个包含所选选项的字符串数组。例如,如果您在创建时在列的选择框中输入:“公司 1”、“公司 2”、“公司 3”,如果用户选择选项 1 和 2,则代码中返回的数组将包含“公司 1 ”和“公司 2”。您必须将代码更改为以下内容:

         foreach (ListItem listItem in listItems)
        {
            string[] values = (string[])listItem["Assigned_x0020_Company"];
            foreach(string s in values)
            {
               listBoxControl1.Items.Add(s);
             }

        }
于 2012-02-08T14:10:58.460 回答