SharePoint Development Bookmark and Share   
 index > SharePoint - Development and Programming > Unable to close the web part dynamically
 

Unable to close the web part dynamically

Hi,

Question-1:

I'm unable to close the webparts from the webpart page in sharepoint using the object model, i'm using the followig code to acomplish this,much appreciate any help.I belive internally it sets properties but could not see the way to set this using the object model.



SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteCollURL))
{
using (SPWeb spWeb = site.AllWebs["Docs"])
{
SPFile spfile = spWeb.GetFile("default.aspx");
if (spfile.Exists)
{
using (SPLimitedWebPartManager spLimitedWebPrtMgr = spfile.GetLimitedWebPartManager(ASPNETWebPart.PersonalizationScope.Shared))
{
for (int l = 0; l < spLimitedWebPrtMgr.WebParts.Count; l++)
{
spLimitedWebPrtMgr.WebParts[l].AllowClose = true; // tried with outsetting this property as well.
spLimitedWebPrtMgr.CloseWebPart(spLimitedWebPrtMgr.WebParts[l]);
spLimitedWebPrtMgr.SaveChanges(spLimitedWebPrtMgr.WebParts[l]);//Robert's suggestion
}
}
}
// spWeb.Update() // doesnot required.

}
}
});



Question-2:

Does any one familar with the javascriptfile(core.js, init.js....etc) and dll is used while webpart closemenu option is selected in the UIto close the webpart on the webpart page?

Vijay Gande  Monday, July 27, 2009 2:41 AM
Maybe you can add
spLimitedWebPrtMgr.SaveChanges(spLimitedWebPrtMgr.WebParts[l]); 
after close web part

Robert in Taipei
  • Marked As Answer byVijay Gande Monday, July 27, 2009 8:50 PM
  •  
Robert Yu  Monday, July 27, 2009 6:51 AM
Maybe you can add
spLimitedWebPrtMgr.SaveChanges(spLimitedWebPrtMgr.WebParts[l]); 
after close web part

Robert in Taipei
  • Marked As Answer byVijay Gande Monday, July 27, 2009 8:50 PM
  •  
Robert Yu  Monday, July 27, 2009 6:51 AM

Hi Robert,

even after placing "spLimitedWebPrtMgr.SaveChanges(spLimitedWebPrtMgr.WebParts[l]);" and "spWeb.Update()" the webpart is not closing.

Vijay Gande  Monday, July 27, 2009 1:48 PM
Have you tried calling spFile.Update()?
My SharePoint Blog - http://www.davehunter.co.uk/blog
Dave Hunter  Monday, July 27, 2009 2:59 PM
spFile.Update() did not help me.
Vijay Gande  Monday, July 27, 2009 6:05 PM

How about if you include

spFile.Publish("modified programmatically");

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfile.publish.aspx

Your code will look like

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteCollURL))
{
using (SPWeb spWeb = site.AllWebs["Docs"])
{
SPFile spfile = spWeb.GetFile("default.aspx");
if (spfile.Exists)
{
using (SPLimitedWebPartManager spLimitedWebPrtMgr = spfile.GetLimitedWebPartManager(ASPNETWebPart.PersonalizationScope.Shared))
{
for (int l = 0; l < spLimitedWebPrtMgr.WebParts.Count; l++)
{
spLimitedWebPrtMgr.WebParts[l].AllowClose = true; // tried with out setting this property as well.
spLimitedWebPrtMgr.CloseWebPart(spLimitedWebPrtMgr.WebParts[l]);
spLimitedWebPrtMgr.SaveChanges(spLimitedWebPrtMgr.WebParts[l]); //Robert's suggestion
}
}
spFile.Update();
spFile.Publish("modified programmatically");

}
// dont need this
//spWeb.Update();

}
}
});


Hope this helps

Dave


My SharePoint Blog - http://www.davehunter.co.uk/blog
Dave Hunter  Monday, July 27, 2009 6:19 PM
try looping in reverse:
<br/>for (int i = spLimitedWebPrtMgr.WebParts.Count; i > 0; i--)
{                               
   spLimitedWebPrtMgr.WebParts[l].AllowClose = true;  // tried with out  setting this property as well. 
   spLimitedWebPrtMgr.CloseWebPart(spLimitedWebPrtMgr.WebParts[i]); 
   spLimitedWebPrtMgr.SaveChanges(spLimitedWebPrtMgr.WebParts[i]);      //Robert's suggestion                          
}          

f00z  Monday, July 27, 2009 7:34 PM

ยท Hi All,

I sincerely appreciate your replies to my question. I was able to resolve the issue by having spLimitedWebPrtMgr.SaveChanges(); as well as testing this piece of code with a new web application .

In my code I was trying to close all the webpart in all web site in a given site collection. The code was failing because ofone ore more error web parts. The error webpart came into picture by having an invalidentries in the web.config safecontrol section referring dll's not in the GAC Or BIN,to identify the issue verified the SharePoint 12 Hive /LOGS and thencreated the

new web application
new site collection
new web site
new sub site
and place few webpart and run the code, it worked.

Regards,
Vijay Gande

Vijay Gande  Monday, July 27, 2009 9:00 PM

You can use google to search for other answers

Custom Search

More Threads

• Adding single Feature Receiver class with feature for Document libraries at various site levels
• Bulk Update User's Name in Profile
• Intermittent Web Part Error
• ListId reference is TSATOK:ListId(Contacts) and not GUID
• dwp files
• Create Gantt view programmatically
• Automatically insert some default webparts?
• Dynamically add control to webpart through property?
• How to make a sharepoint control readonly?
• SharePoint Web Services and Active Directory