繁体   English   中英

Retrieve a SharePoint list data into a datatable in JSON format using asp.net web API

[英]Retrieve a SharePoint list data into a datatable in JSON format using asp.net web API

I am trying to get data from a SharePoint list using asp.net web API MVC application, then load it to a datatable and finally give that as API output as JSON format.

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

我期待数据加载到我的数据表中,API 应该将该数据作为 JSON output 提供。

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

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

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

我希望它可以帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM