/// <summary> /// Runs the specified command. /// </summary> /// <param name="command">The command.</param> /// <param name="keyValues">The key values.</param> /// <param name="output">The output.</param> /// <returns></returns> public override int Execute(string command, StringDictionary keyValues, out string output) { output = string.Empty; string url = Params["url"].Value; if (url != null) url = url.TrimEnd('/'); Logger.LogFile = Params["logfile"].Value; Logger.Verbose = !Params["quiet"].UserTypedIn; Common.WebParts.ReplaceWebPartContent.Settings settings = new Common.WebParts.ReplaceWebPartContent.Settings(); settings.SearchString = Params["searchstring"].Value; settings.ReplaceString = Params["replacestring"].Value; settings.WebPartName = Params["webpartname"].Value; settings.Publish = Params["publish"].UserTypedIn; settings.Test = Params["test"].UserTypedIn; settings.UnsafeXml = Params["unsafexml"].UserTypedIn; string scope = Params["scope"].Value.ToLowerInvariant(); if (scope == "farm") { foreach (SPService svc in SPFarm.Local.Services) { if (!(svc is SPWebService)) continue; foreach (SPWebApplication webApp in ((SPWebService)svc).WebApplications) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(webApp, settings); } } } else if (scope == "webapplication") { SPWebApplication webApp = SPWebApplication.Lookup(new Uri(url)); Common.WebParts.ReplaceWebPartContent.ReplaceValues(webApp, settings); } else if (scope == "site") { using (SPSite site = new SPSite(url)) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(site, settings); } } else if (scope == "web") { using (SPSite site = new SPSite(url)) using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)]) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(web, settings); } } else if (scope == "page") { using (SPSite site = new SPSite(url)) using (SPWeb web = site.OpenWeb()) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(web, web.GetFile(url), settings); } } return (int)ErrorCodes.NoError; }
protected override void InternalProcessRecord() { bool test = false; ShouldProcessReason reason; if (!base.ShouldProcess(null, null, null, out reason)) { if (reason == ShouldProcessReason.WhatIf) { test = true; } } if (test) Logger.Verbose = true; Common.WebParts.ReplaceWebPartContent.Settings settings = new Common.WebParts.ReplaceWebPartContent.Settings(); settings.SearchString = SearchString; settings.ReplaceString = ReplaceString; settings.WebPartName = WebPartName; settings.Publish = Publish.IsPresent; settings.Test = test; settings.UnsafeXml = UnsafeXml; switch (ParameterSetName) { case "WebApplication": SPWebApplication webApp1 = WebApplication.Read(); if (webApp1 == null) throw new SPException("Web Application not found."); Common.WebParts.ReplaceWebPartContent.ReplaceValues(webApp1, settings); break; case "Site": using (SPSite site = Site.Read()) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(site, settings); } break; case "Web": using (SPWeb web = Web.Read()) { try { Common.WebParts.ReplaceWebPartContent.ReplaceValues(web, settings); } finally { web.Site.Dispose(); } } break; case "Page": SPFile file = Page.Read(); try { Common.WebParts.ReplaceWebPartContent.ReplaceValues(file.Web, file, settings); } finally { file.Web.Dispose(); file.Web.Site.Dispose(); } break; default: SPFarm farm = Farm.Read(); foreach (SPService svc in farm.Services) { if (!(svc is SPWebService)) continue; foreach (SPWebApplication webApp2 in ((SPWebService)svc).WebApplications) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(webApp2, settings); } } break; } }
/// <summary> /// Runs the specified command. /// </summary> /// <param name="command">The command.</param> /// <param name="keyValues">The key values.</param> /// <param name="output">The output.</param> /// <returns></returns> public override int Execute(string command, StringDictionary keyValues, out string output) { output = string.Empty; string url = Params["url"].Value; if (url != null) { url = url.TrimEnd('/'); } Logger.LogFile = Params["logfile"].Value; Logger.Verbose = !Params["quiet"].UserTypedIn; Common.WebParts.ReplaceWebPartContent.Settings settings = new Common.WebParts.ReplaceWebPartContent.Settings(); settings.SearchString = Params["searchstring"].Value; settings.ReplaceString = Params["replacestring"].Value; settings.WebPartName = Params["webpartname"].Value; settings.Publish = Params["publish"].UserTypedIn; settings.Test = Params["test"].UserTypedIn; settings.UnsafeXml = Params["unsafexml"].UserTypedIn; string scope = Params["scope"].Value.ToLowerInvariant(); if (scope == "farm") { foreach (SPService svc in SPFarm.Local.Services) { if (!(svc is SPWebService)) { continue; } foreach (SPWebApplication webApp in ((SPWebService)svc).WebApplications) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(webApp, settings); } } } else if (scope == "webapplication") { SPWebApplication webApp = SPWebApplication.Lookup(new Uri(url)); Common.WebParts.ReplaceWebPartContent.ReplaceValues(webApp, settings); } else if (scope == "site") { using (SPSite site = new SPSite(url)) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(site, settings); } } else if (scope == "web") { using (SPSite site = new SPSite(url)) using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)]) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(web, settings); } } else if (scope == "page") { using (SPSite site = new SPSite(url)) using (SPWeb web = site.OpenWeb()) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(web, web.GetFile(url), settings); } } return((int)ErrorCodes.NoError); }
protected override void InternalProcessRecord() { bool test = false; ShouldProcessReason reason; if (!base.ShouldProcess(null, null, null, out reason)) { if (reason == ShouldProcessReason.WhatIf) { test = true; } } if (test) { Logger.Verbose = true; } Common.WebParts.ReplaceWebPartContent.Settings settings = new Common.WebParts.ReplaceWebPartContent.Settings(); settings.SearchString = SearchString; settings.ReplaceString = ReplaceString; settings.WebPartName = WebPartName; settings.Publish = Publish.IsPresent; settings.Test = test; settings.UnsafeXml = UnsafeXml; switch (ParameterSetName) { case "WebApplication": SPWebApplication webApp1 = WebApplication.Read(); if (webApp1 == null) { throw new SPException("Web Application not found."); } Common.WebParts.ReplaceWebPartContent.ReplaceValues(webApp1, settings); break; case "Site": using (SPSite site = Site.Read()) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(site, settings); } break; case "Web": using (SPWeb web = Web.Read()) { try { Common.WebParts.ReplaceWebPartContent.ReplaceValues(web, settings); } finally { web.Site.Dispose(); } } break; case "Page": SPFile file = Page.Read(); try { Common.WebParts.ReplaceWebPartContent.ReplaceValues(file.Web, file, settings); } finally { file.Web.Dispose(); file.Web.Site.Dispose(); } break; default: SPFarm farm = Farm.Read(); foreach (SPService svc in farm.Services) { if (!(svc is SPWebService)) { continue; } foreach (SPWebApplication webApp2 in ((SPWebService)svc).WebApplications) { Common.WebParts.ReplaceWebPartContent.ReplaceValues(webApp2, settings); } } break; } }