public void CloseWebPart(SPWebPartInstance webPart) { if (webPart == null) { throw new JavaScriptException(this.Engine, "Error", "A web part must be supplied as the first argument."); } m_limitedWebPartManager.CloseWebPart(webPart.WebPart); }
internal static void SetWebPart(string url, SetWebPartStateAction action, string webPartId, string webPartTitle, string webPartZone, string webPartZoneIndex, Hashtable props, bool publish) { using (SPSite site = new SPSite(url)) using (SPWeb web = site.OpenWeb()) // The url contains a filename so AllWebs[] will not work unless we want to try and parse which we don't { bool cleanupContext = false; try { if (HttpContext.Current == null) { cleanupContext = true; HttpRequest httpRequest = new HttpRequest("", web.Url, ""); HttpContext.Current = new HttpContext(httpRequest, new HttpResponse(new StringWriter())); SPControl.SetContextWeb(HttpContext.Current, web); } SPFile file = web.GetFile(url); // file.Item will throw "The object specified does not belong to a list." if the url passed // does not correspond to a file in a list. bool checkIn = false; if (file.InDocumentLibrary) { if (!Utilities.IsCheckedOut(file.Item) || !Utilities.IsCheckedOutByCurrentUser(file.Item)) { file.CheckOut(); checkIn = true; // If it's checked out by another user then this will throw an informative exception so let it do so. } } string displayTitle = string.Empty; WebPart wp = null; SPLimitedWebPartManager manager = null; try { if (!string.IsNullOrEmpty(webPartId)) { wp = Utilities.GetWebPartById(web, url, webPartId, out manager); } else { wp = Utilities.GetWebPartByTitle(web, url, webPartTitle, out manager); if (wp == null) { throw new SPException( "Unable to find specified web part using title \"" + webPartTitle + "\". Try specifying the -id parameter instead (use Get-SPWebPartList to get the ID)"); } } if (wp == null) { throw new SPException("Unable to find specified web part."); } // Set this so that we can add it to the check-in comment. displayTitle = wp.DisplayTitle; if (action == SetWebPartStateAction.Delete) { manager.DeleteWebPart(wp); } else if (action == SetWebPartStateAction.Close) { manager.CloseWebPart(wp); } else if (action == SetWebPartStateAction.Open) { manager.OpenWebPart(wp); } if (action != SetWebPartStateAction.Delete) { string zoneID = manager.GetZoneID(wp); int zoneIndex = wp.ZoneIndex; if (!string.IsNullOrEmpty(webPartZone)) { zoneID = webPartZone; } if (!string.IsNullOrEmpty(webPartZoneIndex)) { zoneIndex = int.Parse(webPartZoneIndex); } manager.MoveWebPart(wp, zoneID, zoneIndex); if (props != null && props.Count > 0) { SetWebPartProperties(wp, props); } manager.SaveChanges(wp); } } finally { if (manager != null) { manager.Web.Dispose(); manager.Dispose(); } if (wp != null) { wp.Dispose(); } if (checkIn) { file.CheckIn("Checking in changes to page due to state change of web part " + displayTitle); } if (publish && file.InDocumentLibrary) { PublishItems pi = new PublishItems(); pi.PublishListItem(file.Item, file.Item.ParentList, false, "Set-SPWebPart", "Checking in changes to page due to state change of web part " + displayTitle, null); } } } finally { if (HttpContext.Current != null && cleanupContext) { HttpContext.Current = null; } } } }