SharePoint 站点集和子站点数据互相读取

1.站点集中可以使用SPSite.AllWeb,然后遍历所有站点的isRootWeb,根据siteTemplate取得需要的子站点。

 

/// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            var web = SPControl.GetContextWeb(Context);

            var subSite = new SPSite(web.Site.ID).AllWebs;

            var list = new List<ProjectOverviewContent>();

            //Foreach sub site
            foreach (SPWeb site in subSite)
            { 
                if (site.IsRootWeb) continue;
                if (site.WebTemplate != WebTemplate.PM.ToString()) continue;

                try
                {
                    var content = site.Lists.TryGetList(ProjectOverviewContent.ProjectListName);
                    if (content == null || content.Items.Count == 0) continue;

                    var link = new ProjectOverviewContent
                    {
                        //get sitrUrl
                        SubSiteUrl = site.Url,
                        //get site created time
                        SiteCreateTime = site.Created,
                    };

                    //Foreach sub site list => ProjectContent
                    foreach (SPListItem item in content.Items)
                    {
                        link.ProjectName = item[ProjectOverviewContent.ProjectNameMapping].TryToString();
                        link.StartDate = item[ProjectOverviewContent.StartDateMapping].TryToString().SplitDate();
                        link.EndDate = item[ProjectOverviewContent.EndDateMapping].TryToString().SplitDate();
                        link.ServiceLine = item[ProjectOverviewContent.ServiceLineMapping].TryToString();
                        link.Industry = item[ProjectOverviewContent.IndustryMapping].TryToString();
                    }

                    content = site.Lists.TryGetList(ProjectOverviewContent.MemberListName);
                    if (content == null)
                    {
                        list.Add(link);
                        continue;
                    }

                    //Foreach sub site list => MemberContent
                    foreach (SPListItem item in content.Items)
                    {
                        link.Position = item[ProjectOverviewContent.PositionMapping].TryToString();

                        if (link.Position == Position.MIC.ToString())
                        {
                            link.MIC = item[ProjectOverviewContent.MemberNameMapping].TryToString();
                        }
                        else if (link.Position == Position.PIC.ToString())
                        {
                            link.PIC = item[ProjectOverviewContent.MemberNameMapping].TryToString();
                        }
                        else if (link.Position == Position.PM.ToString())
                        {
                            link.PM = item[ProjectOverviewContent.MemberNameMapping].TryToString();
                        }
                    }

                    list.Add(link);
                }
                catch (SPException spException)
                {
                    LogHelper.Error(spException);
                }
                catch (Exception ex)
                {
                    LogHelper.ShowError(lblErrorMessage, ex);
                }
            }

            list.Sort();

            tblProjectOverview.DataSource = list.Take(10);
            tblProjectOverview.DataBind();
        }
View Code

 

2.子站点读取站点集数据可以通过New SPSite("url").RootWeb获取。

 

public SPWeb RootSite = new SPSite(ProjectContent.SiteUrlMapping).RootWeb;

/// <summary>
        /// Gets the project information from root site.
        /// </summary>
        public List<ServiceOfferingContent> GetProjectInfo(string projectCode)
        {
            var siteContent = RootSite.Lists[ServiceOfferingContent.ListName];
            if (siteContent == null || siteContent.Items.Count == 0) return null;

            var list = new List<ServiceOfferingContent>();

            foreach (SPListItem item in siteContent.Items)
            {
                var link = new ServiceOfferingContent()
                {
                    ProjectCode = item[ServiceOfferingContent.ProjectCodeMapping].TryToString(),
                    ServiceLine = item[ServiceOfferingContent.ServiceLineMapping].TryToString(),
                    Industry = item[ServiceOfferingContent.IndustryMapping].TryToString(),
                    Id = item[ServiceOfferingContent.IdMapping].TryToString(),
                    Title = item[ServiceOfferingContent.TitleMapping].TryToString(),
                    Contents = item[ServiceOfferingContent.ContentsMapping].TryToString(),
                    ThumbnailUrl = item[ServiceOfferingContent.ThumbnailUrlMapping].TryToString(),
                    HeadImageUrl = item[ServiceOfferingContent.HeadImageUrlMapping].TryToString(),
                    ContentsImageUrl = item[ServiceOfferingContent.ContentsImageUrlMapping].TryToString(),
                    Summary = item[ServiceOfferingContent.SummaryMapping].TryToString(),
                };
                if (link.ProjectCode != projectCode) continue;
                
                list.Add(link);
                return list;
            }
            return null;
        }
View Code

 

posted @ 2016-09-02 11:24  致林  阅读(984)  评论(1编辑  收藏  举报