public void GetTableData(TableCallback callback) { if (powerWebPartException != null) { return; } if (runspace != null) { if (functions.ContainsKey(FunctionTableProvider)) { EnsureChildControls(); try { Command cmd = new Command(FunctionTableProvider); Pipeline pipe = runspace.CreatePipeline(); pipe.Commands.Add(cmd); Collection <PSObject> results = InvokePipeline(pipe); if (results.Count > 0) { callback.Invoke((ICollection)results); } } catch (Exception ex) { powerWebPartException = new PowerWebPartException(FunctionTableProvider, ex); } } } }
public void SetTableConnectionInterface(IWebPartTable provider) { if (powerWebPartException != null) { return; } tableProviderToConsume = provider; provider.GetTableData(delegate(ICollection table) { if (functions.ContainsKey(FunctionTableConsumer)) { EnsureChildControls(); try { Command cmd = new Command(FunctionTableConsumer); cmd.Parameters.Add(new CommandParameter("collection", table)); cmd.Parameters.Add(new CommandParameter("schema", tableProviderToConsume.Schema)); Pipeline pipe = runspace.CreatePipeline(); pipe.Commands.Add(cmd); InvokePipeline(pipe); } catch (Exception ex) { powerWebPartException = new PowerWebPartException(FunctionTableConsumer, ex); } } }); }
protected void RefreshIntervalElapsed(object sender, EventArgs e) { if (powerWebPartException != null) { return; } EnsureChildControls(); if (functions.ContainsKey(FunctionRefresh)) { try { Command cmd = new Command(FunctionRefresh); Pipeline pipe = runspace.CreatePipeline(); pipe.Commands.Add(cmd); InvokePipeline(pipe); } catch (Exception ex) { powerWebPartException = new PowerWebPartException(FunctionRefresh, ex); } } }
protected void EventDispatcher(object sender, EventArgs args) { if (powerWebPartException != null) { return; } Control ctrl = sender as Control; if (eventMappingDictionary.ContainsKey(ctrl)) { try { Command cmd = new Command(eventMappingDictionary[ctrl]); cmd.Parameters.Add(new CommandParameter("sender", sender)); cmd.Parameters.Add(new CommandParameter("args", args)); Pipeline pipe = runspace.CreatePipeline(); pipe.Commands.Add(cmd); InvokePipeline(pipe); } catch (Exception ex) { powerWebPartException = new PowerWebPartException("event dispatcher", ex); } } }
protected override void OnUnload(EventArgs e) { if (functions.ContainsKey(FunctionUnload)) { try { Command cmd = new Command(FunctionUnload); Pipeline pipe = runspace.CreatePipeline(); pipe.Commands.Add(cmd); InvokePipeline(pipe); } catch (Exception ex) { if (powerWebPartException == null) { powerWebPartException = new PowerWebPartException(FunctionUnload, ex); } } } runspace.Close(); runspace.Dispose(); base.OnUnload(e); }
protected override void OnPreRender(EventArgs e) { if (powerWebPartException != null) { return; } if (functions.ContainsKey(FunctionBind)) { try { Command cmd = new Command(FunctionBind); Pipeline pipe = runspace.CreatePipeline(); pipe.Commands.Add(cmd); InvokePipeline(pipe); } catch (Exception ex) { powerWebPartException = new PowerWebPartException(FunctionBind, ex); } } base.OnPreRender(e); }
protected override void Render(System.Web.UI.HtmlTextWriter writer) { if (powerWebPartException == null) { if (functions.ContainsKey(FunctionRender)) { try { Command cmd = new Command(FunctionRender); cmd.Parameters.Add(new CommandParameter("writer", writer)); Pipeline pipe = runspace.CreatePipeline(); pipe.Commands.Add(cmd); InvokePipeline(pipe); } catch (Exception ex) { powerWebPartException = new PowerWebPartException(FunctionRender, ex); } } else { if (powerWebPartException == null) { base.Render(writer); } } } if (powerWebPartException != null) { writer.Write("<h3>Error in " + powerWebPartException.Function + "</h3>"); writer.Write(powerWebPartException.InnerException.Message); } }
protected override void CreateChildControls() { if (powerWebPartException != null) { return; } if (functions.ContainsKey(FunctionCreateControls)) { try { Command cmd = new Command(FunctionCreateControls); cmd.Parameters.Add("controls", Controls); Pipeline pipe = runspace.CreatePipeline(); pipe.Commands.Add(cmd); InvokePipeline(pipe); } catch (Exception ex) { powerWebPartException = new PowerWebPartException(FunctionCreateControls, ex); } } base.CreateChildControls(); }
private void Initialize() { eventHandlerDelegate = new EventHandler(EventDispatcher); runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); SPContext ctx = SPContext.Current; runspace.SessionStateProxy.SetVariable("this", this); runspace.SessionStateProxy.SetVariable("viewState", this.ViewState); runspace.SessionStateProxy.SetVariable("spContext", ctx); runspace.SessionStateProxy.SetVariable("site", ctx.Site); runspace.SessionStateProxy.SetVariable("web", ctx.Web); runspace.SessionStateProxy.SetVariable("list", ctx.List); runspace.SessionStateProxy.SetVariable("item", ctx.Item); runspace.SessionStateProxy.SetVariable("param1", this.Parameter1); runspace.SessionStateProxy.SetVariable("param2", this.Parameter2); runspace.SessionStateProxy.SetVariable("param3", this.Parameter3); runspace.SessionStateProxy.SetVariable("param4", this.Parameter4); if (this.Page != null) { runspace.SessionStateProxy.SetVariable("isPostBack", this.Page.IsPostBack); runspace.SessionStateProxy.SetVariable("page", this.Page); } if (String.IsNullOrEmpty(Script) == false) { try { Pipeline pipe = null; if (!String.IsNullOrEmpty(PredefinedFunctions)) { pipe = runspace.CreatePipeline(PredefinedFunctions); InvokePipeline(pipe); } if (!String.IsNullOrEmpty(Script)) { pipe = runspace.CreatePipeline(Script); InvokePipeline(pipe); pipe = runspace.CreatePipeline("get-childitem function:\\"); Collection <PSObject> result = InvokePipeline(pipe); foreach (PSObject obj in result) { FunctionInfo func = (FunctionInfo)obj.BaseObject; functions.Add(func.Name.ToLower(), func); } } } catch (Exception ex) { powerWebPartException = new PowerWebPartException("Intitialization", ex); } } }
public void RegisterForEvent(Control ctrl, string eventName, string psCallbackFunction) { if (powerWebPartException != null) { return; } try { Type ctrlType = ctrl.GetType(); EventInfo eventInfo = ctrlType.GetEvent(eventName); eventInfo.AddEventHandler(ctrl, eventHandlerDelegate); eventMappingDictionary.Add(ctrl, psCallbackFunction); } catch (Exception ex) { powerWebPartException = new PowerWebPartException("RegisterForEvent", ex); } }
protected override void Render(HtmlTextWriter writer) { if (powerWebPartException == null) { try { base.Render(writer); } catch (Exception ex) { powerWebPartException = new PowerWebPartException(FunctionRender, ex); } } if (powerWebPartException != null) { if (functions.ContainsKey(FunctionError)) { try { Command cmd = new Command(FunctionError); cmd.Parameters.Add("exception", powerWebPartException.InnerException); cmd.Parameters.Add("writer", writer); Pipeline pipe = runspace.CreatePipeline(); pipe.Commands.Add(cmd); InvokePipeline(pipe); powerWebPartException = null; } catch (Exception ex) { powerWebPartException = new PowerWebPartException(FunctionError, ex); } } if (powerWebPartException != null) { writer.Write("<b>Error on " + powerWebPartException.Function + "</b><br/>"); writer.Write(powerWebPartException.InnerException.Message); } } }
protected override void CreateChildControls() { if (powerWebPartException != null) { return; } try { renderControl = new DynamicControl(this); if (AjaxEnabled) { updatePanel = new UpdatePanel(); updatePanel.ID = "updatePanel"; string progressTemplate = runspace.SessionStateProxy.GetVariable(VariableProgressTemplate) as string; bool progressDynamicLayout = (bool)runspace.SessionStateProxy.GetVariable(VariableProgressDynamicLayout); int progressDisplayAfter = (int)runspace.SessionStateProxy.GetVariable(VariableProgressDisplayAfter); int refreshInterval = (int)runspace.SessionStateProxy.GetVariable(VariableRefreshInterval); if (String.IsNullOrEmpty(progressTemplate) == false) { updateProgress = new UpdateProgress(); updateProgress.ID = "updateProgress"; updateProgress.DynamicLayout = progressDynamicLayout; updateProgress.AssociatedUpdatePanelID = updatePanel.ClientID; updateProgress.ProgressTemplate = new PowerWebPartProgressTemplate(progressTemplate); updateProgress.DisplayAfter = progressDisplayAfter; this.Controls.Add(updateProgress); } if (refreshInterval > 0) { timer = new Timer(); timer.Interval = refreshInterval; timer.Tick += new EventHandler <EventArgs>(RefreshIntervalElapsed); updatePanel.ContentTemplateContainer.Controls.Add(timer); } this.Controls.Add(updatePanel); } if (functions.ContainsKey(FunctionCreateControls)) { Command cmd = new Command(FunctionCreateControls); cmd.Parameters.Add("controls", renderControl.Controls); Pipeline pipe = runspace.CreatePipeline(); pipe.Commands.Add(cmd); InvokePipeline(pipe); } if (AjaxEnabled) { this.updatePanel.ContentTemplateContainer.Controls.Add(renderControl); } else { this.Controls.Add(renderControl); } } catch (Exception ex) { powerWebPartException = new PowerWebPartException(FunctionCreateControls, ex); } base.CreateChildControls(); }
protected void Initialize() { runspace = RunspaceFactory.CreateRunspace(); runspace.Open(); eventHandlerDelegate = new EventHandler(EventDispatcher); SPContext ctx = SPContext.Current; runspace.SessionStateProxy.SetVariable("this", this); runspace.SessionStateProxy.SetVariable("viewState", this.ViewState); runspace.SessionStateProxy.SetVariable("spContext", ctx); runspace.SessionStateProxy.SetVariable("httpContext", HttpContext.Current); runspace.SessionStateProxy.SetVariable("site", ctx.Site); runspace.SessionStateProxy.SetVariable("web", ctx.Web); runspace.SessionStateProxy.SetVariable("list", ctx.List); runspace.SessionStateProxy.SetVariable("item", ctx.Item); runspace.SessionStateProxy.SetVariable(VariableAjaxEnabled, false); runspace.SessionStateProxy.SetVariable(VariableProgressTemplate, null); runspace.SessionStateProxy.SetVariable(VariableProgressDynamicLayout, true); runspace.SessionStateProxy.SetVariable(VariableProgressDisplayAfter, 500); runspace.SessionStateProxy.SetVariable(VariableRefreshInterval, 0); runspace.SessionStateProxy.SetVariable("param1", this.Parameter1); runspace.SessionStateProxy.SetVariable("param2", this.Parameter2); runspace.SessionStateProxy.SetVariable("param3", this.Parameter3); runspace.SessionStateProxy.SetVariable("param4", this.Parameter4); if (this.Page != null) { runspace.SessionStateProxy.SetVariable("isPostBack", this.Page.IsPostBack); runspace.SessionStateProxy.SetVariable("page", this.Page); } if (String.IsNullOrEmpty(Script) == false) { try { Pipeline pipe = null; if (!String.IsNullOrEmpty(PredefinedFunctions)) { pipe = runspace.CreatePipeline(PredefinedFunctions); InvokePipeline(pipe); } if (!String.IsNullOrEmpty(Script)) { pipe = runspace.CreatePipeline(Script); InvokePipeline(pipe); pipe = runspace.CreatePipeline("get-childitem function:\\"); Collection <PSObject> result = InvokePipeline(pipe); foreach (PSObject obj in result) { FunctionInfo func = (FunctionInfo)obj.BaseObject; functions.Add(func.Name.ToLower(), func); } } if (AjaxEnabled && this.Page != null) { EnsureScriptManager(); EnsureUpdatePanelFixup(); } } catch (Exception ex) { powerWebPartException = new PowerWebPartException("Intitialization", ex); } } }