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

Posted 致林

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于SharePoint 站点集和子站点数据互相读取的主要内容,如果未能解决你的问题,请参考以下文章

如何在SharePoint中将Web部件从一个站点放到另一个站点?

SharePoint 删除废弃站点步骤

Sharepoint 子站点用户可以访问父站点

powershell 这会将列表从一个SharePoint站点复制到另一个SharePoint站点

日常运维SharePoint 2013 缺少站点保存为模板选项

powershell 迭代站点中SharePoint子站点中的所有文档。它同时执行站点和所有子站点