以编程方式修改主页上的丰富内容



我在SP2010中以编程方式提供站点集合。这个过程工作得很好,但我需要能够在主页上自定义一些文本,这是包含在富文本(不是在web部件)。是否有一种方法可以抓取HTML代码并从代码中修改它?例如,嵌入在主页,我有一个标题和一段文字。我想根据供应请求提供的输入值之一自定义标题。这可能吗?任何建议将不胜感激!

您可以通过以下方式访问内容区:

PublishingWeb pw = null;
//"web" is your SPWeb, didn't include using statement for your SPSite/SPWeb in this sample
if (PublishingWeb.IsPublishingWeb(web))
{
    pw = PublishingWeb.GetPublishingWeb(web); 
}
//todo: add some handling here for if web is not a publishingWeb
//assuming your home page is the default, if not - get the correct page here
SPFile defaultpage = pw.DefaultPage;
if (!(defaultpage.CheckOutType == SPFile.SPCheckOutType.None))
{
   //programatically creating this stuff, so cancel any unexpected checkouts
   defaultpage.UndoCheckOut();
}
defaultpage.CheckOut();
//Field you want to add HTML in (Alternatively, retreive existing data and modify the HTML)
defaultpage.ListItemAllFields["PublishingPageContent"] = "string of HTML";
defaultpage.ListItemAllFields.Update();
defaultpage.CheckIn("My Comment");

最新更新