Exemplo n.º 1
0
        /// <summary>
        /// Flushes the BLOB cache for the specified Web Application.
        /// WARNING: This method needs to be run as Farm Admin and have security_admin SQL server role and the db_owner role
        /// on the web app's content DB in order to successfully flush the web app's BLOB cache.
        /// </summary>
        /// <param name="webApplication">The SharePoint web application.</param>
        public void FlushBlobCache(SPWebApplication webApplication)
        {
            try
            {
                PublishingCache.FlushBlobCache(webApplication);
            }
            catch (SPException exception)
            {
                this.logger.Error("Failed to flush the BLOB cache accross the web app. You need You need security_admin SQL server role and the db_owner role on the web app's content DB. Caught and swallowed exception: {0}", exception);
            }
            catch (AccessViolationException exception)
            {
                this.logger.Warn("Received an AccessViolationException when flushing BLOB Cache. Trying again with RemoteAdministratorAccessDenied set to true. Caught and swallowed exception: {0}", exception);

                bool         initialRemoteAdministratorAccessDenied = true;
                SPWebService myService = SPWebService.ContentService;

                try
                {
                    initialRemoteAdministratorAccessDenied    = myService.RemoteAdministratorAccessDenied;
                    myService.RemoteAdministratorAccessDenied = false;
                    myService.Update();

                    PublishingCache.FlushBlobCache(webApplication);
                }
                finally
                {
                    myService.RemoteAdministratorAccessDenied = initialRemoteAdministratorAccessDenied;
                    myService.Update();
                }
            }
        }
Exemplo n.º 2
0
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWebService service = SPWebService.ContentService;

                Collection <SPWebConfigModification> modsCollection = service.WebConfigModifications;

                // Find the most recent modification of a specified owner
                int modsCount1 = modsCollection.Count;
                for (int i = modsCount1 - 1; i > -1; i--)
                {
                    //I will remove from the web.config just the lines related to this solution and not the ones deployed from other solutions.
                    if (modsCollection[i].Owner.Equals("DH"))
                    {
                        modsCollection.Remove(modsCollection[i]);
                    }
                }

                // Save web.config changes.
                service.Update();
                // Applies the list of web.config modifications to all Web applications in this Web service across the farm.
                service.ApplyWebConfigModifications();
            }
            catch (Exception exception)
            {
                //This is a logging class I use to write exceptions to the ULS Logs. There are plenty of options in this area as well.
                ULSLog2013.LogError(exception);
                throw;
            }
        }
        private void UnregisterHttpModule(SPFeatureReceiverProperties properties)
        {
            SPWebConfigModification webConfigModification = CreateWebModificationObject();

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                SPWebService contentService = properties.Definition.Farm.Services.GetValue <SPWebService>();

                int numberOfModifications = contentService.WebConfigModifications.Count;

                //Iterate over all WebConfigModification and delete only those we have created
                for (int i = numberOfModifications - 1; i >= 0; i--)
                {
                    SPWebConfigModification currentModifiction = contentService.WebConfigModifications[i];

                    if (currentModifiction.Owner.Equals(webConfigModification.Owner))
                    {
                        contentService.WebConfigModifications.Remove(currentModifiction);
                    }
                }

                //Update only if we have something deleted
                if (numberOfModifications > contentService.WebConfigModifications.Count)
                {
                    contentService.Update();
                    contentService.ApplyWebConfigModifications();
                }
            });
        }
