cwogle

I am attempting to modify the properties of another webpart when my webpart's button is clicked. The code compiles and runs, and stepping through it shows that the properties do change. However, they don't stay changed. This example attempts to toggle the visibility state, but other properties won't stay changed either. Any help is appreciated.

SPSecurity.RunWithElevatedPrivileges(delegate()

{

// get the site in this context

using (SPSite SiteCollection = new SPSite(mySiteGuid))

{

// get the web in this context

SPWeb myWeb = SiteCollection.OpenWeb(myWebGuid);

myWeb .AllowUnsafeUpdates = true;

// Hide/display another webpart.

Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager mgr = null;

mgr = myWeb.GetLimitedWebPartManager(

"default.aspx",

System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

foreach (System.Web.UI.WebControls.WebParts.WebPart myWebPart in mgr.WebParts)

{

// If this is the webpart we want to modify...

if (myWebPart.Title == "Other Webpart Name")

{

// Toggle the visibility state.

myWebPart.Visible = ! myWebPart.Visible;

myWeb.Update();

break;

}

}

}

});



Re: SharePoint - Development and Programming Change webpart properties programmatically

royjs

try myWebPart.SaveProperties = true;





Re: SharePoint - Development and Programming Change webpart properties programmatically

cwogle

Thanks for your reply. Unfortunately (you know what's coming next!) it did not work. If I try:

this.SaveProperties = true;

then everything is fine. However, I don't need to change the WebPart that is running this code; I need to change another one on the page. When I try:

myWebpart.SaveProperties = true;

the compiler gives me:

Cannot access protected member 'Microsoft.SharePoint.WebPartPages.WebPart.SaveProperties' via a qualifier of type 'Microsoft.SharePoint.WebPartPages.WebPart'; the qualifier must be of type 'myWebPartClassName' (or derived from it)

Since the WebPart I am trying to change is a standard WebPart, and not derived from mine, I can't access SaveProperties.

I feel that I must be missing something obvious. Any ideas





Re: SharePoint - Development and Programming Change webpart properties programmatically

cwogle

I found the answer. Instead of:

myWeb.Update();

you should use:

mgr.SaveChanges(myWebPart);