Esempio n. 1
0
        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 tmpPipe = CreatePipeline();
                    tmpPipe.Commands.Add(cmd);
                    InvokePipeline(tmpPipe, false);
                }
                catch (Exception ex)
                {
                    powerWebPartException = new PowerControlException("event dispatcher", ex);
                }
            }
        }
Esempio n. 2
0
        public void ImportPowerModule(string name, bool noCache)
        {
            string moduleScript = PowerWebPartHelper.LoadPowerModule(name, noCache);

            if (moduleScript == null)
            {
                throw new PowerControlException("ImportPowerModule", new FileNotFoundException("Module could not be found!"));
            }

            try
            {
                Pipeline tmpPipe = CreatePipeline();
                tmpPipe.Commands.AddScript(moduleScript.Trim());
                InvokePipeline(tmpPipe, false);

                tmpPipe = CreatePipeline();
                tmpPipe.Commands.AddScript("get-childitem function:\\");
                Collection <PSObject> result = InvokePipeline(tmpPipe, false);

                foreach (PSObject obj in result)
                {
                    FunctionInfo func = (FunctionInfo)obj.BaseObject;
                    if (functions.ContainsKey(func.Name.ToLower()) == false)
                    {
                        functions.Add(func.Name.ToLower(), func);
                    }
                }
            }
            catch (Exception ex)
            {
                powerWebPartException = new PowerControlException("ImportPowerModule", ex);
            }
        }
Esempio n. 3
0
        public void GetTableData(TableCallback callback)
        {
            if (powerWebPartException != null)
            {
                return;
            }

            if (runspace == null)
            {
                Initialize();
            }

            if (functions.ContainsKey(PowerWebPartConstants.FunctionTableProvider))
            {
                EnsureChildControls();

                try
                {
                    Command cmd = new Command(PowerWebPartConstants.FunctionTableProvider);

                    Pipeline tmpPipe = CreatePipeline();
                    tmpPipe.Commands.Add(cmd);
                    Collection <PSObject> results = InvokePipeline(tmpPipe, false);

                    if (results.Count > 0)
                    {
                        callback.Invoke((ICollection)results);
                    }
                }
                catch (Exception ex)
                {
                    powerWebPartException = new PowerControlException(PowerWebPartConstants.FunctionTableProvider, ex);
                }
            }
        }
Esempio n. 4
0
        internal bool FireApplyChanges()
        {
            if (powerWebPartException != null)
            {
                return(false);
            }

            if (functions.ContainsKey(PowerWebPartConstants.FunctionOnApplyChanges))
            {
                try
                {
                    Command cmd = new Command(PowerWebPartConstants.FunctionOnApplyChanges);

                    Pipeline tmpPipe = CreatePipeline();
                    tmpPipe.Commands.Add(cmd);
                    Collection <PSObject> results = InvokePipeline(tmpPipe, false);
                    if (results.Count > 0 && results[results.Count - 1].BaseObject is bool)
                    {
                        return((bool)results[results.Count - 1].BaseObject);
                    }
                    else
                    {
                        return(true);
                    }
                }
                catch (Exception ex)
                {
                    powerWebPartException = new PowerControlException(PowerWebPartConstants.FunctionOnApplyChanges, ex);
                }
            }

            return(true);
        }
Esempio n. 5
0
        protected override void OnUnload(EventArgs e)
        {
            if (functions.ContainsKey(PowerWebPartConstants.FunctionOnUnload))
            {
                try
                {
                    Command cmd = new Command(PowerWebPartConstants.FunctionOnUnload);

                    Pipeline tmpPipe = CreatePipeline();
                    tmpPipe.Commands.Add(cmd);
                    InvokePipeline(tmpPipe, false);
                }
                catch (Exception ex)
                {
                    if (powerWebPartException == null)
                    {
                        powerWebPartException = new PowerControlException(PowerWebPartConstants.FunctionOnUnload, ex);
                    }
                }
            }

            if (debugHost != null)
            {
                debugHost.EndDebugSession();
            }

            if (runspace != null)
            {
                runspace.Close();
                runspace.Dispose();
            }

            base.OnUnload(e);
        }
Esempio n. 6
0
        protected override void OnPreRender(EventArgs e)
        {
            if (powerWebPartException != null)
            {
                return;
            }

            if (functions.ContainsKey(PowerWebPartConstants.FunctionOnPreRender))
            {
                try
                {
                    Command cmd = new Command(PowerWebPartConstants.FunctionOnPreRender);

                    Pipeline tmpPipe = CreatePipeline();
                    tmpPipe.Commands.Add(cmd);
                    InvokePipeline(tmpPipe, false);
                }
                catch (Exception ex)
                {
                    powerWebPartException = new PowerControlException(PowerWebPartConstants.FunctionOnPreRender, ex);
                }
            }


            base.OnPreRender(e);
        }