Exemplo n.º 4
0
 public static void Disable()
 {
     SPSecurity.RunWithElevatedPrivileges(delegate()
     {
         SPWebService webservice = SPWebService.ContentService;
         webservice.RemoteAdministratorAccessDenied = true;
         webservice.Update();
     });
 }
        protected override void UpdateDataObject()
        {
            SPQuotaTemplate quota = new SPQuotaTemplate();

            if (Identity != null)
            {
                SPQuotaTemplate clone = Identity.Read();
                quota.Name = clone.Name;
                quota.StorageMaximumLevel  = clone.StorageMaximumLevel;
                quota.StorageWarningLevel  = clone.StorageWarningLevel;
                quota.UserCodeMaximumLevel = clone.UserCodeMaximumLevel;
                quota.UserCodeWarningLevel = clone.UserCodeWarningLevel;
            }
            else
            {
                throw new SPCmdletException("A quota template is required.");
            }


            if (StorageMaximumLevel.HasValue)
            {
                if (StorageMaximumLevel.Value > quota.StorageWarningLevel)
                {
                    quota.StorageMaximumLevel = StorageMaximumLevel.Value;
                    quota.StorageWarningLevel = StorageWarningLevel.Value;
                }
                else
                {
                    quota.StorageWarningLevel = StorageWarningLevel.Value;
                    quota.StorageMaximumLevel = StorageMaximumLevel.Value;
                }
            }

            if (UserCodeMaximumLevel.HasValue)
            {
                if (UserCodeMaximumLevel.Value > quota.UserCodeWarningLevel)
                {
                    quota.UserCodeMaximumLevel = UserCodeMaximumLevel.Value;
                    quota.UserCodeWarningLevel = UserCodeWarningLevel.Value;
                }
                else
                {
                    quota.UserCodeWarningLevel = UserCodeWarningLevel.Value;
                    quota.UserCodeMaximumLevel = UserCodeMaximumLevel.Value;
                }
            }
            SPWebService webService = SPWebService.ContentService;

            webService.QuotaTemplates[quota.Name] = quota;

            webService.Update();
        }
        private void RegisterHttpModule(SPFeatureReceiverProperties properties)
        {
            SPWebConfigModification webConfigModification = CreateWebModificationObject();

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                SPWebService contentService = SPWebService.ContentService;

                contentService.WebConfigModifications.Add(webConfigModification);
                contentService.Update();
                contentService.ApplyWebConfigModifications();
            });
        }
        protected override SPQuota CreateDataObject()
        {
            SPQuotaTemplate quota = new SPQuotaTemplate();

            quota.Name = Name;

            SPWebService webService = SPWebService.ContentService;

            webService.QuotaTemplates.Add(quota);

            if (Quota != null)
            {
                SPQuota clone = Quota.Read();
                quota.StorageMaximumLevel  = clone.StorageMaximumLevel;
                quota.StorageWarningLevel  = clone.StorageWarningLevel;
                quota.UserCodeMaximumLevel = clone.UserCodeMaximumLevel;
                quota.UserCodeWarningLevel = clone.UserCodeWarningLevel;
            }

            if (StorageMaximumLevel.HasValue)
            {
                if (StorageMaximumLevel.Value > quota.StorageWarningLevel)
                {
                    quota.StorageMaximumLevel = StorageMaximumLevel.Value;
                    quota.StorageWarningLevel = StorageWarningLevel.Value;
                }
                else
                {
                    quota.StorageWarningLevel = StorageWarningLevel.Value;
                    quota.StorageMaximumLevel = StorageMaximumLevel.Value;
                }
            }

            if (UserCodeMaximumLevel.HasValue)
            {
                if (UserCodeMaximumLevel.Value > quota.UserCodeWarningLevel)
                {
                    quota.UserCodeMaximumLevel = UserCodeMaximumLevel.Value;
                    quota.UserCodeWarningLevel = UserCodeWarningLevel.Value;
                }
                else
                {
                    quota.UserCodeWarningLevel = UserCodeWarningLevel.Value;
                    quota.UserCodeMaximumLevel = UserCodeMaximumLevel.Value;
                }
            }
            webService.Update();

            return(quota);
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            SPWebService contentService = SPWebService.ContentService;

            SPWcfServiceSettings wcfServiceSettings = new SPWcfServiceSettings();

            wcfServiceSettings.ReaderQuotasMaxStringContentLength = ReaderQuotasMaxStringContentLength;
            wcfServiceSettings.ReaderQuotasMaxArrayLength         = ReaderQuotasMaxArrayLength;
            wcfServiceSettings.ReaderQuotasMaxBytesPerRead        = ReaderQuotasMaxBytesPerRead;
            wcfServiceSettings.MaxReceivedMessageSize             = MaxReceivedMessageSize;
            contentService.WcfServiceSettings[ServiceName]        = wcfServiceSettings;

            contentService.Update();
        }
 public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
 {
     try
     {
         SPWebService contentService = SPWebService.ContentService;
         contentService.WebConfigModifications.Remove(GetConfigModification());
         // Serialize the web application state and propagate changes across the farm.
         contentService.Update();
         // Save web.config changes.
         contentService.ApplyWebConfigModifications();
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
         throw;
     }
 }
        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                SPWebService service = SPWebService.ContentService;
                Collection <SPWebConfigModification> modsCollection = service.WebConfigModifications;
                int modsCount1 = modsCollection.Count;
                for (int i = modsCount1 - 1; i > -1; i--)
                {
                    if (modsCollection[i].Owner.Equals("TopNavCustomSiteMapProvider"))
                    {
                        modsCollection.Remove(modsCollection[i]);
                    }
                }
                service.Update();
                service.ApplyWebConfigModifications();
            });
        }
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                SPWebService service = SPWebService.ContentService;
                SPWebConfigModification myModification = new SPWebConfigModification();
                myModification.Path     = "configuration/system.web/siteMap/providers";
                myModification.Name     = "add[@name='TestPortalSiteMapNonPublishingSite']";
                myModification.Sequence = 0;
                myModification.Owner    = "TopNavCustomSiteMapProvider";
                myModification.Type     = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                var typeName            = typeof(TestPortalSiteMapNonPublishingSite.Navigation).FullName + ", " + typeof(TestPortalSiteMapNonPublishingSite.Navigation).Assembly.FullName;
                myModification.Value    = "<add name=\"TestPortalSiteMapNonPublishingSite\" type=\"" + typeName + "\" NavigationType=\"Global\" />";
                service.WebConfigModifications.Add(myModification);
                service.Update();
                service.ApplyWebConfigModifications();
            });
        }
