/// <summary>
        /// Removes the module.
        /// </summary>
        /// <param name="id">The id of the module.</param>
        public static void RemoveModule(SPWebApplication Webapp, Guid id)
        {
            WebApp = Webapp;
            IWebSiteControllerModule module = GetModule(Webapp, id);

            GetFromConfigDB().modules.Remove(id);

            // Disable all rules for this module
            foreach (WebSiteControllerRule rule in Rules)
            {
                if (rule.RuleType.Equals(module.RuleType, StringComparison.OrdinalIgnoreCase))
                {
                    RemoveRule(rule);
                    AddRule(
                        Webapp,
                        rule.SiteCollection,
                        rule.Page,
                        rule.RuleType,
                        rule.Principal,
                        rule.PrincipalType,
                        true,
                        rule.AppliesToSsl,
                        rule.Sequence,
                        rule.Properties);
                }
            }
        }
        /// <summary>
        /// Raises the <see cref="E:Load"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected override void OnLoad(EventArgs e)
        {
            SPContext.Current.Web.AllowUnsafeUpdates = true;
            if (!String.IsNullOrEmpty(Request.QueryString["guid"]))
            {
                _guid = new Guid(Request.QueryString["guid"]);
            }

            if (!String.IsNullOrEmpty(Request.QueryString["Source"]))
            {
                _source = Request.QueryString["Source"];
            }

            if (this.Page.Request["__EVENTTARGET"] == RibbonPostbackId)
            {
                _event = this.Page.Request["__EVENTARGUMENT"];
            }

            _modulename = this.moduleTextBox.Text;
            _assembly   = this.assemblyTextbox.Text;

            if (_event == "GoModules")
            {
                GoBack();
            }

            if (_guid != Guid.Empty)
            {
                _module = WebSiteControllerConfig.GetModule(SPContext.Current.Site.WebApplication, _guid);

                if (!IsPostBack)
                {
                    this.moduleTextBox.Text      = _module.GetType().FullName;
                    this.moduleTextBox.Enabled   = false;
                    this.assemblyTextbox.Text    = _module.GetType().AssemblyQualifiedName;
                    this.assemblyTextbox.Enabled = false;

                    this.LoadRules();
                }

                if (_event == "Delete")
                {
                    DeleteModule();
                }

                if (_event == "Rules")
                {
                    CreateRule();
                }
            }
            else
            {
                if (_event == "Save")
                {
                    SaveModule();
                }
            }
            SPContext.Current.Web.AllowUnsafeUpdates = false;
        }
        /// <summary>
        /// Adds the module.
        /// </summary>
        /// <param name="fullyQualifiedClassName">Name of the fully qualified class of the module being added.</param>
        public static void AddModule(string fullyQualifiedClassName)
        {
            WebSiteControllerConfig config = GetFromConfigDB();

            IWebSiteControllerModule module = GetModuleFromClassName(fullyQualifiedClassName);

            config.modules.Add(new PersistedWebSiteControllerModule(Guid.NewGuid(), module.RuleType, fullyQualifiedClassName, config));
        }
        public static void AddModule(IWebSiteControllerModule module, string fullyQualifiedClassName)
        {
            WebSiteControllerConfig config = GetFromConfigDB();

            PersistedWebSiteControllerModule mod = new PersistedWebSiteControllerModule(Guid.NewGuid(), module.RuleType, fullyQualifiedClassName, config);

            mod.Update();

            //config.modules.Add(new PersistedWebSiteControllerModule(module.Id, module.RuleType, fullyQualifiedClassName, config));
        }
        /// <summary>
        /// Gets the module.
        /// </summary>
        /// <param name="id">The id of the module</param>
        /// <returns>A page controller module</returns>
        public static IWebSiteControllerModule GetModule(SPWebApplication Webapp, Guid id)
        {
            WebApp = Webapp;
            PersistedWebSiteControllerModule module = GetFromConfigDB().modules[id] as PersistedWebSiteControllerModule;

            if (module != null)
            {
                IWebSiteControllerModule imodule = GetModuleFromClassName(module.FullyQualifiedClassName);
                imodule.Id = id;
                return(imodule);// GetModuleFromClassName(module.FullyQualifiedClassName);
            }
            return(null);
        }
        public static List <WebSiteControllerRule> GetRulesForPage(SPWebApplication Webapp, Uri url, string ruleType, SPUser user)
        {
            WebApp = Webapp;
            List <WebSiteControllerRule> list = new List <WebSiteControllerRule>();

            if (url == null)
            {
                return(list);
            }

            WebSiteControllerModulesCollection modules = GetFromConfigDB().modules;

            foreach (PersistedWebSiteControllerModule module in modules)
            {
                IWebSiteControllerModule imodule = GetModule(Webapp, module.Id);
                if (imodule.AlwaysRun)
                {
                    try
                    {
                        WebSiteControllerRule _rule = GetRule(imodule.RuleType);
                        if (_rule == null)
                        {
                            _rule = new WebSiteControllerRule();
                        }
                        //WebSiteControllerRule temp = new WebSiteControllerRule(SPContext.Current.Site.Url, url.ToString(), ruleType, String.Empty, WebSiteControllerPrincipalType.None, false, true, 0, _rule.Parent);
                        list.Add(_rule);
                    }
                    catch (Exception ex)
                    {
                        SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data);
                        //ex.ToString();
                    }
                }
            }

            WebSiteControllerRulesCollection rules = GetFromConfigDB().rules;

            foreach (WebSiteControllerRule rule in rules)
            {
                if (IsSinglePageControlled(rule, url, ruleType, user))
                {
                    list.Add(rule);
                }
            }

            list.Sort(WebSiteControllerRule.CompareBySequence);
            return(list);
        }
        public static void AddModule(SPWebApplication Webapp, IWebSiteControllerModule module, string fullyQualifiedClassName)
        {
            try
            {
                WebApp = Webapp;
                WebSiteControllerConfig config = GetFromConfigDB();

                PersistedWebSiteControllerModule mod = new PersistedWebSiteControllerModule(Guid.NewGuid(), module.RuleType, fullyQualifiedClassName, config);
                mod.Update();
            }
            catch (Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data);
                //ex.ToString();
            }

            //config.modules.Add(new PersistedWebSiteControllerModule(module.Id, module.RuleType, fullyQualifiedClassName, config));
        }