Esempio n. 7
0
        internal void AjaxAutoRefreshIntervalElapsed(object sender, EventArgs e)
        {
            if (powerWebPartException != null)
            {
                return;
            }

            EnsureChildControls();

            if (functions.ContainsKey(PowerWebPartConstants.FunctionAjaxRefresh))
            {
                try
                {
                    Command cmd = new Command(PowerWebPartConstants.FunctionAjaxRefresh);

                    Pipeline tmpPipe = CreatePipeline();
                    tmpPipe.Commands.Add(cmd);
                    InvokePipeline(tmpPipe, false);
                }
                catch (Exception ex)
                {
                    powerWebPartException = new PowerControlException(PowerWebPartConstants.FunctionAjaxRefresh, ex);
                }
            }
        }
Esempio n. 8
0
        protected override void CreateChildControls()
        {
            if (powerWebPartException != null)
            {
                return;
            }

            try
            {
                renderControl = new DynamicControl(this);

                if (functions.ContainsKey(PowerWebPartConstants.FunctionCreateControls))
                {
                    Command cmd = new Command(PowerWebPartConstants.FunctionCreateControls);
                    cmd.Parameters.Add("controls", renderControl.Controls);

                    Pipeline tmpPipe = CreatePipeline();
                    tmpPipe.Commands.Add(cmd);
                    InvokePipeline(tmpPipe, false);
                }

                this.Controls.Add(renderControl);
            }
            catch (Exception ex)
            {
                powerWebPartException = new PowerControlException(PowerWebPartConstants.FunctionCreateControls, ex);
            }

            base.CreateChildControls();
        }
Esempio n. 9
0
        protected override void RenderContents(HtmlTextWriter writer)
        {
            System.IO.StringWriter content = new System.IO.StringWriter();

            HtmlTextWriter customWriter = new HtmlTextWriter(content);

            if (powerControl != null)
            {
                try
                {
                    foreach (Control ctrl in this.Controls)
                    {
                        ctrl.RenderControl(customWriter);
                    }

                    writer.Write(content.ToString());
                }
                catch (PowerControlException pex)
                {
                    writer.Write(pex.ToHtmlString());
                }
                catch (Exception ex)
                {
                    PowerControlException pex = new PowerControlException("Render", ex);
                    writer.Write(pex.ToHtmlString());
                }
            }
        }
Esempio n. 10
0
        public void QueryConnections()
        {
            if (rowProviderToConsume != null)
            {
                rowProviderToConsume.GetRowData(delegate(object row)
                {
                    if (functions.ContainsKey(PowerWebPartConstants.FunctionRowConsumer))
                    {
                        EnsureChildControls();

                        try
                        {
                            Command cmd = new Command(PowerWebPartConstants.FunctionRowConsumer);
                            cmd.Parameters.Add(new CommandParameter("row", row));
                            cmd.Parameters.Add(new CommandParameter("schema", rowProviderToConsume.Schema));

                            Pipeline tmpPipe = CreatePipeline();
                            tmpPipe.Commands.Add(cmd);
                            InvokePipeline(tmpPipe, false);
                        }
                        catch (Exception ex)
                        {
                            powerWebPartException = new PowerControlException(PowerWebPartConstants.FunctionRowConsumer, ex);
                        }
                    }
                });
            }

            if (tableProviderToConsume != null)
            {
                tableProviderToConsume.GetTableData(delegate(ICollection table)
                {
                    if (functions.ContainsKey(PowerWebPartConstants.FunctionTableConsumer))
                    {
                        EnsureChildControls();

                        try
                        {
                            Command cmd = new Command(PowerWebPartConstants.FunctionTableConsumer, true);
                            cmd.Parameters.Add(new CommandParameter("table", table));
                            cmd.Parameters.Add(new CommandParameter("schema", tableProviderToConsume.Schema));

                            Pipeline tmpPipe = CreatePipeline();
                            tmpPipe.Commands.Add(cmd);
                            InvokePipeline(tmpPipe, false);
                        }
                        catch (Exception ex)
                        {
                            powerWebPartException = new PowerControlException(PowerWebPartConstants.FunctionTableConsumer, ex);
                        }
                    }
                });
            }
        }