Exemplo n.º 12
0
        public static void UpdateCustomWCF()
        {
            SPWebService         contentService     = SPWebService.ContentService;
            SPWcfServiceSettings wcfServiceSettings = new SPWcfServiceSettings();

            wcfServiceSettings.ReaderQuotasMaxStringContentLength = Int32.MaxValue;
            wcfServiceSettings.ReaderQuotasMaxArrayLength         = Int32.MaxValue;
            wcfServiceSettings.ReaderQuotasMaxBytesPerRead        = Int32.MaxValue;
            wcfServiceSettings.MaxReceivedMessageSize             = Int32.MaxValue;

            // Note: "runtime.svc" must be in lowercase
            contentService.WcfServiceSettings.Remove("calendarservice.svc");
            contentService.WcfServiceSettings.Remove("commonservice.svc");
            contentService.WcfServiceSettings.Remove("departmentservice.svc");
            contentService.WcfServiceSettings.Remove("employeeservice.svc");
            contentService.WcfServiceSettings.Remove("shifttimeservice.svc");
            contentService.WcfServiceSettings.Remove("shiftmanagementservice.svc");
            contentService.WcfServiceSettings.Remove("changeshiftmanagementservice.svc");
            contentService.WcfServiceSettings.Remove("overtimeservice.svc");
            contentService.WcfServiceSettings.Remove("notoverTimemanagementservice.svc");
            contentService.WcfServiceSettings.Remove("vehiclemanagementservice.svc");
            contentService.WcfServiceSettings.Remove("leavemanagementservice.svc");
            contentService.WcfServiceSettings.Remove("freightmanagementservice.svc");
            contentService.WcfServiceSettings.Remove("businesstripmanagementservice.svc");



            //contentService.WcfServiceSettings["calendarservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["commonservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["departmentservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["employeeservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["shifttimeservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["shiftmanagementservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["changeshiftmanagementservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["overtimeservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["notoverTimemanagementservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["vehiclemanagementservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["leavemanagementservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["freightmanagementservice.svc"] = wcfServiceSettings;
            //contentService.WcfServiceSettings["businesstripmanagementservice.svc"] = wcfServiceSettings;

            contentService.Update(true);
        }