示例#8
0
        protected override bool ProcessWorkItems(SPContentDatabase contentDatabase, SPWorkItemCollection workItems, SPJobState jobState)
        {
            foreach (SPWorkItem workItem in workItems)
            {
                if (workItem != null)
                {
                    try
                    {
                        SPSite site = new SPSite(workItem.SiteId);
                        IWebSiteControllerModule module = null;
                        WebSiteControllerRule    rule   = null;
                        bool _deleted = false;
                        //int ItemID = workItem.ItemId;

                        if (workItem.ItemId > 0)
                        {
                            rule = WebSiteControllerConfig.GetRule(contentDatabase.WebApplication, workItem.ItemGuid);
                        }
                        else
                        {
                            module = WebSiteControllerConfig.GetModule(contentDatabase.WebApplication, workItem.ItemGuid);
                        }

                        if (rule != null)
                        {
                            WebSiteControllerConfig.RemoveRule(rule.Id);
                            _deleted = true;
                        }

                        if (workItem.ItemId < 0 || _deleted)
                        {
                            string[] data                = workItem.TextPayload.Split(new char[] { '#' });
                            string   parameterRule       = data[0];
                            string   parameterProperties = data[1];
                            string[] rules               = parameterRule.Split(new char[] { ';' });

                            string url      = rules[0];
                            bool   disabled = bool.Parse(rules[1]);
                            bool   ssl      = bool.Parse(rules[2]);
                            int    sequence = int.Parse(rules[3]);

                            WebSiteControllerPrincipalType principalType = WebSiteControllerPrincipalType.None;

                            if (!String.IsNullOrEmpty(rules[4]))
                            {
                                principalType = (WebSiteControllerPrincipalType)Enum.Parse(typeof(WebSiteControllerPrincipalType), rules[4]);
                            }
                            string principal = rules[5];

                            string ruletype = string.Empty;

                            if (module != null || String.IsNullOrEmpty(rule.RuleType))
                            {
                                ruletype = module.RuleType;
                            }
                            else if (rule != null && ruletype == string.Empty)
                            {
                                ruletype = rule.RuleType;
                            }

                            string[]  properties = parameterProperties.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                            Hashtable props      = new Hashtable();

                            foreach (string prop in properties)
                            {
                                try
                                {
                                    if (!string.IsNullOrEmpty(prop))
                                    {
                                        string[] keyval = prop.Split(new char[] { ':' });
                                        props.Add(keyval[0], keyval[1]);
                                    }
                                }
                                catch { };
                            }

                            if (_deleted && workItem.ItemId != 1)
                            {
                                WebSiteControllerConfig.AddRule(contentDatabase.WebApplication,
                                                                site.Url + "/",
                                                                url,
                                                                rule.RuleType,
                                                                rule.Principal,
                                                                rule.PrincipalType,
                                                                disabled,
                                                                ssl,
                                                                sequence,
                                                                props
                                                                );
                            }

                            if (workItem.ItemId == -1)
                            {
                                WebSiteControllerConfig.AddRule(contentDatabase.WebApplication,
                                                                site.Url + "/",
                                                                url,
                                                                ruletype,
                                                                principal,
                                                                principalType,
                                                                disabled,
                                                                ssl,
                                                                sequence,
                                                                props
                                                                );
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.ToString();
                        //throw (ex);
                    }
                    finally
                    {
                        DeleteWorkItem(workItems, workItem);
                        //using (SPSite site = new SPSite(workItem.SiteId))
                        //{

                        //    using (SPWeb web = site.OpenWeb(workItem.WebId))
                        //    {
                        //        SPWorkItemCollection deletableCollection = workItems.SubCollection(site, web, (uint)workItem.i, (uint)itemIndex + 1);
                        //        deletableCollection.DeleteWorkItem(currentItem.Id);

                        //        //workItems.CompleteInProgressWorkItems(workItem.ParentId, workItem.Type, workItem.BatchId);
                        //        //workItems.SubCollection(site, web, 0, (uint)workItems.Count).DeleteWorkItem(workItem.Id);

                        //    }

                        //}

                        ////workItems.DeleteWorkItem(workItem.Id);
                    }
                }
            }
            return(true);
        }
        private void CreateWorkItem(string url)
        {
            Guid siteId = SPContext.Current.Site.ID;
            Guid webId  = SPContext.Current.Web.ID;

            bool disabled = false;
            WebSiteControllerPrincipalType principalType = WebSiteControllerPrincipalType.None;
            bool   appliesToSSL = true;
            int    sequence     = 1;
            String pricipal     = string.Empty;

            StringBuilder builder = new StringBuilder();

            builder.Append(SPContext.Current.Web.ServerRelativeUrl + ";");
            builder.Append(disabled.ToString() + ";");
            builder.Append(appliesToSSL.ToString() + ";");
            builder.Append(sequence.ToString() + ";");
            builder.Append(principalType.ToString() + ";");
            builder.Append(pricipal + ";");
            builder.Append("#");

            builder.Append(String.Format("{0}:{1};", "OriginalUrl", url));

            string full = builder.ToString();

            SemanticModule           mod  = new SemanticModule();
            IWebSiteControllerModule imod = null;// WebSiteControllerConfig.GetModule(web.Site.WebApplication, mod.RuleType);

            while (imod == null)
            {
                System.Threading.Thread.Sleep(1000);
                try
                {
                    imod = WebSiteControllerConfig.GetModule(SPContext.Current.Site.WebApplication, mod.RuleType);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }


            int item = -1;

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (SPSite site = new SPSite(siteId))
                {
                    site.AddWorkItem(
                        Guid.NewGuid(),
                        DateTime.Now.ToUniversalTime(),
                        WebSiteControllerRuleWorkItem.WorkItemTypeId,
                        webId,
                        siteId,
                        item,
                        true,
                        imod.Id,
                        Guid.Empty,
                        site.SystemAccount.ID,
                        null,
                        builder.ToString(),
                        Guid.Empty
                        );
                }
            });

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(siteId))//, SPUserToken.SystemAccount))
                {
                    try
                    {
                        WebSiteControllerRuleWorkItem WebSiteControllerModuleJob = new WebSiteControllerRuleWorkItem(WebSiteControllerRuleWorkItem.WorkItemJobDisplayName + "HomePage", site.WebApplication);
                        SPOneTimeSchedule oneTimeSchedule = new SPOneTimeSchedule(DateTime.Now);

                        WebSiteControllerModuleJob.Schedule = oneTimeSchedule;
                        WebSiteControllerModuleJob.Update();
                    }
                    catch { };
                }
            });
        }
        private void CreateWorkItem(SPWebEventProperties properties, string pagename, string url)
        {
            Guid siteId = properties.SiteId;
            Guid webId  = properties.WebId;

            bool disabled = false;
            WebSiteControllerPrincipalType principalType = WebSiteControllerPrincipalType.None;
            bool   appliesToSSL = true;
            int    sequence     = 1;
            String pricipal     = string.Empty;

            StringBuilder builder = new StringBuilder();

            builder.Append(properties.Web.ServerRelativeUrl + url + ";");
            builder.Append(disabled.ToString() + ";");
            builder.Append(appliesToSSL.ToString() + ";");
            builder.Append(sequence.ToString() + ";");
            builder.Append(principalType.ToString() + ";");
            builder.Append(pricipal + ";");
            builder.Append("#");

            builder.Append(String.Format("{0}:{1};", "OriginalUrl", properties.Web.ServerRelativeUrl + pagename));

            string full = builder.ToString();

            IWebSiteControllerModule imod = null;
            SPSite parentsite             = new SPSite(properties.SiteId);

            while (imod == null)
            {
                System.Threading.Thread.Sleep(1000);
                try
                {
                    imod = WebSiteControllerConfig.GetModule(parentsite.WebApplication, "Hemrika.SharePoint.WebSite.Modules.SemanticModule.SemanticModule");
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }

            int item = -1;

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (SPSite site = new SPSite(siteId))
                {
                    site.AddWorkItem(
                        Guid.NewGuid(),
                        DateTime.Now.ToUniversalTime(),
                        WebSiteControllerRuleWorkItem.WorkItemTypeId,
                        webId,
                        siteId,
                        item,
                        true,
                        imod.Id,
                        Guid.Empty,
                        site.SystemAccount.ID,
                        null,
                        builder.ToString(),
                        Guid.Empty
                        );
                }
            });
        }
