2011-10-21 40 views
0

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

回答

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

谢谢你的快速反应。经过一些额外的挖掘,我被告知我们没有实施发布,所以这对我来说不是一个可行的解决方案。听起来,我们将把我们的改变融入到新的页面布局中。感谢您清晰,简明的回答,希望它最终能够帮助别人。 – user1007118