Exemplo n.º 13
0
        static void ConfigureWebServiceQuotas(string webServiceName)
        {
            SPWcfServiceSettings settings = new SPWcfServiceSettings {
                MaxReceivedMessageSize             = 2147483647,
                MaxBufferSize                      = 2147483647,
                OpenTimeout                        = TimeSpan.FromMinutes(5),
                CloseTimeout                       = TimeSpan.FromMinutes(5),
                ReceiveTimeout                     = TimeSpan.FromMinutes(5),
                ReaderQuotasMaxDepth               = 2147483647,
                ReaderQuotasMaxStringContentLength = 2147483647,
                ReaderQuotasMaxArrayLength         = 2147483647,
                ReaderQuotasMaxBytesPerRead        = 2147483647,
                ReaderQuotasMaxNameTableCharCount  = 2147483647,
            };

            SPWebService contentService = SPWebService.ContentService;

            contentService.WcfServiceSettings[webServiceName] = settings;
            contentService.Update();
        }
Exemplo n.º 14
0
        // Uncomment the method below to handle the event raised after a feature has been activated.

        //public override void FeatureActivated(SPFeatureReceiverProperties properties)
        //{
        //}

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                ULSLog2013.LogWarning("Starting feature activation");
                SPWebService service = SPWebService.ContentService;

                //There is another SPWebConfigModification constructor accepting 2 parameters:
                //name and xpath, but I think it is cleaner from the reader's perspective to do it in separate lines.
                SPWebConfigModification myModification = new SPWebConfigModification();

                //xPath to where we want to add our node. In order to avoid weird errors, consider this as key sensitive.
                myModification.Path = "configuration/system.web/siteMap/providers";
                myModification.Name = "add[@name='MyCustomNavigationProvider']";

                myModification.Sequence = 0;
                //The owner property will help us to categorize and clean just our own customizations when the feature will be deactivated.
                //You can choose a more professional naming convention to identify your changes to web.config.
                myModification.Owner = "DH";
                myModification.Type  = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                myModification.Value = "<add name=\"MyCustomNavigationProvider\" type=\"TopNav.MyCustomSiteMapProvider, TopNav, Version=1.0.0.0, Culture=neutral, PublicKeyToken=912f3c6491be12c2\" NavigationType=\"Global\" />";
                //You are adding a SPWebConfigModification to the collection of modifications.
                //Every time you run this code a new item will be added to it.
                service.WebConfigModifications.Add(myModification);

                /*Call Update and ApplyWebConfigModifications to save changes*/
                //If there is an error in any of the elements to add/modify in the web.config, it will produce an error and your line will not be added.
                //This collection works as a FIFO queue and therefore, you could be adding more and more elements to it.
                service.Update();
                service.ApplyWebConfigModifications();
            }
            catch (Exception exception)
            {
                //This is a logging class I use to write exceptions to the ULS Logs. There are plenty of options in this area as well.
                //Logging.WriteExceptionToTraceLog(exception.Source, exception.Message, exception, null);
                throw;
            }
        }
