コード例 #1
0
        public void UnextendVirtualServer(string url, bool deleteContent)
        {
            try
            {
                WindowsImpersonationContext wic = WindowsIdentity.GetCurrent().Impersonate();

                Uri uri = new Uri("http://" + url);
                SPWebApplication app = SPWebApplication.Lookup(uri);
                if (app == null)
                {
                    return;
                }

                SPGlobalAdmin adm = new SPGlobalAdmin();
                adm.UnextendVirtualServer(uri, false);

                //typeof(SPWebApplication).InvokeMember("UnprovisionIisWebSitesAsAdministrator",
                //    BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod,
                //    null, null, new object[] { false, new string[] { url }, app.ApplicationPool });
                //app.Unprovision();
                app.Delete();

                wic.Undo();
            }
            catch (Exception ex)
            {
                throw new Exception("Could not uninstall SharePoint from the web site", ex);
            }
        }
コード例 #2
0
 public void Delete()
 {
     m_webApplication.Delete();
 }
コード例 #3
0
        /// <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, System.Collections.Specialized.StringDictionary keyValues, out string output)
        {
            output = string.Empty;



            string url           = Params["url"].Value;
            bool   deleteContent = Params["deletecontentdb"].UserTypedIn;
            bool   deleteIis     = Params["deleteiiswebsite"].UserTypedIn;

            SPWebApplication webApp = SPWebApplication.Lookup(new Uri(url));

            foreach (SPIisSettings iis in webApp.IisSettings.Values)
            {
                try
                {
                    DirectoryInfo path = iis.Path;
                    if (!path.Exists)
                    {
                        throw new Exception();
                    }
                }
                catch (Exception)
                {
                    iis.Path = new DirectoryInfo("c:\\");
                    webApp.Update();
                }
            }

            if (webApp.IisSettings.Count <= 0 && deleteContent)
            {
                DeleteContentDBs(webApp);
                webApp.Delete();
                return((int)ErrorCodes.NoError);
            }

            int index = 0;

            string[] serverComments = new string[webApp.IisSettings.Count];
            string[] vdirs          = new string[webApp.IisSettings.Count];
            foreach (SPIisSettings iisSetting in webApp.IisSettings.Values)
            {
                vdirs[index]          = iisSetting.Path.ToString();
                serverComments[index] = iisSetting.ServerComment;
                index++;
            }

            // webApp.Unprovision() does not allow us to specify whether we want the site deleted so we have to use the internal version.
            // SPWebApplication.UnprovisionIisWebSites(deleteIis, serverComments, webApp.ApplicationPool.Name);
            MethodInfo unprovisionIisWebSites = webApp.GetType().GetMethod("UnprovisionIisWebSites",
                                                                           BindingFlags.NonPublic | BindingFlags.Public |
                                                                           BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Static,
                                                                           null, new Type[] { typeof(bool), typeof(string[]), typeof(string) }, null);

            unprovisionIisWebSites.Invoke(webApp, new object[] { deleteIis, serverComments, webApp.ApplicationPool.Name });


            // SPSolution.RetractSolutions(webApp.Farm, webApp.Id, vdirs, serverComments, true);
            MethodInfo retractSolutions = typeof(SPSolution).GetMethod("RetractSolutions",
                                                                       BindingFlags.NonPublic | BindingFlags.Public |
                                                                       BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Static,
                                                                       null, new Type[] { typeof(SPFarm), typeof(Guid), typeof(string[]), typeof(string[]), typeof(bool) }, null);

            retractSolutions.Invoke(null, new object[] { webApp.Farm, webApp.Id, vdirs, serverComments, true });


            if (SPFarm.Local.TimerService.Instances.Count > 1)
            {
                // SPIisWebsiteUnprovisioningJobDefinition is an internal class so we need to use reflection to set it.

                // SPIisWebsiteUnprovisioningJobDefinition jobDef = new SPIisWebsiteUnprovisioningJobDefinition(deleteIis, serverComments, webApp.ApplicationPool.Name, vdirs, webApp.Id, true);
                Type sPIisWebsiteUnprovisioningJobDefinitionType = Type.GetType("Microsoft.SharePoint.Administration.SPIisWebsiteUnprovisioningJobDefinition, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");

                ConstructorInfo sPIisWebsiteUnprovisioningJobDefinitionConstructor =
                    sPIisWebsiteUnprovisioningJobDefinitionType.GetConstructor(
                        BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,
                        null,
                        new Type[] { typeof(bool), typeof(string[]), typeof(string), typeof(string[]), typeof(Guid), typeof(bool) }, null);
                object jobDef = sPIisWebsiteUnprovisioningJobDefinitionConstructor.Invoke(new object[] { deleteIis, serverComments, webApp.ApplicationPool.Name, vdirs, webApp.Id, true });


                // jobDef.Schedule = new SPOneTimeSchedule(DateTime.Now);
                PropertyInfo scheduleProp = sPIisWebsiteUnprovisioningJobDefinitionType.GetProperty("Schedule",
                                                                                                    BindingFlags.FlattenHierarchy |
                                                                                                    BindingFlags.NonPublic |
                                                                                                    BindingFlags.Instance |
                                                                                                    BindingFlags.InvokeMethod |
                                                                                                    BindingFlags.GetProperty |
                                                                                                    BindingFlags.Public);

                scheduleProp.SetValue(jobDef, new SPOneTimeSchedule(DateTime.Now), null);

                // jobDef.Update();
                MethodInfo update = sPIisWebsiteUnprovisioningJobDefinitionType.GetMethod("Update",
                                                                                          BindingFlags.NonPublic |
                                                                                          BindingFlags.Public |
                                                                                          BindingFlags.Instance |
                                                                                          BindingFlags.InvokeMethod |
                                                                                          BindingFlags.FlattenHierarchy,
                                                                                          null,
                                                                                          new Type[] { }, null);


                update.Invoke(jobDef, new object[] { });
            }

            if (deleteContent)
            {
                DeleteContentDBs(webApp);
            }

            webApp.Delete();


            return((int)ErrorCodes.NoError);
        }