Esempio n. 11
0
        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 PowerControlException("RegisterForEvent", ex);
            }
        }
Esempio n. 12
0
        protected override void Render(HtmlTextWriter writer)
        {
            if (powerWebPartException == null)
            {
                try
                {
                    base.Render(writer);
                }
                catch (Exception ex)
                {
                    powerWebPartException = new PowerControlException(PowerWebPartConstants.FunctionRender, ex);
                }
            }

            if (powerWebPartException != null)
            {
                if (functions.ContainsKey(PowerWebPartConstants.FunctionOnError))
                {
                    try
                    {
                        Command cmd = new Command(PowerWebPartConstants.FunctionOnError);
                        cmd.Parameters.Add("exception", powerWebPartException.InnerException);
                        cmd.Parameters.Add("writer", writer);

                        Pipeline tmpPipe = CreatePipeline();
                        tmpPipe.Commands.Add(cmd);
                        InvokePipeline(tmpPipe, false);
                        powerWebPartException = null;
                    }
                    catch (Exception ex)
                    {
                        powerWebPartException = new PowerControlException(PowerWebPartConstants.FunctionOnError, ex);
                    }
                }

                if (powerWebPartException != null)
                {
                    throw powerWebPartException;
                }
            }
        }
Esempio n. 13
0
        internal void FireSyncChanges()
        {
            if (powerWebPartException != null)
            {
                return;
            }

            if (functions.ContainsKey(PowerWebPartConstants.FunctionOnSyncChanges))
            {
                try
                {
                    Command cmd = new Command(PowerWebPartConstants.FunctionOnSyncChanges);

                    Pipeline tmpPipe = CreatePipeline();
                    tmpPipe.Commands.Add(cmd);
                    InvokePipeline(tmpPipe, false);
                }
                catch (Exception ex)
                {
                    powerWebPartException = new PowerControlException(PowerWebPartConstants.FunctionOnSyncChanges, ex);
                }
            }
        }
