以编程方式更新webpart属性不会更新

时间:2012-05-31 23:25:48

标签: c# sharepoint web-parts

我正在尝试以编程方式更新某些webpart的标题。为此,我从网站获取所有页面,获取每个页面的webparts,然后首先打印出当前标题,更改它然后再次打印出来以验证标题是否已正确更改。然而,如果我事后检查,我仍然会看到webparts的旧标题......我错过了什么?

代码如下:

... getting the pages for the site...
SPFile ofile = page.File;

SPLimitedWebPartManager wpColl = ofile.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.User);
int cont = wpColl.WebParts.Count;

for (int i = 0; i < cont; i++)
{
    System.Web.UI.WebControls.WebParts.WebPart wp1 = wpColl.WebParts[i];

Console.WriteLine(" - Title : " + wp1.Title);

wp1.Title = "test" + i;

ofile.Update();
Console.WriteLine(" - New title: " + wp1.Title);

page.Update();
site.Update();                             
}

谢谢!

2 个答案:

答案 0 :(得分:3)

可能应该使用SPLimitedWebPartManager.Save(WebPart webPart)方法来保存修改后的属性

答案 1 :(得分:1)

在进行更新之前,您是否尝试设置site.AllowUnsafeUpdates = true