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

时间:2011-10-21 12:48:40

标签: c# sharepoint-2010

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

1 个答案:

答案 0 :(得分:0)

您可以按如下方式访问内容区域:

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");