locked
How to communicate with Stsadm.exe in sharepoint? RRS feed

  • Question

  • Hi to all,
    I created one new ASPX page for interact with Stsadm.exe . By using Process we can run the files know.
    So I used  same to run STSADM.EXE . I tried to display result which I got from STSADM.EXE . But I unable to do that. But it is working for other exe like diskpart.exe, net command...
    Same code running in default web site. If I used this code in sharepoint it is not Displaying the result for STSADM.EXE .
    But in Default web site i am getting out for STSADM.EXE also.
    Here is my code:

    <%@ Page Language="C#" AutoEventWireup="true" %>
    <%@ Import Namespace="System.Text" %>
    <%@ Import Namespace="System.Diagnostics" %>
    <%@ Import Namespace="Microsoft.SharePoint" %>
    <%@ Import Namespace="Microsoft.SharePoint.Administration"%>

    <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
        Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    <%@ Register TagPrefix="AdminControls" Namespace="Microsoft.SharePoint.WebControls"
        Assembly="Microsoft.SharePoint.ApplicationPages.Administration" %>
    <script runat="server">

            protected void Page_Load(object sender, EventArgs e)
            {
                try
                {
                    Page.EnableViewState = true;
                    if (!IsPostBack)
                    {
                        AppConsole cmd = new AppConsole();

                        Response.Write("<BR>"+DateTime.Now);
                        Response.Write("stsadm:");
                        Response.Write("<TEXTAREA>");
                        Response.Write(cmd.Start("stsadm", "-o enumsites -url http://hostingsp01:27605"));
                        Response.Write("</TEXTAREA>");
                        cmd.Exit();

                       
                        Response.Write("diskpart:");
                        //Response.Write(cmd.Start("diskpart", string.Empty));
                        Response.Write("<BR>");
                        cmd.Exit();

                        Response.Write("<BR>");
                        Response.Write("stsadm:");
                        Response.Write("<TEXTAREA>");
                        Response.Write(cmd.Start("stsadm", "-o enumsites -url http://localhost:27605"));
                        Response.Write("</TEXTAREA>");
                        cmd.Exit();
                    }
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
              
            }
       




        class AppConsole
        {
            ProcessStartInfo _psinfo;
            Process _process;

            public AppConsole() { }

            public AppConsole(string command)
            {
                Start(command, String.Empty);
            }

            public string Start(string executable, string arguments)
            {
               // try
                //{
                    _psinfo = new ProcessStartInfo();
                    _psinfo.WindowStyle = ProcessWindowStyle.Maximized;
                    _psinfo.FileName = executable;
                    _psinfo.Arguments = arguments;
                    _psinfo.RedirectStandardInput = true;
                    _psinfo.RedirectStandardOutput = true;
                    _psinfo.UseShellExecute = false;
                    _process = Process.Start(_psinfo);
                    _process.StandardInput.AutoFlush = true;
                    _process.WaitForExit();            
                    return GetOutput();
               // }
               // catch
                //{
                 //   return "error";
               // }
            }

            public string Execute(string command)
            {
                _process.StandardInput.WriteLine(command);
                string result = GetOutput();
                return result;
            }

            private string GetOutput()
            {
                //try
               // {
                    string result = System.Environment.NewLine;
                    StringBuilder sb = new StringBuilder();
                    //while (!result.Contains("</"))
                    //{
                    //System.Threading.Thread.Sleep(10000);
                    sb.Append(_process.StandardOutput.ReadLine());               
                    while (_process.StandardOutput.Peek() >= 10)
                    {
                        sb.Append((char)_process.StandardOutput.Read());
                    }
                    result = sb.ToString();
                   // }

                    return result;
               // }
               // catch
              //  {
              //      return "error";
                //}

            }

            public void Exit()
            {
                _process.Close();
            }


        }

      
    </script>


    <html>

    <body>

    <form runat="server">


    <%--<div style="font-size:16px;font-weight:bold;">Web Applications</div>

    <AdminControls:AdministrationDataSourceControl runat="server"
                ID="WebApplicationDS" ViewName="SelectWebApplication">
                <selectparameters>
                    <asp:QueryStringParameter Name="QueryStringMode" QueryStringField="Mode" />
                </selectparameters>
            </AdminControls:AdministrationDataSourceControl>
               
            <SharePoint:SPGridView id="GvWebApplicationsItems" runat="server" autogeneratecolumns="false" width="100%"
                        allowsorting="True" allowpaging="True" pagesize="15" datasourceid="WebApplicationDS"                 
                        datakeynames="Id,Port">
              <AlternatingRowStyle CssClass="ms-alternating" />
              <Columns>
                <asp:ButtonField
                    ButtonType="Link"
                    CommandName="Select"
                    HeaderText="Name"
                    SortExpression="Name"
                    DataTextField="Name"/>
              </Columns>
            </SharePoint:SPGridView>

    <div style="font-size:16px;font-weight:bold;">Sites</div>
       <table>
       <tr>
       <td align="left" valign="top" style="width:250px">
    <AdminControls:AdministrationDataSourceControl runat="server"
                ID="SiteDS" ViewName="SelectSite">
                <SelectParameters>
                                <asp:ControlParameter
                        Name="WebApplicationId"
                        ControlId="GvWebApplicationsItems"
                        PropertyName="SelectedValue" />
                    <asp:QueryStringParameter Name="Mode" QueryStringField="Mode" />
                </SelectParameters>
            </AdminControls:AdministrationDataSourceControl>
            <SharePoint:SPGridView runat="server"
                 ID="GvItems123"
                 AutoGenerateColumns="false"
                 width="250px"
                 AllowSorting="True"
                 DataSourceId="SiteDS"
                 AllowPaging="True"
                 PageSize="10"
                 EnabledViewState="false"             
                 DataKeyNames="Id,Url"
                 EmptyDataText="Please select web application">
              <AlternatingRowStyle CssClass="ms-alternating" />
              <Columns>
                <asp:ButtonField
                    ButtonType="Link"
                    CommandName="Select"
                    HeaderText="Sites"
                    SortExpression="Url"
                    DataTextField="Url" />
              </Columns>
            </SharePoint:SPGridView>      
          
           </td>
           <td align="left" valign="top">
           Details
           <AdminControls:AdministrationDataSourceControl runat="server"
             ID="SiteDetailDS" ViewName="SiteDetail">
                <SelectParameters>
                    <asp:ControlParameter
                        Name="SiteId"
                        ControlId="GvItems123"
                        PropertyName="SelectedValue" />
                </SelectParameters>
            </AdminControls:AdministrationDataSourceControl>
            <asp:DetailsView runat="server"
                ID="DvSite"
                DataSourceID="SiteDetailDS"
                AutoGenerateRows="false"           
                CssClass="ms-formtable"
                EmptyDataText="Please select site"
                >
                <Fields>
                    <asp:TemplateField
                        HeaderStyle-CssClass="ms-formlabel"
                        ItemStyle-CssClass="ms-formbody"
                        HeaderText="URL" >
                        <ItemTemplate>
                          <p STYLE="word-wrap:break-word;word-break:break-all;width:100%">
                            <%# Eval("Url").ToString() %>
                          </p>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField
                        HeaderStyle-CssClass="ms-formlabel"
                        ItemStyle-CssClass="ms-formbody"
                        HeaderText="Title" >
                        <ItemTemplate>
                          <p STYLE="word-wrap:break-word;word-break:break-all;width:100%">
                            <%# Eval("Title").ToString() %>
                          </p>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField
                        HeaderStyle-CssClass="ms-formlabel"
                        ItemStyle-CssClass="ms-formbody"
                        HeaderText="Web Description" >
                        <ItemTemplate>
                          <p STYLE="word-wrap:break-word;word-break:break-all;width:100%">
                            <%# Eval("Description").ToString() %>
                          </p>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField
                        HeaderStyle-CssClass="ms-formlabel"
                        ItemStyle-CssClass="ms-formbody"
                        HeaderText="Administrator"
                        HtmlEncode="true"
                        DataField="OwnerLoginName" />
                    <asp:BoundField
                        HeaderStyle-CssClass="ms-formlabel"
                        ItemStyle-CssClass="ms-formbody"
                        HeaderText="ContactEmail"
                        HtmlEncode="true"
                        DataField="OwnerEmail" />
                    <asp:BoundField
                        HeaderStyle-CssClass="ms-formlabel"
                        ItemStyle-CssClass="ms-formbody"
                        HeaderText="Database"
                        HtmlEncode="true"
                        DataField="ContentDatabaseName" />
                </Fields>
            </asp:DetailsView>
           </td>
           </tr>
           </table>
        <div style="font-size:16px;font-weight:bold;">Database</div>  
           <AdminControls:AdministrationDataSourceControl runat="server"
            ID="ContentDatabasesDS" ViewName="ContentDatabases" >
            <SelectParameters>
                <asp:ControlParameter
                        Name="WebApplicationId"
                        ControlId="GvWebApplicationsItems"
                        PropertyName="SelectedValue" />
            </SelectParameters>
        </AdminControls:AdministrationDataSourceControl>
        <SharePoint:SPGridView
            EmptyDataText="Please select web application"
            ID="GvContentDatabases"
            Runat="server"
            width="100%"
            AllowSorting="True"
            DataSourceId="ContentDatabasesDS"
            AutoGenerateColumns="false"   
               
            >
            <AlternatingRowStyle CssClass="ms-alternating" />
            <Columns>           
                <asp:BoundField DataField="Name" HeaderText="Database name" />
                <asp:BoundField DataField="Status" HeaderText="Status" />
                <asp:BoundField DataField="CurrentSiteCount" HeaderText="Site count" />
                <asp:BoundField DataField="WarningSiteCount" HeaderText="Warning site" />
                <asp:BoundField DataField="MaxSiteCount" HeaderText="Max site count" />
            </Columns>
        </SharePoint:SPGridView>--%>
    </form>
    </body>
    </html>

    this code works in default web site and for Microsoft Visual Studio 2008 also by hit F5.  Same page is not works when I kept in _layout folder in sharepoint. Calling like this <!-- /* Font Definitions */ @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman";} a:link, span.MsoHyperlink {mso-style-noshow:yes; mso-style-priority:99; color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {mso-style-noshow:yes; mso-style-priority:99; color:purple; mso-themecolor:followedhyperlink; text-decoration:underline; text-underline:single;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; font-size:10.0pt; mso-ansi-font-size:10.0pt; mso-bidi-font-size:10.0pt;} @page Section1 {size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} -->

    http://localhost:5000/_layouts/test.aspx .

    • Moved by eryang Monday, December 7, 2009 6:13 AM not bcl issue. (From:.NET Base Class Library)
    Saturday, December 5, 2009 3:05 PM

Answers