Esempio n. 14
0
        protected void Initialize()
        {
            try
            {
                if (String.IsNullOrEmpty(Script) == false)
                {
                    VerifySignature();
                }

                if (Debug && PowerWebPartHelper.IsPowerUser)
                {
                    debugHost = new PowerWebPartDebugHost(this);
                    runspace  = RunspaceFactory.CreateRunspace(debugHost);
                    debugHost.StartDebugSession();
                }
                else
                {
                    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("webpart", WebPart);
                runspace.SessionStateProxy.SetVariable("debug", Debug);

                if (this.Page != null)
                {
                    runspace.SessionStateProxy.SetVariable("scriptManager", ScriptManager.GetCurrent(this.Page));
                    runspace.SessionStateProxy.SetVariable("isPostBack", this.Page.IsPostBack);
                    runspace.SessionStateProxy.SetVariable("page", this.Page);
                    runspace.SessionStateProxy.SetVariable("webPartManager", SPWebPartManager.GetCurrentWebPartManager(Page));
                }


                if (String.IsNullOrEmpty(Script) == false)
                {
                    string coreScript = PowerWebPartHelper.CoreScript;

                    if (!String.IsNullOrEmpty(coreScript))
                    {
                        Pipeline tmpPipe = CreatePipeline();
                        tmpPipe.Commands.AddScript(coreScript);
                        if (Debug && PowerWebPartHelper.IsPowerUser && String.IsNullOrEmpty(DebugOptions) == false)
                        {
                            tmpPipe.Commands.AddScript("Set-PSDebug " + DebugOptions);
                        }
                        InvokePipeline(tmpPipe, false);
                    }

                    if (!String.IsNullOrEmpty(Script))
                    {
                        LoadScriptFunctions();
                    }
                }
            }
            catch (Exception ex)
            {
                powerWebPartException = new PowerControlException("Initialization", ex);
            }
        }
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            needsRefresh.RenderControl(writer);
            writer.Write("<table border=\"0\">");

            SPUser currentUser = SPContext.Current.Web.CurrentUser;

            if (PowerWebPartHelper.IsPowerUser)
            {
                writer.Write("<tr><td><div class=\"UserSectionTitle\">PowerShell Scripts</div></td></tr>");

                writer.Write("<tr>");
                writer.Write(@"<td style=""vertical-align:middle"">");
                writer.Write(string.Format(@"<table border=""0""><tr><td><a id=""btnPopupScript"" href=""javascript:powerWebPartEditorShowPopup('{0}')"" >Script ({1} lines)</a></td>
                    <td><span style=""display:none"" id=""btnPowerGuiLauncher""><a href=""javascript:startPowerGuiLaucher('{0}', '{1}')""><img src=""/_layouts/images/iLoveSharePoint/PowerWebPartPowerGuiIcon3.gif"" alt=""Edit in PowerGUI"" border=""0""/></a></span></td></tr></table>",
                                           script.ClientID, String.IsNullOrEmpty(script.Value) ? "0" : (System.Text.RegularExpressions.Regex.Matches(script.Value, "\n").Count + 1).ToString())
                             , this.Zone.FindControl("MSOTlPn_AppBtn").ClientID);
                script.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("</tr>");

                writer.Write("<tr>");
                writer.Write(@"<td style=""vertical-align:middle"">");
                writer.Write(string.Format(@"<table border=""0""><tr><td><a id=""btnPopupEditorScript"" href=""javascript:powerWebPartEditorShowPopup('{0}')"" >Editor Script ({1} lines)</a></td>
                    <td><span style=""display:none"" id=""btnPowerGuiLauncher4Editor""><a href=""javascript:startPowerGuiLaucherEditor('{0}', '{1}')""><img src=""/_layouts/images/iLoveSharePoint/PowerWebPartPowerGuiIcon3.gif"" alt=""Edit in PowerGUI"" border=""0""/></a></span></td></tr></table>",
                                           editorScript.ClientID, String.IsNullOrEmpty(editorScript.Value) ? "0" : (System.Text.RegularExpressions.Regex.Matches(editorScript.Value, "\n").Count + 1).ToString())
                             , this.Zone.FindControl("MSOTlPn_AppBtn").ClientID);
                editorScript.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("</tr>");

                //debug
                writer.Write("<tr><td><div class=\"UserSectionTitle\">Debug</div></td></tr>");

                writer.Write("<tr><td><div>Enabled</div></td></tr>");
                writer.Write("<tr><td>");
                chkDebug.RenderControl(writer);
                writer.Write("</tr></td>");

                writer.Write("<tr><td><div>Options</div></td></tr>");
                writer.Write("<tr><td>");
                txtDebugOptions.RenderControl(writer);
                writer.Write("</tr></td>");

                writer.Write("<tr><td><div>Console Url</div></td></tr>");
                writer.Write("<tr><td>");
                txtDebugUrl.RenderControl(writer);
                writer.Write("</tr></td>");

                writer.Write(String.Format("<tr><td><a href=\"javascript:setToClientIP('{0}','{1}');\"/>Set to my IP</a></td></tr>"
                                           , txtDebugUrl.ClientID, this.Page.Request.UserHostAddress));
                writer.Write(String.Format("<tr><td><a href=\"javascript:pingDebugConsole('{0}');\"/>Ping Debug Console</a></td></tr>"
                                           , txtDebugUrl.ClientID));

                writer.Write("<tr><td>&nbsp;</td></tr>");
            }

            //config
            writer.Write("<tr><td><div class=\"UserSectionTitle\">Configuration</div></td></tr>");

            if (powerControl == null)
            {
                if (_signingException != null)
                {
                    writer.Write("<tr><td>Invalid Script Signing!</td></tr>");
                }

                RenderParameter("Parameter1", parameter1, parameterDescription1, currentUser, writer);
                RenderParameter("Parameter2", parameter2, parameterDescription2, currentUser, writer);
                RenderParameter("Parameter3", parameter3, parameterDescription3, currentUser, writer);
                RenderParameter("Parameter4", parameter4, parameterDescription4, currentUser, writer);
                RenderParameter("Parameter5", parameter5, parameterDescription5, currentUser, writer);
                RenderParameter("Parameter6", parameter6, parameterDescription6, currentUser, writer);
                RenderParameter("Parameter7", parameter7, parameterDescription7, currentUser, writer);
                RenderParameter("Parameter8", parameter8, parameterDescription8, currentUser, writer);
            }
            else
            {
                try
                {
                    writer.Write("<tr>");
                    writer.Write("<td>");
                    powerControl.RenderControl(writer);
                    writer.Write("</td>");
                    writer.Write("</tr>");
                }
                catch (PowerControlException pex)
                {
                    writer.Write(pex.ToHtmlString());
                }
                catch (Exception ex)
                {
                    PowerControlException pex = new PowerControlException("Render", ex);
                    writer.Write(pex.ToHtmlString());
                }
            }

            writer.Write("</table>");
        }