2

所以,我正在开发一个与 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();

    }

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

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

有谁知道如何将这 3 个语句放在一个语句中,或者以一种更好更快的方式来检索数据?

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

期待答案!

4

1 回答 1

0

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

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

要获得站点中的所有公告,您的查询将变得简单而简单,您http://SiteUrl/_api/search/query?querytext=%27ContentTypeId:0x01*%27 只需在 Web 浏览器中发出请求并查看结果的形状即可。

Tobias ZimmergrenChris O'Brien发表了关于唱 REST Search API 的精彩博客文章。这种方法确实有局限性,即结果仅与上次搜索爬网一样新鲜,但它更快更有效

于 2014-04-25T07:00:19.920 回答