Exemplo n.º 15
0
        public static void ConfigureRemoteManagerTimeout(string service)
        {
            SPWebService contentService = SPWebService.ContentService;

            if (contentService.WcfServiceSettings.ContainsKey(service))
            {
                SPWcfServiceSettings wcfServiceSettings = new SPWcfServiceSettings();
                wcfServiceSettings.ReaderQuotasMaxStringContentLength = 10485760;
                wcfServiceSettings.ReaderQuotasMaxArrayLength         = int.MaxValue;
                wcfServiceSettings.ReaderQuotasMaxBytesPerRead        = 10485760;
                wcfServiceSettings.MaxReceivedMessageSize             = 10485760;
                wcfServiceSettings.ReceiveTimeout = TimeSpan.FromMinutes(15);
                wcfServiceSettings.OpenTimeout    = TimeSpan.FromMinutes(15);
                wcfServiceSettings.CloseTimeout   = TimeSpan.FromMinutes(15);

                contentService.WcfServiceSettings[service.ToLower()] = wcfServiceSettings;
                contentService.Update();
            }
            else
            {
                // TODO - Exception handling
            }
        }
        /// <summary>
        /// Make modifications to the web.config when the feature is activated.
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            spSite = (SPSite)properties.Feature.Parent;
            spWeb = spSite.OpenWeb();
            spWebService = SPWebService.ContentService;

            Configuration config = WebConfigurationManager.OpenWebConfiguration("/", spSite.WebApplication.Name);
            string smtpServer = string.Empty;
            string configFilePath = config.FilePath;
            doc = new XmlDocument();
            doc.Load(configFilePath);
            AppSettingsSection appSettings = config.AppSettings;

            if (appSettings.ElementInformation.IsPresent)
            {
                nodeSmtpServer = doc.SelectSingleNode("configuration/appSettings/add[@key='smtpServer']");
                nodeUserName = doc.SelectSingleNode("configuration/appSettings/add[@key='docuSignUserName']");
                nodePassword = doc.SelectSingleNode("configuration/appSettings/add[@key='docuSignPassword']");

                // Add smtpServer to web.config file
                if(nodeSmtpServer == null)
                    ModifyWebConfigData("add[@key='SMTPServer']", "configuration/appSettings", "<add key='smtpServer' value='" + SMTP_SERVER + "' />");

                // Add DocuSign Service user Credentials to web.config file
                if (nodeUserName == null)
                    ModifyWebConfigData("add[@key='userName']", "configuration/appSettings", "<add key='docuSignUserName' value='" + DOCUSIGN_USERNAME + "' />");

                // Add Profiles to web.config file
                if(nodePassword == null)
                    ModifyWebConfigData("add[@key='password']", "configuration/appSettings", "<add key='docuSignPassword' value='" + DOCUSIGN_PASSWORD + "' />");
            }
            else
                ModifyWebConfigData("add[@key='SMTPServer']", "configuration", "<appSettings><add key='smtpServer' value='" + SMTP_SERVER + "' /><add key='docuSignUserName' value='" + DOCUSIGN_USERNAME + "' /><add key='docuSignPassword' value='" + DOCUSIGN_PASSWORD + "' /></appSettings>");

            nodeServiceModel = doc.SelectSingleNode("configuration/system.serviceModel");

            // Add ServiceModel to web.config file
            if(nodeServiceModel == null)
                ModifyWebConfigData("ServiceModel", "configuration", GetServiceModelTag());

            XmlNode nodeTrust = null;
            XmlNode nodeEnableSessionState = null;
            nodeTrust = doc.SelectSingleNode("configuration/system.web/trust");
            nodeEnableSessionState = doc.SelectSingleNode("configuration/system.web/pages");

            if (nodeEnableSessionState != null)
            {
                XmlAttribute attributeEnableSessionState = nodeEnableSessionState.Attributes["enableSessionState"];
                if (attributeEnableSessionState != null)
                {
                    attributeEnableSessionState.Value = "true";
                }
            }

            // Set the level attribute to WSS_Minimal
            if (nodeTrust != null)
            {
                XmlAttribute attributeLevel = nodeTrust.Attributes["level"];
                if (attributeLevel != null)
                {
                    attributeLevel.Value = "Full";
                }
            }
            doc.Save(configFilePath);
            ModifyWebConfigData("add[@name='Session']", "configuration/system.web/httpModules", "<add name='Session' type='System.Web.SessionState.SessionStateModule' />");

            spWebService.Update();
            spWebService.ApplyWebConfigModifications();
        }
