设置SharePoint页面的HTML内容

时间:2012-01-27 11:49:53

标签: sharepoint

我有一个团队网站,我尝试以编程方式修改其中的页面。此代码将Web部件添加到页面中:

using (SPSite site = new SPSite("http://myserver"))
{
    using (var web = site.OpenWeb("/test16"))
    {

        foreach(var l in web.Lists)
        {
            Console.WriteLine("list : " + l);
        }

        SPList list = web.Lists["Shared Documents"];
        // Instantiate the web part
        ListViewWebPart wp = new ListViewWebPart();
        wp.ZoneID = "Left";
        wp.ListName = list.ID.ToString("B").ToUpper();
        wp.ViewGuid = list.DefaultView.ID.ToString("B").ToUpper();

        SPFile file = web.GetFile("/test16/SitePages/Home.aspx");
        SPLimitedWebPartManager lwpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
        SPLimitedWebPartCollection webParts = lwpm.WebParts;
        lwpm.AddWebPart(wp, "Left", 0);
    }
}

Console.WriteLine("Press any key to continue...");
Console.ReadLine();

如何修改页面的HTML内容?

3 个答案:

答案 0 :(得分:0)

您可以将LiteralControl添加到您的Web部件,并使用以下HTML填充它:

this.Controls.Add(new LiteralControl("<br /"));

答案 1 :(得分:0)

尝试SPFile.CheckOutSPFile.CheckIn

SPFile file = web.GetFile("/test16/SitePages/Home.aspx");
file.CheckOut();
using (SPLimitedWebPartManager lwpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
    try
    {
        SPLimitedWebPartCollection webParts = lwpm.WebParts;
        lwpm.AddWebPart(wp, "Left", 0);
    }
    finally
    {
        lwpm.Web.Dispose();
    }
}       
file.CheckIn("checkin");        

答案 2 :(得分:0)

如果您需要修改纯HTML内容,最好使用cutom IHttpHandler(包装在dll中)并通过web.config将其连接到SharePoint。也允许修改动态内容,如AJAX响应。