示例#11
0
        private void CreateWorkItem(SPWeb web)//, HttpStatusCode code)
        {
            Guid siteId = web.Site.ID;
            Guid webId  = web.ID;

            bool disabled = false;
            WebSiteControllerPrincipalType principalType = WebSiteControllerPrincipalType.None;
            bool   appliesToSSL = true;
            int    sequence     = 1;
            String pricipal     = string.Empty;

            StringBuilder builder = new StringBuilder();

            builder.Append("/;");
            builder.Append(disabled.ToString() + ";");
            builder.Append(appliesToSSL.ToString() + ";");
            builder.Append(sequence.ToString() + ";");
            builder.Append(principalType.ToString() + ";");
            builder.Append(pricipal + ";");
            builder.Append("#");

            string value = new JavaScriptSerializer().Serialize(this);

            builder.Append(String.Format("{0}:{1};", "GateKeeper", Encryption.Encrypt(value)));
            //builder.Append(value);

            string full = builder.ToString();

            GateKeeperModule         mod  = new GateKeeperModule();
            IWebSiteControllerModule imod = null;  //WebSiteControllerConfig.GetModule(web.Site.WebApplication, mod.RuleType);

            while (imod == null)
            {
                try
                {
                    imod = WebSiteControllerConfig.GetModule(web.Site.WebApplication, mod.RuleType);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
                System.Threading.Thread.Sleep(1000);
            }

            Guid itemGuid = _guid;
            int  item     = 0;

            if (itemGuid.Equals(Guid.Empty))
            {
                itemGuid = mod.Id;
                item     = -1;
            }
            else
            {
                item = 2;
            }

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (SPSite site = new SPSite(siteId))
                {
                    site.AddWorkItem(
                        Guid.NewGuid(),
                        DateTime.Now.ToUniversalTime(),
                        WebSiteControllerRuleWorkItem.WorkItemTypeId,
                        webId,
                        siteId,
                        item,
                        true,
                        itemGuid,
                        Guid.Empty,
                        site.SystemAccount.ID,
                        null,
                        builder.ToString(),
                        Guid.Empty
                        );
                }
            });
        }
        private void CreateWorkItem(SPWeb web, string pagename, string url)
        {
            Guid siteId = web.Site.ID;
            Guid webId  = web.ID;

            if (url.StartsWith("/"))
            {
                url = url.TrimStart('/');
            }

            bool disabled = false;
            WebSiteControllerPrincipalType principalType = WebSiteControllerPrincipalType.None;
            bool   appliesToSSL = true;
            int    sequence     = 1;
            String pricipal     = string.Empty;

            StringBuilder builder = new StringBuilder();

            builder.Append(url + ";");
            builder.Append(disabled.ToString() + ";");
            builder.Append(appliesToSSL.ToString() + ";");
            builder.Append(sequence.ToString() + ";");
            builder.Append(principalType.ToString() + ";");
            builder.Append(pricipal + ";");
            builder.Append("#");

            if (url.EndsWith("/"))
            {
                builder.Append(String.Format("{0}:{1};", "OriginalUrl", url + pagename + ".aspx"));
            }
            else
            {
                builder.Append(String.Format("{0}:{1};", "OriginalUrl", url + ".aspx"));
            }


            string full = builder.ToString();

            SemanticModule           mod  = new SemanticModule();
            IWebSiteControllerModule imod = null;

            while (imod == null)
            {
                try
                {
                    imod = WebSiteControllerConfig.GetModule(web.Site.WebApplication, mod.RuleType);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
                System.Threading.Thread.Sleep(1000);
            }


            int item = -1;

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (SPSite site = new SPSite(siteId))
                {
                    site.AddWorkItem(
                        Guid.NewGuid(),
                        DateTime.Now.ToUniversalTime(),
                        WebSiteControllerRuleWorkItem.WorkItemTypeId,
                        webId,
                        siteId,
                        item,
                        true,
                        imod.Id,
                        Guid.Empty,
                        site.SystemAccount.ID,
                        null,
                        builder.ToString(),
                        Guid.Empty
                        );
                }
            });
        }
        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event to initialize the page.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            SPContext.Current.Web.AllowUnsafeUpdates = true;

            if (!String.IsNullOrEmpty(Request.QueryString["guid"]))
            {
                _guid = new Guid(Request.QueryString["guid"]);
            }

            if (!String.IsNullOrEmpty(Request.QueryString["ruletype"]))
            {
                _ruletype = Request.QueryString["ruletype"];
            }


            if (!String.IsNullOrEmpty(Request.QueryString["Source"]))
            {
                _source = Request.QueryString["Source"];
            }

            if (this.Page.Request["__EVENTTARGET"] == RibbonPostbackId)
            {
                _event = this.Page.Request["__EVENTARGUMENT"];
            }

            if (_event == "GoModules")
            {
                GoBack();
            }

            if (!string.IsNullOrEmpty(_ruletype))
            {
                _module = WebSiteControllerConfig.GetModule(SPContext.Current.Site.WebApplication, _ruletype);
            }

            try
            {
                if (_module != null)
                {
                    if (!String.IsNullOrEmpty(_module.Control))
                    {
                        WebSiteControllerRuleControl control = (WebSiteControllerRuleControl)Page.LoadControl(_module.Control);
                        control.ID = "moduleControl";
                        if (control.SimpleView)
                        {
                            _SimpleView = control.SimpleView;

                            ControlCollection controls = this.defaultPlaceHolder.Controls;
                            foreach (Control ctrl in controls)
                            {
                                ctrl.Visible = false;
                            }
                        }

                        this.propertiesPlaceholder.Controls.Add(control);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

            SPContext.Current.Web.AllowUnsafeUpdates = false;
        }
示例#14
0
        protected override bool ProcessWorkItems(SPContentDatabase contentDatabase, SPWorkItemCollection workItems, SPJobState jobState)
        {
            foreach (SPWorkItem workItem in workItems)
            {
                if (workItem != null)
                {
                    if (workItem.ItemGuid != Guid.Empty)
                    {
                        WebSiteControllerConfig.RemoveModule(contentDatabase.WebApplication, workItem.ItemGuid);
                    }

                    if (!String.IsNullOrEmpty(workItem.TextPayload))
                    {
                        try
                        {
                            string[] param = workItem.TextPayload.Split(new char[] { ';' });

                            Assembly assembly   = System.Reflection.Assembly.Load(param[1]);
                            Type     moduletype = null;
                            Type[]   types      = assembly.GetTypes();

                            foreach (Type type in types)
                            {
                                string name = type.FullName;
                                if (name.Equals(param[0]))
                                {
                                    moduletype = type;
                                    break;
                                }
                            }

                            if (moduletype != null)
                            {
                                SPWebApplication WebApp = contentDatabase.WebApplication;
                                //WebSiteControllerConfig config = WebApp.GetChild<WebSiteControllerConfig>(WebSiteControllerConfig.OBJECTNAME);
                                IWebSiteControllerModule module = (IWebSiteControllerModule)Activator.CreateInstance(moduletype);
                                WebSiteControllerConfig.AddModule(WebApp, module, param[0] + "," + param[1]);
                            }
                        }
                        catch (Exception ex)
                        {
                            ex.ToString();
                            //throw (ex);
                        }
                        finally
                        {
                            DeleteWorkItem(workItems, workItem);

                            /*
                             * using (SPSite site = new SPSite(workItem.SiteId))
                             * {
                             *
                             *  using (SPWeb web = site.OpenWeb(workItem.WebId))
                             *  {
                             *      workItems.CompleteInProgressWorkItems(workItem.ParentId, workItem.Type, workItem.BatchId);
                             *      //workItems.SubCollection(site, web, 0, (uint)workItems.Count).DeleteWorkItem(workItem.Id);
                             *
                             *  }
                             *
                             * }
                             */
                        }
                    }
                }
            }
            return(true);
        }
示例#15
0
        private void CreateWorkItem(string url)
        {
            Guid siteId = SPContext.Current.Site.ID;
            Guid webId  = SPContext.Current.Web.ID;

            bool disabled = false;
            WebSiteControllerPrincipalType principalType = WebSiteControllerPrincipalType.None;
            bool   appliesToSSL = true;
            int    sequence     = 1;
            String pricipal     = string.Empty;

            StringBuilder builder = new StringBuilder();

            builder.Append(url + ";");
            builder.Append(disabled.ToString() + ";");
            builder.Append(appliesToSSL.ToString() + ";");
            builder.Append(sequence.ToString() + ";");
            builder.Append(principalType.ToString() + ";");
            builder.Append(pricipal + ";");
            builder.Append("#");

            builder.Append(String.Format("{0}:{1};", "OriginalUrl", _url));

            string full = builder.ToString();

            SemanticModule           mod  = new SemanticModule();
            IWebSiteControllerModule imod = null;// WebSiteControllerConfig.GetModule(web.Site.WebApplication, mod.RuleType);

            while (imod == null)
            {
                System.Threading.Thread.Sleep(1000);
                try
                {
                    imod = WebSiteControllerConfig.GetModule(SPContext.Current.Site.WebApplication, mod.RuleType);
                }
                catch (Exception ex)
                {
                    SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data);
                    //ex.ToString();
                }
            }


            int item = -1;

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (SPSite site = new SPSite(siteId))
                {
                    site.AddWorkItem(
                        Guid.NewGuid(),
                        DateTime.Now.ToUniversalTime(),
                        WebSiteControllerRuleWorkItem.WorkItemTypeId,
                        webId,
                        siteId,
                        item,
                        true,
                        imod.Id,
                        Guid.Empty,
                        site.SystemAccount.ID,
                        null,
                        builder.ToString(),
                        Guid.Empty
                        );
                }

                SPJobDefinitionCollection jobs = SPContext.Current.Site.WebApplication.JobDefinitions;

                foreach (SPJobDefinition job in jobs)
                {
                    if (job.Name == WebSiteControllerRuleWorkItem.WorkItemJobDisplayName)
                    {
                        try
                        {
                            DateTime next = job.Schedule.NextOccurrence(job.LastRunTime);
                            _seconds      = next.Second;
                            break;
                        }
                        catch (Exception ex)
                        {
                            SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data);
                            //ex.ToString();
                        }
                    }
                }
            });
        }
