从 SharePoint 下载数据的最佳方式

本文关键字:最佳 方式 数据 SharePoint 下载 | 更新日期: 2023-09-27 18:37:25

因此,我正在开发一个与SharePoint 2013通信的Windows 8.1应用程序。

目前,我使用以下方式下载数据:

    public async Task<IList<NewsSite>> GetGroups(string vaultResource)
    {
        _clientContext.Credentials = new NetworkCredential(UserName, PassWord);
        SP.Web site = _clientContext.Web;
        ListCollection announcementListCollection = site.Lists;
        _clientContext.Load(announcementListCollection);
        List<NewsItem> NewsItems = new List<NewsItem>();
        //load all news items wich are announcement items for all lists in the current site.
        _clientContext.Load(announcementListCollection);
        _clientContext.ExecuteQuery();

        foreach (List sharePointGroup in announcementListCollection)
        {
            var ctypes = sharePointGroup.ContentTypes;
            _clientContext.Load(ctypes);
            _clientContext.ExecuteQuery();
            if (ctypes.Where(c => c.Name == "Announcement").Count() == 1)
            {
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<View><RowLimit>8</RowLimit></View>";
                ListItemCollection newsListColl = sharePointGroup.GetItems(camlQuery);
                _clientContext.Load(newsListColl,
                    eachItem => eachItem.Include(item => item.Id, item => item["Title"], item => item["Body"]));
                _clientContext.ExecuteQuery();
                foreach (ListItem NewsItem in newsListColl)
                {
                    NewsItem newsItem = new NewsItem();
                    newsItem.Id = NewsItem.Id;
                    newsItem.Title = (string) NewsItem["Title"];
                    newsItem.Content = (string) NewsItem["Body"];
                    newsItem.ListHolder = sharePointGroup.Title;
                    string tempImageString = GetImageInHTML((string) NewsItem["Body"]);
                    var rgx1 = new Regex("http");
                    if (tempImageString != null)
                    {
                        var match = rgx1.Match(tempImageString);
                        if (match.Success)
                        {
                            newsItem.Image = Regex.Replace(tempImageString, "&#58;", ":");
                        }
                        else
                        {
                            string pattern2 = @"http(s)?://(www'.)?'w*((.'w*)|(-*'w*))?('.'w*)?";
                            var rgx2 = new Regex(pattern2);
                            string imgUrl = rgx2.Match(_siteUrl).ToString() + tempImageString;

                            newsItem.Image = imgUrl;
                        }
                    }
                    if (tempImageString == null)
                    {
                        newsItem.Image = null;
                    }
                    NewsItems.Add(newsItem);
                }
            }
        }
        var newsItemsBySite =
                NewsItems.GroupBy(x => x.ListHolder).Select(x => new NewsSite {Title = x.Key, Items = x.ToList()});
        return newsItemsBySite.ToList();
    }

通过这种方式,应用程序对每个列表项的 SharePoint 网站进行 3 次调用,以获取所需的数据。

我试图将所有 load 语句放在一个语句中,但没有成功。

有没有人知道如何将 3 个语句放在一个语句中或更好、更快的方法来检索数据?

对于 Reccord:我使用的是 SharePoint CLIENT 对象模型。

期待答案!

从 SharePoint 下载数据的最佳方式

我认为您需要改变在此处获取数据的基本方法。

鉴于您希望从不确定数量的列表中收集数据,因此在所有公告中,您可能在任何列表中,您应该使用另一个数据访问 API,搜索。说真的,搜索对你来说会快得多。

要获取网站中的所有公告,您的查询将变得简单明了,您只需向http://SiteUrl/_api/search/query?querytext=%27ContentTypeId:0x01*%27提出请求只需在您的网络浏览器中点击它,然后查看结果的形状。

Tobias Zimmergren和Chris O'Brien在REST Search API上发表了很棒的博客文章。这种方法确实存在一个限制,即结果仅与上次搜索爬网一样新鲜,但它更快、更高效。