从SharePoint下载数据的最佳方式

时间:2013-10-30 15:42:48

标签: c# sharepoint sharepoint-2013 windows-8.1 csom

所以,我正在开发一个与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个语句放在一个语句中,或者更好更快地检索数据吗?

对于Reccord:我正在使用SharePoint CLIENT对象模型。

期待答案!

1 个答案:

答案 0 :(得分:0)

我认为您需要更改基本方法来获取数据。

鉴于您希望从不确定数量的列表中收集数据,在您可以在任何列表中的所有公告之后,您应该使用其他数据访问API,搜索。说真的,搜索对你来说要快得多。

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

Tobias ZimmergrenChris O'Brien有关于REST搜索API的精彩博客文章。这种方法确实有限制,结果只是与上次搜索爬网一样新鲜,但它更快更有效

相关问题