Exemplo n.º 17
0
        /// <summary>
        /// Make modifications to the web.config when the feature is activated.
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            spSite       = (SPSite)properties.Feature.Parent;
            spWeb        = spSite.OpenWeb();
            spWebService = SPWebService.ContentService;

            Configuration config         = WebConfigurationManager.OpenWebConfiguration("/", spSite.WebApplication.Name);
            string        smtpServer     = string.Empty;
            string        configFilePath = config.FilePath;

            doc = new XmlDocument();
            doc.Load(configFilePath);
            AppSettingsSection appSettings = config.AppSettings;

            if (appSettings.ElementInformation.IsPresent)
            {
                nodeSmtpServer = doc.SelectSingleNode("configuration/appSettings/add[@key='smtpServer']");
                nodeUserName   = doc.SelectSingleNode("configuration/appSettings/add[@key='docuSignUserName']");
                nodePassword   = doc.SelectSingleNode("configuration/appSettings/add[@key='docuSignPassword']");

                // Add smtpServer to web.config file
                if (nodeSmtpServer == null)
                {
                    ModifyWebConfigData("add[@key='SMTPServer']", "configuration/appSettings", "<add key='smtpServer' value='" + SMTP_SERVER + "' />");
                }

                // Add DocuSign Service user Credentials to web.config file
                if (nodeUserName == null)
                {
                    ModifyWebConfigData("add[@key='userName']", "configuration/appSettings", "<add key='docuSignUserName' value='" + DOCUSIGN_USERNAME + "' />");
                }

                // Add Profiles to web.config file
                if (nodePassword == null)
                {
                    ModifyWebConfigData("add[@key='password']", "configuration/appSettings", "<add key='docuSignPassword' value='" + DOCUSIGN_PASSWORD + "' />");
                }
            }
            else
            {
                ModifyWebConfigData("add[@key='SMTPServer']", "configuration", "<appSettings><add key='smtpServer' value='" + SMTP_SERVER + "' /><add key='docuSignUserName' value='" + DOCUSIGN_USERNAME + "' /><add key='docuSignPassword' value='" + DOCUSIGN_PASSWORD + "' /></appSettings>");
            }

            nodeServiceModel = doc.SelectSingleNode("configuration/system.serviceModel");

            // Add ServiceModel to web.config file
            if (nodeServiceModel == null)
            {
                ModifyWebConfigData("ServiceModel", "configuration", GetServiceModelTag());
            }

            XmlNode nodeTrust = null;
            XmlNode nodeEnableSessionState = null;

            nodeTrust = doc.SelectSingleNode("configuration/system.web/trust");
            nodeEnableSessionState = doc.SelectSingleNode("configuration/system.web/pages");

            if (nodeEnableSessionState != null)
            {
                XmlAttribute attributeEnableSessionState = nodeEnableSessionState.Attributes["enableSessionState"];
                if (attributeEnableSessionState != null)
                {
                    attributeEnableSessionState.Value = "true";
                }
            }

            // Set the level attribute to WSS_Minimal
            if (nodeTrust != null)
            {
                XmlAttribute attributeLevel = nodeTrust.Attributes["level"];
                if (attributeLevel != null)
                {
                    attributeLevel.Value = "Full";
                }
            }
            doc.Save(configFilePath);
            ModifyWebConfigData("add[@name='Session']", "configuration/system.web/httpModules", "<add name='Session' type='System.Web.SessionState.SessionStateModule' />");

            spWebService.Update();
            spWebService.ApplyWebConfigModifications();
        }