コード例 #4
0
        public void ExtendVirtualServer(SharePointSite site, bool exclusiveNTLM)
        {
            try
            {
                WindowsImpersonationContext wic = WindowsIdentity.GetCurrent().Impersonate();

                string siteUrl = "http://" + site.Name;

                // check input parameters
                if (String.IsNullOrEmpty(site.RootFolder) ||
                    !Directory.Exists(site.RootFolder))
                {
                    throw new Exception("Could not create SharePoint site, because web site root folder does not exist. Open web site properties and click \"Update\" button to re-create site folder.");
                }

                SPWebApplication app = SPWebApplication.Lookup(new Uri(siteUrl));
                if (app != null)
                {
                    throw new Exception("SharePoint is already installed on this web site.");
                }

                //SPFarm farm = SPFarm.Local;
                SPFarm farm = SPFarm.Local;
                SPWebApplicationBuilder builder = new SPWebApplicationBuilder(farm);
                builder.ApplicationPoolId = site.ApplicationPool;
                builder.DatabaseServer    = site.DatabaseServer;
                builder.DatabaseName      = site.DatabaseName;
                builder.DatabaseUsername  = site.DatabaseUser;
                builder.DatabasePassword  = site.DatabasePassword;

                builder.ServerComment = site.Name;
                builder.HostHeader    = site.Name;
                builder.Port          = 80;

                builder.RootDirectory      = new DirectoryInfo(site.RootFolder);
                builder.DefaultZoneUri     = new Uri(siteUrl);
                builder.UseNTLMExclusively = exclusiveNTLM;

                app      = builder.Create();
                app.Name = site.Name;

                app.Sites.Add(siteUrl, null, null, (uint)site.LocaleID, null, site.OwnerLogin, null, site.OwnerEmail);

                app.Update();
                app.Provision();

                wic.Undo();
            }
            catch (Exception ex)
            {
                try
                {
                    // try to delete app if it was created
                    SPWebApplication app = SPWebApplication.Lookup(new Uri("http://" + site.Name));
                    if (app != null)
                    {
                        app.Delete();
                    }
                }
                catch { /* nop */ }

                throw new Exception("Error creating SharePoint site", ex);
            }
        }