首页 > 解决方案 > 使用 asp.net Web API 将 SharePoint 列表数据检索到 JSON 格式的数据表中

问题描述

我正在尝试使用 asp.net Web API MVC 应用程序从 SharePoint 列表中获取数据,然后将其加载到数据表中,最后将其作为 JSON 格式的 API 输出提供。

我试图从 SharePoint 列表中获取数据,但 SharePoint 给出null了结果。

我期望将数据加载到我的数据表中,并且 API 应该将该数据作为 JSON 输出提供。

标签: c#asp.net-web-apisharepointdatatable

解决方案


你的问题不清楚。不过,我试过了。

这是我在控制台应用程序中使用的代码。

public static SPListItemCollection GetCollection()
        {
            string listName = 'Your List Name';
            string queryStr = "<Where><Eq><FieldRef Name='Title''/><Value Type='Text'>" + YourValue + "</Value></Eq></Where><OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy>";
            SPListItemCollection oCollection = GetListItemCollectionByQuery(listName, queryStr);

            return oCollection;
        }

        public static SPListItemCollection GetListItemCollectionByQuery(string ListName, string queryText)
        {
            SPListItemCollection spListItemCollection = null;
            string webUrl = Your Site URL;
            try
            {
                using (SPSite oSPsite = new SPSite(webUrl))
                {
                    using (SPWeb oSPWeb = oSPsite.OpenWeb())
                    {
                        string listName = ListName;
                        SPList spList = oSPWeb.Lists[listName];

                        if (spList != null)
                        {
                            SPQuery spQuery = new SPQuery();
                            spQuery.Query = queryText;
                            spListItemCollection = spList.GetItems(spQuery);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            return spListItemCollection;
        }

此外,您需要检查此Get DataTable

我希望它可以帮助:)


推荐阅读