Aidenn
Aidenn

Reputation: 559

Programatically update a webpart property doesn't get updated

I'm trying to programatically update the title of some webparts. To do this, I am getting all the pages from the site, getting the webparts for each page, and first print out the current title, change it and then print it out again to verify the title was correctly changed. Nevertheless, if I afterwards check, I still see the old title for the webparts... What am I missing?

The code is as follows:

... 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();                             
}

Thank you!

Upvotes: 1

Views: 3788

Answers (2)

Alexander
Alexander

Reputation: 1297

May be you should use SPLimitedWebPartManager.Save(WebPart webPart) method for saving modified properties

Upvotes: 3

mishod
mishod

Reputation: 1040

Did you try to set site.AllowUnsafeUpdates = true before you do the update?

Upvotes: 1

Related Questions