示例#16
0
        private void CreateErrorWorkItem(SPWeb web, HttpStatusCode code)
        {
            try
            {
                Guid siteId = web.Site.ID;
                Guid webId  = web.ID;

                bool disabled = false;
                WebSiteControllerPrincipalType principalType = WebSiteControllerPrincipalType.None;
                bool   appliesToSSL = true;
                int    sequence     = 1;
                String pricipal     = string.Empty;

                StringBuilder builder = new StringBuilder();
                builder.Append("Error/" + code.ToString() + ".aspx;");
                builder.Append(disabled.ToString() + ";");
                builder.Append(appliesToSSL.ToString() + ";");
                builder.Append(sequence.ToString() + ";");
                builder.Append(principalType.ToString() + ";");
                builder.Append(pricipal + ";");
                builder.Append("#");

                builder.Append(String.Format("{0}:{1};", "ErrorPage", "Error/" + code.ToString() + ".aspx;"));
                builder.Append(String.Format("{0}:{1};", "ErrorCode", ((int)code).ToString()));


                string full = builder.ToString();

                ErrorModule mod = new ErrorModule();
                IWebSiteControllerModule imod = null;  //WebSiteControllerConfig.GetModule(web.Site.WebApplication, mod.RuleType);

                while (imod == null)
                {
                    System.Threading.Thread.Sleep(1000);
                    try
                    {
                        imod = WebSiteControllerConfig.GetModule(web.Site.WebApplication, mod.RuleType);
                    }
                    catch (Exception ex)
                    {
                        SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data);
                        //ex.ToString();
                    }
                }

                //Guid itemGuid = new Guid("17A3219B-049F-4056-9566-37590122BE8E");
                int item = -1;

                SPSecurity.RunWithElevatedPrivileges(() =>
                {
                    using (SPSite site = new SPSite(siteId))
                    {
                        site.AddWorkItem(
                            Guid.NewGuid(),
                            DateTime.Now.ToUniversalTime(),
                            WebSiteControllerRuleWorkItem.WorkItemTypeId,
                            webId,
                            siteId,
                            item,
                            true,
                            imod.Id,
                            Guid.Empty,
                            site.SystemAccount.ID,
                            null,
                            builder.ToString(),
                            Guid.Empty
                            );
                    }
                });

                try
                {
                    WebSiteControllerRuleWorkItem WebSiteControllerModuleJob = new WebSiteControllerRuleWorkItem(WebSiteControllerRuleWorkItem.WorkItemJobDisplayName + code.ToString(), web.Site.WebApplication);
                    SPOneTimeSchedule             oneTimeSchedule            = new SPOneTimeSchedule(DateTime.Now);

                    WebSiteControllerModuleJob.Schedule = oneTimeSchedule;
                    WebSiteControllerModuleJob.Update();
                }
                catch (Exception ex)
                {
                    SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data);
                    //ex.ToString();
                }
            }
            catch (Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data);
            }
        }
        private void CreateWorkItem(SPWeb web, string pagename, string url)
        {
            Guid siteId = web.Site.ID;
            Guid webId  = web.ID;
            //string url = properties.ServerRelativeUrl;

            /*
             * if (url.StartsWith("/"))
             * {
             *  url = url.TrimStart('/');
             * }
             */

            bool disabled = false;
            WebSiteControllerPrincipalType principalType = WebSiteControllerPrincipalType.None;
            bool   appliesToSSL = true;
            int    sequence     = 1;
            String pricipal     = string.Empty;

            StringBuilder builder = new StringBuilder();

            builder.Append(url + ";");
            builder.Append(disabled.ToString() + ";");
            builder.Append(appliesToSSL.ToString() + ";");
            builder.Append(sequence.ToString() + ";");
            builder.Append(principalType.ToString() + ";");
            builder.Append(pricipal + ";");
            builder.Append("#");

            //builder.Append(String.Format("{0}:{1};", "OriginalUrl", url));
            //string full = builder.ToString();

            if (url.EndsWith("/"))
            {
                if (!pagename.EndsWith(".aspx"))
                {
                    builder.Append(String.Format("{0}:{1};", "OriginalUrl", pagename + ".aspx"));
                }
                else
                {
                    builder.Append(String.Format("{0}:{1};", "OriginalUrl", pagename));
                }
            }
            else
            {
                if (!url.EndsWith(".aspx"))
                {
                    builder.Append(String.Format("{0}:{1};", "OriginalUrl", url + pagename + ".aspx"));
                }
                else
                {
                    builder.Append(String.Format("{0}:{1};", "OriginalUrl", url + pagename));
                }
            }

            string full = builder.ToString();

            //Guid itemGuid = new Guid("386577D9-0777-4AD3-A90A-C240D8B0A49E");
            SemanticModule           mod  = new SemanticModule();
            IWebSiteControllerModule imod = null;// WebSiteControllerConfig.GetModule(web.Site.WebApplication, mod.RuleType);

            while (imod == null)
            {
                System.Threading.Thread.Sleep(1000);
                try
                {
                    imod = WebSiteControllerConfig.GetModule(web.Site.WebApplication, mod.RuleType);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }


            int item = -1;

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (SPSite site = new SPSite(siteId))
                {
                    site.AddWorkItem(
                        Guid.NewGuid(),
                        DateTime.Now.ToUniversalTime(),
                        WebSiteControllerRuleWorkItem.WorkItemTypeId,
                        webId,
                        siteId,
                        item,
                        true,
                        imod.Id,
                        Guid.Empty,
                        site.SystemAccount.ID,
                        null,
                        builder.ToString(),
                        Guid.Empty
                        );
                }
            });

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(siteId, SPUserToken.SystemAccount))
                {
                    try
                    {
                        WebSiteControllerRuleWorkItem WebSiteControllerModuleJob = new WebSiteControllerRuleWorkItem(WebSiteControllerRuleWorkItem.WorkItemJobDisplayName + "HomePage", site.WebApplication);
                        SPOneTimeSchedule oneTimeSchedule = new SPOneTimeSchedule(DateTime.Now);

                        WebSiteControllerModuleJob.Schedule = oneTimeSchedule;
                        WebSiteControllerModuleJob.Update();
                    }
                    catch { };
                }

                /*
                 * if (SPContext.Current != null)
                 * {
                 *  SPJobDefinitionCollection jobs = SPContext.Current.Site.WebApplication.JobDefinitions;
                 *
                 *  int _seconds = 0;
                 *
                 *  foreach (SPJobDefinition job in jobs)
                 *  {
                 *      if (job.Name == WebSiteControllerRuleWorkItem.WorkItemJobDisplayName)
                 *      {
                 *          DateTime next = job.Schedule.NextOccurrence(job.LastRunTime);
                 *          _seconds = next.Second;
                 *          break;
                 *      }
                 *  }
                 * }
                 */
            });

            /*
             * SPSecurity.RunWithElevatedPrivileges(() =>
             * {
             *  using (SPSite site = new SPSite(siteId))
             *  {
             *      site.AddWorkItem(
             *          Guid.NewGuid(),
             *          DateTime.Now.ToUniversalTime(),
             *          WebSiteControllerRuleWorkItem.WorkItemTypeId,
             *          webId,
             *          siteId,
             *          item,
             *          true,
             *          imod.Id,
             *          Guid.Empty,
             *          site.SystemAccount.ID,
             *          null,
             *          builder.ToString(),
             *          Guid.Empty
             *          );
             *
             *      try
             *      {
             *
             *          WebSiteControllerRuleWorkItem WebSiteControllerModuleJob = new WebSiteControllerRuleWorkItem(WebSiteControllerRuleWorkItem.WorkItemJobDisplayName + "HomePage", site.WebApplication);
             *          SPOneTimeSchedule oneTimeSchedule = new SPOneTimeSchedule(DateTime.Now);
             *
             *          WebSiteControllerModuleJob.Schedule = oneTimeSchedule;
             *          WebSiteControllerModuleJob.Update();
             *      }
             *      catch (Exception ex)
             *      {
             *          SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(ex.Source, TraceSeverity.High, EventSeverity.Error), TraceSeverity.High, ex.Message, ex.Data);
             *          //ex.ToString();
             *      }
             *
             *  }
             *
             *  SPJobDefinitionCollection jobs = SPContext.Current.Site.WebApplication.JobDefinitions;
             *
             *  int _seconds = 0;
             *
             *  foreach (SPJobDefinition job in jobs)
             *  {
             *      if (job.Name == WebSiteControllerRuleWorkItem.WorkItemJobDisplayName)
             *      {
             *          DateTime next = job.Schedule.NextOccurrence(job.LastRunTime);
             *          _seconds = next.Second;
             *          break;
             *      }
             *  }
             * });
             */
        }