public static XmlDocument GetPersonalizationXml(HttpContext context, Page pageNode) { XmlDocument xml = new XmlDocument(); xml.AppendChild(xml.CreateElement("webPartZones")); string virtualPath = CreateVirtualPath(pageNode.Path); PageBase page = InstantiatePage(context, virtualPath, pageNode); page.PreLoad += delegate { WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(page); WebPartZoneCollection webPartZones = wpm.Zones; foreach (WebPartZone zone in webPartZones) { XmlElement zoneElement = xml.CreateElement("webPartZone"); XmlAttribute zoneId = xml.CreateAttribute("id"); zoneId.Value = zone.ID; zoneElement.SetAttributeNode(zoneId); xml.DocumentElement.AppendChild(zoneElement); WebPartCollection webParts = zone.WebParts; foreach (WebPart webPart in webParts) { if (!webPart.IsStatic) { XmlDocument xmlFragment = new XmlDocument(); using (StringWriter sw = new StringWriter()) { using (XmlWriter writer = new XmlTextWriter(sw)) { webPart.ExportMode = WebPartExportMode.All; // Force exporting of all information wpm.ExportWebPart(webPart, writer); writer.Flush(); } xmlFragment.LoadXml(sw.ToString()); } zoneElement.AppendChild(xml.ImportNode(xmlFragment.FirstChild.FirstChild, true)); } } } }; ExecutePage(context, virtualPath, page, pageNode, true); return(xml); }
// </snippet5> // <snippet6> protected void Button5_Click(object sender, EventArgs e) { Label1.Text = String.Empty; WebPartZoneCollection zoneCollection = mgr.Zones; foreach (WebPartZone zone in zoneCollection) { if (zone.WebPartVerbRenderMode == WebPartVerbRenderMode.Menu) { zone.WebPartVerbRenderMode = WebPartVerbRenderMode.TitleBar; } else { zone.WebPartVerbRenderMode = WebPartVerbRenderMode.Menu; } } }
public static void SetPersonalizationFromXml(HttpContext context, Page pageNode, XmlDocument xml, out string errorMessage) { string error = String.Empty; string virtualPath = CreateVirtualPath(pageNode.Path); PageBase page = InstantiatePage(context, virtualPath, pageNode); page.PreLoad += delegate { WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(page); WebPartZoneCollection webPartZones = wpm.Zones; foreach (WebPartZone zone in webPartZones) { foreach (WebPart part in zone.WebParts) { if (!part.IsStatic) { wpm.DeleteWebPart(part); } } XmlNode zoneDescriptionNode = xml.SelectNodes(@"//*[@id='" + zone.ID + "']")[0]; XmlElement zoneDescription = zoneDescriptionNode as XmlElement; if (zoneDescription != null) { foreach (XmlElement webPartDescription in zoneDescription.GetElementsByTagName("webPart")) { string webPartXml = String.Empty; using (StringWriter sw = new StringWriter()) { using (XmlWriter writer = new XmlTextWriter(sw)) { writer.WriteStartElement("webParts"); webPartDescription.WriteTo(writer); writer.WriteEndElement(); writer.Flush(); } webPartXml = sw.ToString(); } using (StringReader sr = new StringReader(webPartXml)) { using (XmlReader reader = new XmlTextReader(sr)) { WebPart part = wpm.ImportWebPart(reader, out error); wpm.AddWebPart(part, zone, zone.WebParts.Count); } } } } } }; //TODO: Clever exception handling ExecutePage(context, virtualPath, page, pageNode, true); errorMessage = error; }
/// <summary> /// Gets the zone by its ID. /// </summary> /// <param name="zones"></param> /// <param name="zoneID"></param> /// <returns></returns> private WebPartZone GetZoneByID(WebPartZoneCollection zones, string zoneID) { foreach (WebPartZone zone in zones) { if (zone.ID == zoneID) return zone; } throw new InvalidOperationException("The target zone to which the webpart panel was dragged does not exist."); }