代码改变世界

发布网站页面的对象模型操作

2008-02-15 11:41  努力学习的小熊  阅读(848)  评论(2编辑  收藏  举报
 

发布网站页面的对象模型操作

使用对象模型创建发布网站的页面并向其中填写文章页的内容。用代码的好处就是可以批量操作。

         

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Publishing;

using System.IO;

 

 

namespace CreatePublishPage

{

    class Program

    {

        public static void CreateNewPage(SPWeb web)

        {

            // TODO: Replace these variable values with your own

            // values.

            //

            // The URL name of the new page

            string newPageName = "testpage";

            //

            // The comment to set when the page is checked in.

            string checkInComment = "Your check in comments";

 

            // Validate the input parameters.

            if (null == web)

            {

                throw new System.ArgumentNullException("web");

            }

         

 

            // Get the PublishingWeb wrapper for the SPWeb

            // that was passed in.

            PublishingWeb publishingWeb = null;

            if (PublishingWeb.IsPublishingWeb(web))

            {

                publishingWeb = PublishingWeb.GetPublishingWeb(web);

            }

            else

            {

                throw new System.ArgumentException("The SPWeb must be a PublishingWeb", "web");

            }

 

            // Create the new page in the PublishingWeb.

          

            PublishingPageCollection pages = publishingWeb.GetPublishingPages();

 

            PageLayout[] plc = publishingWeb.GetAvailablePageLayouts();

           

            PageLayout pageLayout = plc[0];

            PublishingPage newPage = pages.Add(newPageName+".aspx", pageLayout);

 

            newPage.Title = newPageName;

            newPage.Update();

            // Check the new page in so that other contributors

            // can work on it.

 

            byte[] fs =  newPage.ListItem.File.OpenBinary();

            string s = Encoding.UTF8.GetString(fs);

 

 

            //Edit the page content

            string news = "publishing page content. ";

 

            string ss = s.Replace(@"</mso:PublishingPageLayout>", "</mso:PublishingPageLayout><mso:PublishingPageContent msdt:dt=\"string\">"+news+"</mso:PublishingPageContent><mso:PublishingRollupImage msdt:dt=\"string\"></mso:PublishingRollupImage>");

            byte[] ffs = Encoding.UTF8.GetBytes(ss);

 

 

            newPage.ListItem.File.SaveBinary(ffs);

 

            newPage.ListItem.File.Update();

 

          

           // newPage.ListItem.File.

            newPage.CheckIn(checkInComment);

            publishingWeb.Update();

 

            Console.WriteLine("Success!");

           

        }

 

      

        static void Main(string[] args)

        {

           // CreateNewPage();

            string url = "http://moss:8017/Pages";

 

           SPSite site = new SPSite(url);

           SPWeb web = site.OpenWeb();

 

          CreateNewPage(web);          

        

        }

    }

}