SharePoint ListItemCollection.GetById returns empty ListItem



所以我试图从共享点服务器ListItem检索数据

唯一的问题是这里的这部分:

收集。GetById(itemId(

返回一个完全空的列表项,而不是与 id 对应的列表项

当我使用调试器检查时,该项确实在集合中,并且所有数据都在那里。

我该如何纠正这个问题,还是我在这里遗漏了一些重要的东西?

        public Dictionary<string, object> GetPendingEmployeeItem(int itemId)
        {
            var list = _app.Sharepoint.Web.Lists.GetByTitle("New Employee");
            var query = new CamlQuery();
            query.ViewXml = "<View></View>";
            var collection = list.GetItems(query);
            _app.Sharepoint.Load(collection, items => items.Include(
                item => item.Id,
                item => item.DisplayName,
                item => item.FieldValuesAsText));
            _app.Sharepoint.ExecuteQuery();
            return ConvertToDictionary(collection.GetById(itemId));
        }
        private Dictionary<string, object> ConvertToDictionary(ListItem item)
        {
            var dic = new Dictionary<string, object>();
            foreach (var pair in item.FieldValuesAsText.FieldValues)
            {
                dic.Add(pair.Key, pair.Value);
            }
            return dic;
        }

所以我找到了解决方案。 我不知道为什么它以前不起作用,但我想只要它现在有效就可以了。

我没有使用给定的方法GetById/GetItemById,我只是用First解决了它们。要么我错过了什么,要么这些方法被破坏了。

public Dictionary<string, object> GetPendingEmployeeItem(int itemId)
    {
        var list = _app.Sharepoint.Web.Lists.GetByTitle("New Employee");
        var query = new CamlQuery();
        query.ViewXml = "<View></View>";
        var collection = list.GetItems(query);
        _app.Sharepoint.Load(collection);
        _app.Sharepoint.ExecuteQuery();
        return collection.First(item => item.Id.Equals(itemId)).FieldValues;
    }

最新更新