Spostamento di un WebPart ad una zona diversa in una pagina
-
16-10-2019 - |
Domanda
Come faccio a livello di codice muovo un WebPart da una zona all'altra in una pagina?
Non ho più siti che voglio collegare attraverso ogni sito e aggiornare la pagina.
Soluzione
Use the SPLimitedWebPartManager.MoveWebPart method:
string zoneId = "TopZone"; // ID of the WebPartZone control
string zoneIndex = 0; // Location of web part within zone
string webPartTitle = "Sales Tips"; // Title of web part
string filename = "default.aspx"; // Filename relative to SPWeb object
using (SPLimitedWebPartManager webPartManager =
site.GetLimitedWebPartManager(filename, PersonalizationScope.Shared))
{
try
{
foreach (WebPart webPart in webPartManager.WebParts)
{
if (webPart.Title == webPartTitle)
{
webPartManager.MoveWebPart(webPart, zoneId, zoneIndex);
webPartManager.SaveChanges(webPart);
break;
}
}
}
finally
{
webPartManager.Web.Dispose();
}
}
Altri suggerimenti
In addition to the answer from Alex, be careful when you iterate your site collections and sites. If you do this from for example a console application where you have to establish your own context, you will run out of memory very fast if you forget to dispose your SPSite and SPWeb objects while iterating (eg. sites.AllWebs).
read and understand the disposing guidelines on MSDN:Best Practices: Using Disposable Windows SharePoint Services Objects
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange