コード例 #1
0
        private List <WebListItem> GetSiteList()
        {
            // code to iterate through user's list of SPWebs
            bool addCurrentToList = true;
            int  mruItems         = SlkStore.Settings.UserWebListMruSize;
            int  itemMax;
            int  listCount;
            ReadOnlyCollection <SlkUserWebListItem> userWebList = SlkStore.FetchUserWebList();

            foreach (SlkUserWebListItem webListItem in userWebList)
            {
                if (SPWeb.ID.Equals(webListItem.SPWebGuid))
                {
                    addCurrentToList = false;
                    break;
                }
            }

            if (addCurrentToList)
            {
                listCount = userWebList.Count + 1;
            }
            else
            {
                listCount = userWebList.Count;
            }

            if (!ShowAllSites && listCount > mruItems)
            {
                itemMax = mruItems;
                lnkMRUShowAll.Visible = true;
                lnkMRUShowAll.Text    = PageCulture.Resources.ActionslnkMRUShowAll;
            }
            else
            {
                itemMax = listCount;
                lnkMRUShowAll.Visible = false;
            }

            List <WebListItem> webList = new List <WebListItem>(itemMax);

            if (addCurrentToList)
            {
                itemMax--;
            }

            for (int i = 0; i < itemMax && i < userWebList.Count; i++)
            {
                bool previousValue = SPSecurity.CatchAccessDeniedException;
                SPSecurity.CatchAccessDeniedException = false;
                try
                {
                    SlkUserWebListItem item = userWebList[i];
                    using (SPSite site = new SPSite(item.SPSiteGuid, SPContext.Current.Site.Zone))
                    {
                        using (SPWeb web = site.OpenWeb(item.SPWebGuid))
                        {
                            if (SPWeb.ID.Equals(item.SPWebGuid))
                            {
                                webList.Add(new WebListItem(item, web.Url, PageCulture.Format("{0} {1}", web.Title, PageCulture.Resources.ActionslblMRUCurrentSite)));
                            }
                            else
                            {
                                webList.Add(new WebListItem(item, web.Url, web.Title));
                            }
                        }
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    // the user doesn't have permission to access this site, so ignore it
                    continue;
                }
                catch (System.Data.SqlClient.SqlException)
                {
                    // site is in another web application and this web application's app pool user doesn't have access
                    continue;
                }
                catch (FileNotFoundException)
                {
                    // the site doesn't exist
                    continue;
                }
                finally
                {
                    SPSecurity.CatchAccessDeniedException = previousValue;
                }
            }

            if (addCurrentToList)
            {
                webList.Add(new WebListItem(SPWeb.Site.ID, SPWeb.ID, DateTime.Now, SPWeb.Url,
                                            PageCulture.Format("{0} {1}", SPWeb.Title, PageCulture.Resources.ActionslblMRUCurrentSite)));
            }
            webList.Sort();

            return(webList);
        }
コード例 #2
0
        protected void addButton_Click(object sender, EventArgs e)
        {
            // user clicked "Add" button (after clicking "Add a site to this list")

            bool showAddSite = true;

            bool previousValue = SPSecurity.CatchAccessDeniedException;

            SPSecurity.CatchAccessDeniedException = false;
            try
            {
                string destinationUrl = txtNewSite.Text.Trim();
                Uri    destinationUri = new Uri(destinationUrl);
                using (SPSite site = new SPSite(destinationUri.AbsoluteUri))
                {
                    using (SPWeb destinationWeb = site.OpenWeb())
                    {
                        // check if the site is a valid Slk site
                        SlkStore destinationSlkStore;
                        try
                        {
                            destinationSlkStore = SlkStore.GetStore(destinationWeb);
                        }
                        catch (SlkNotConfiguredException)
                        {
                            errorBanner.AddHtmlErrorText(ErrorType.Warning, PageCulture.Format(PageCulture.Resources.ActionsNotEnabled, Server.HtmlEncode(destinationUrl)));
                            DisplayAddSite();
                            return;
                        }

                        // check if the user is an instructor on that site
                        if (!destinationSlkStore.IsInstructor(destinationWeb))
                        {
                            errorBanner.AddHtmlErrorText(ErrorType.Warning, PageCulture.Format(PageCulture.Resources.ActionsNotInstructor, Server.HtmlEncode(destinationUrl)));
                            DisplayAddSite();
                            return;
                        }

                        // check if the site is already in the list
                        ReadOnlyCollection <SlkUserWebListItem> userWebList = SlkStore.FetchUserWebList();
                        foreach (SlkUserWebListItem webListItem in userWebList)
                        {
                            if (destinationWeb.ID.Equals(webListItem.SPWebGuid))
                            {
                                errorBanner.AddHtmlErrorText(ErrorType.Info, PageCulture.Format(PageCulture.Resources.ActionsAlreadyInList, Server.HtmlEncode(destinationWeb.Title)));
                                break;
                            }
                        }

                        // add the web to the list
                        SlkStore.AddToUserWebList(destinationWeb); //local slkstore
                        ShowAllSites    = false;
                        txtNewSite.Text = string.Empty;
                        newSite         = destinationWeb.ID;
                        showAddSite     = false;
                    }
                }
            }
            catch (UriFormatException)
            {
                // the url is an invalid format
                errorBanner.AddHtmlErrorText(ErrorType.Warning, PageCulture.Format(PageCulture.Resources.ActionsInvalidSite, Server.HtmlEncode(txtNewSite.Text)));
            }
            catch (UnauthorizedAccessException)
            {
                // the user doesn't have permission to access this site, so show an error message
                errorBanner.AddHtmlErrorText(ErrorType.Warning, PageCulture.Format(PageCulture.Resources.ActionsInvalidSite, Server.HtmlEncode(txtNewSite.Text)));
            }
            catch (FileNotFoundException)
            {
                errorBanner.AddHtmlErrorText(ErrorType.Warning, PageCulture.Format(PageCulture.Resources.ActionsInvalidSite, Server.HtmlEncode(txtNewSite.Text)));
            }
            finally
            {
                SPSecurity.CatchAccessDeniedException = previousValue;
            }
            if (showAddSite)
            {
                DisplayAddSite();
            }
        }