public void MoveWebPart(SPWebPartInstance webPart, string zoneId, int zoneIndex) { if (webPart == null) { throw new JavaScriptException(this.Engine, "Error", "A web part must be supplied as the first argument."); } m_limitedWebPartManager.MoveWebPart(webPart.WebPart, zoneId, zoneIndex); }
public static void MoveWebPart(SPWeb web, string pageUrl, string webPartName, string zoneID, int zoneIndex) { try { string fullPageUrl = string.Format("{0}/{1}", web.Url.TrimEnd('/'), pageUrl.TrimStart('/')); SPLimitedWebPartManager mgr = web.GetLimitedWebPartManager( fullPageUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared); foreach (System.Web.UI.WebControls.WebParts.WebPart wp in mgr.WebParts) { if (wp.Title.Equals(webPartName.Replace(".webpart", ""))) { mgr.MoveWebPart(wp, zoneID, zoneIndex); mgr.SaveChanges(wp); break; } } } catch (Exception) { } }
public static void DeployWebPartsToPage(SPLimitedWebPartManager webPartManager, IEnumerable <WebPartDefinition> webpartDefinitions, Action <WebPart> onUpdating, Action <WebPart> onUpdated) { foreach (var webpartModel in webpartDefinitions) { var site = webPartManager.Web.Site; var webPartInstance = ResolveWebPartInstance(site, webPartManager, webpartModel); if (onUpdating != null) { onUpdating(webPartInstance); } // webpartModel.InvokeOnModelUpdatingEvents<WebPartDefinition, AspWebPart.WebPart>(webPartInstance); var needUpdate = false; var targetWebpartType = webPartInstance.GetType(); foreach (System.Web.UI.WebControls.WebParts.WebPart existingWebpart in webPartManager.WebParts) { if (existingWebpart.ID == webpartModel.Id && existingWebpart.GetType() == targetWebpartType) { webPartInstance = existingWebpart; needUpdate = true; break; } } // process common properties webPartInstance.Title = webpartModel.Title; webPartInstance.ID = webpartModel.Id; // faking context for CQWP deployment var webDeploymentAction = new Action(delegate() { // webpartModel.InvokeOnModelUpdatedEvents<WebPartDefinition, AspWebPart.WebPart>(webPartInstance); if (!needUpdate) { if (onUpdating != null) { onUpdating(webPartInstance); } if (onUpdated != null) { onUpdated(webPartInstance); } webPartManager.AddWebPart(webPartInstance, webpartModel.ZoneId, webpartModel.ZoneIndex); } else { if (webPartInstance.ZoneIndex != webpartModel.ZoneIndex) { webPartManager.MoveWebPart(webPartInstance, webpartModel.ZoneId, webpartModel.ZoneIndex); } if (onUpdating != null) { onUpdating(webPartInstance); } if (onUpdated != null) { onUpdated(webPartInstance); } webPartManager.SaveChanges(webPartInstance); } }); if (SPContext.Current == null) { SPContextExtensions.WithFakeSPContextScope(webPartManager.Web, webDeploymentAction); } else { webDeploymentAction(); } } }
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; } } } }
public static void DeployWebPartToPage(SPLimitedWebPartManager webPartManager, WebPartDefinition webpartModel, Action<WebPart> onUpdating, Action<WebPart> onUpdated, Action<WebPart, WebPartDefinition> onProcessCommonProperties) { var site = webPartManager.Web.Site; var webPartInstance = ResolveWebPartInstance(site, webPartManager, webpartModel); if (onUpdating != null) onUpdating(webPartInstance); // webpartModel.InvokeOnModelUpdatingEvents<WebPartDefinition, AspWebPart.WebPart>(webPartInstance); var needUpdate = false; var targetWebpartType = webPartInstance.GetType(); foreach (System.Web.UI.WebControls.WebParts.WebPart existingWebpart in webPartManager.WebParts) { if (existingWebpart.ID == webpartModel.Id && existingWebpart.GetType() == targetWebpartType) { webPartInstance = existingWebpart; needUpdate = true; break; } } // process common properties webPartInstance.Title = webpartModel.Title; webPartInstance.ID = webpartModel.Id; if (onProcessCommonProperties != null) onProcessCommonProperties(webPartInstance, webpartModel); // faking context for CQWP deployment var webDeploymentAction = new Action(delegate() { // webpartModel.InvokeOnModelUpdatedEvents<WebPartDefinition, AspWebPart.WebPart>(webPartInstance); if (!needUpdate) { if (onUpdating != null) onUpdating(webPartInstance); if (onUpdated != null) onUpdated(webPartInstance); webPartManager.AddWebPart(webPartInstance, webpartModel.ZoneId, webpartModel.ZoneIndex); } else { if (webPartInstance.ZoneIndex != webpartModel.ZoneIndex) webPartManager.MoveWebPart(webPartInstance, webpartModel.ZoneId, webpartModel.ZoneIndex); if (onUpdating != null) onUpdating(webPartInstance); if (onUpdated != null) onUpdated(webPartInstance); webPartManager.SaveChanges(webPartInstance); } }); if (SPContext.Current == null) SPContextExtensions.WithFakeSPContextScope(webPartManager.Web, webDeploymentAction); else webDeploymentAction(); }