如何用编程方式实现创建一个页面并替换掉站点首页

用SharePoint Designer可以创建一个新页面, 并且指定这个页面为站点首页.

有个朋友问我, 她有几千个站点, 如果都要这么做, 怎么办?

于是我就写了下面的代码段来完成这个动作. 至于如何扩展到上千个站点, 读个配置文件也就可以了.

本代码已经过博主测试通过.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.IO;

namespace QiaoluTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (SPSite osite = new SPSite("http://moss/sites/teamsite/"))
                {
                    using (SPWeb oweb = osite.RootWeb)
                    {
                        // back up the original home page
                        SPFile defaultPage = oweb.Files["default.aspx"];
                        defaultPage.MoveTo("default-old.aspx");

                        // add components to the new custom default page here, if necessary
                        string newFilename = "CustomDefault.aspx";
                        string templateFilename = "bpstd.aspx"; 
                        string hive = SPUtility.GetGenericSetupPath("TEMPLATE\\1033\\STS\\DOCTEMP\\BLANKPGS\\");
                        FileStream stream = new FileStream(hive + templateFilename, FileMode.Open);
                        SPFile newDefaultPage = oweb.Files.Add(newFilename, stream);

                        // move the new default page to default.aspx
                        newDefaultPage.MoveTo("default.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

参考资料

===============

Create a blank webpart page programatically?

http://www.go4sharepoint.com/Forum/create-blank-webpart-page-17285.aspx

原文地址:https://www.cnblogs.com/awpatp/p/2038106.html