protected bool CheckCredentialsOnSharepointServer()
        {
            try
            {
                this.AttemptNumber = 0;
                this.HideErrorLabel();
            }
            catch (SoapException)
            {
                this.errorLbl.Text = string.Format("There is no '{0}' list in the '{1}' web.", this.ListName, this.Web);
                this.ShowErrorLabel();
                return(false);
            }
            catch (WebException ex)
            {
                this.errorLbl.Text = Translate.Text(UIMessages.CouldntGetResponseFromSharepointServer);
                var webResponse = ex.Response as HttpWebResponse;
                if ((webResponse != null) && (webResponse.StatusCode == HttpStatusCode.Unauthorized) &&
                    webResponse.Headers.AllKeys.Contains("WWW-Authenticate"))
                {
                    this.errorLbl.Text = Translate.Text(UIMessages.YouDoNotHaveEnoughRights);
                    if (this.AttemptNumber < 3)
                    {
                        SharepointExtension.WriteAuthenticationResponseBasic(this.Request, this.Response);
                        this.AttemptNumber++;
                    }
                }

                this.ShowErrorLabel();
                return(false);
            }

            return(true);
        }
        protected void advancedSearchBtn_Click(object sender, EventArgs e)
        {
            advancedSearchBtn.Text          = "»&nbsp;" + Translate.Text(UIMessages.AdvancedSearch);
            advancedSearchSitesPane.Visible = !advancedSearchSitesPane.Visible;
            advancedSearchListsPane.Visible = !advancedSearchListsPane.Visible;

            if (advancedSearchSitesPane.Visible)
            {
                advancedSearchBtn.Text = "»&nbsp;" + Translate.Text(UIMessages.AdvancedSearch);
                Server spServer = ObjectModel.Entities.Server.Connect(SpContext);
                try
                {
                    List <System.Web.UI.WebControls.ListItem> webs = new List <System.Web.UI.WebControls.ListItem> {
                        new System.Web.UI.WebControls.ListItem(Translate.Text(UIMessages.AllSites), this.Web)
                    };
                    webs.AddRange(spServer.Webs.Select(web => new ListItem(web.Title, web.Path)));
                    if ((webs.Count > 0))
                    {
                        sitesList.DataSource = webs;
                    }
                    sitesList.DataTextField  = "Text";
                    sitesList.DataValueField = "Value";
                    sitesList.DataBind();

                    listsList.DataSource = new[] { new System.Web.UI.WebControls.ListItem(Translate.Text(UIMessages.AllLists)) };
                    listsList.DataBind();
                    AttemptNumber = 0;
                }
                catch (WebException ex)
                {
                    sitesList.DataSource = new[] { new System.Web.UI.WebControls.ListItem(Translate.Text(UIMessages.AllSites), this.Web) };
                    sitesList.DataBind();

                    listsList.DataSource = new[] { new System.Web.UI.WebControls.ListItem(Translate.Text(UIMessages.AllLists)) };
                    listsList.DataBind();

                    errorLbl.Text = Translate.Text(UIMessages.CouldntGetResponseFromSharepointServer);
                    HttpWebResponse webResponse = ex.Response as HttpWebResponse;
                    if ((webResponse != null) && (webResponse.StatusCode == HttpStatusCode.Unauthorized) &&
                        (webResponse.Headers.AllKeys.Contains("WWW-Authenticate")))
                    {
                        errorLbl.Text = Translate.Text(UIMessages.YouDoNotHaveEnoughRights);
                        if (AttemptNumber < 3)
                        {
                            SharepointExtension.WriteAuthenticationResponseBasic(Request, Response);
                            AttemptNumber++;
                        }
                    }
                    this.ShowErrorLabel();
                    return;
                }
                catch (SoapException ex)
                {
                    SharepointUtils.LogDebugInfo(SpContext, "Can't retrieve info for advanced search");
                    return;
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string queryString        = Attributes["sc_parameters"];
            NameValueCollection param = Sitecore.Web.WebUtil.ParseUrlParameters(queryString);

            if (!string.IsNullOrEmpty(queryString))
            {
                if (!string.IsNullOrEmpty(param["Web"]))
                {
                    this.Web = param["Web"];
                }

                if (!string.IsNullOrEmpty(param["Server"]))
                {
                    this.ServerUrl = param["Server"];
                }

                if (!string.IsNullOrEmpty(param["SharepointServerType"]))
                {
                    this.SharepointServerType = param["SharepointServerType"];
                }
            }

            if (string.IsNullOrEmpty(this.Web))
            {
                this.Web = SharepointUtils.CurrentWebPath;
            }

            if (string.IsNullOrEmpty(this.ServerUrl))
            {
                ServerUrl = SharepointUtils.CurrentSharepointServer;
            }

            this.SpContext = SpContextProviderBase.Instance.CreateUIContext(this.ServerUrl, this.Web);
            NetworkCredential headerCredential = SharepointExtension.GetCredentialsFromHeader(Request);

            if (headerCredential != null)
            {
                SpContext.Credentials = headerCredential;
            }

            if (!string.IsNullOrEmpty(queryString) && string.IsNullOrEmpty(this.ListName) && !string.IsNullOrEmpty(param["List"]))
            {
                var list = ObjectModel.Entities.Lists.BaseList.GetList(SpContext.Url, param["List"], SpContext);
                if (list != null)
                {
                    this.ListName = list is ObjectModel.Entities.Lists.List ? "Lists/" + list.Name : list.Name;
                }
            }
        }
        protected void Initialize()
        {
            string queryString = Attributes["sc_parameters"];

            if (!string.IsNullOrEmpty(queryString))
            {
                NameValueCollection param = Sitecore.Web.WebUtil.ParseUrlParameters(queryString);
                if (string.IsNullOrEmpty(this.Web))
                {
                    if (string.IsNullOrEmpty(param["Web"]) && string.IsNullOrEmpty(param["Server"]))
                    {
                        this.Web = SharepointUtils.CurrentWebPath;
                    }
                    else
                    {
                        this.Web = param["Web"];
                    }
                }

                if (!string.IsNullOrEmpty(param["Server"]))
                {
                    this.Server = param["Server"];
                }

                if (string.IsNullOrEmpty(this.ListName))
                {
                    this.ListName = param["List"];
                    this.ListName = this.ListName.Replace("%2b", " ");
                    this.ListName = this.ListName.Replace("+", " ");
                }

                this.ShowIfEmpty = MainUtil.GetBool(param["ShowIfEmpty"], false);
            }

            this.context = SpContextProviderBase.Instance.CreateUIContext(this.Server, this.Web);
            NetworkCredential headerCredential = SharepointExtension.GetCredentialsFromHeader(Request);

            if (headerCredential != null)
            {
                this.context.Credentials = headerCredential;
            }
        }
        protected void sitesList_IndexChanged(object sender, EventArgs e)
        {
            List <System.Web.UI.WebControls.ListItem> lists = new List <System.Web.UI.WebControls.ListItem> {
                new System.Web.UI.WebControls.ListItem(Translate.Text(UIMessages.AllLists))
            };

            listsList.DataSource = null;
            listsList.Items.Clear();
            string siteName = sitesList.SelectedValue;

            if (!string.IsNullOrEmpty(siteName) && (sitesList.SelectedIndex > 0))
            {
                Server spServer = ObjectModel.Entities.Server.Connect(SpContext);
                try
                {
                    Web selectedWeb = spServer.Webs.First(web => web.Path == siteName);
                    if (selectedWeb != null)
                    {
                        lists.AddRange(selectedWeb.Lists.Select(list => new ListItem(list.Name, list is ObjectModel.Entities.Lists.List ? "Lists/" + list.Name : list.Name)));
                    }
                }
                catch (WebException ex)
                {
                    HttpWebResponse webResponse = ex.Response as HttpWebResponse;
                    if ((webResponse != null) && (webResponse.StatusCode == HttpStatusCode.Unauthorized) &&
                        (webResponse.Headers.AllKeys.Contains("WWW-Authenticate")))
                    {
                        SharepointExtension.WriteAuthenticationResponseBasic(Request, Response);
                    }
                    return;
                }
                catch (SoapException ex)
                {
                    SharepointUtils.LogDebugInfo(SpContext, "Couldn't retrieve lists for {0} site.\n{1}", siteName, ex.StackTrace);
                    return;
                }
            }
            listsList.DataTextField  = "Text";
            listsList.DataValueField = "Value";
            listsList.DataSource     = lists;
            listsList.DataBind();
        }
        protected void searchBtn_Click(object sender, EventArgs e)
        {
            string siteName = advancedSearchSitesPane.Visible ? sitesList.SelectedValue : this.Web;
            string listName = advancedSearchSitesPane.Visible ? listsList.SelectedValue : this.ListName;

            listName = (listName == Translate.Text(UIMessages.AllLists)) ? string.Empty : listName;

            Query searchQuery = new Query(searchBox.Text)
            {
                Range = new QueryRange {
                    Count = pageSize, StartAt = 1
                }
            };

            if (!string.IsNullOrEmpty(siteName) || !string.IsNullOrEmpty(listName))
            {
                searchQuery.AddFilter(siteName, listName);
            }

            try
            {
                SearchBase searchServer;
                if (this.SharepointServerType == "WSS")
                {
                    searchServer = new WSSSearch(SpContext);
                }
                else
                {
                    searchServer = new MOSSSearch(SpContext);
                }
                SearchResultCollection searchResultCollection = searchServer.Search(searchQuery);
                searchResults.DataSource = searchResultCollection;
                if ((searchResultCollection == null) || (searchResultCollection.Count == 0))
                {
                    searchResults.HeaderTemplate =
                        new CompiledTemplateBuilder(
                            headerControl =>
                            headerControl.Controls.Add(new Literal {
                        Text = Translate.Text(UIMessages.NoResultsCouldBeFoundForThisQuery)
                    }));
                }
                searchResults.DataBind();
                this.HideErrorLabel();
                AttemptNumber = 0;
            }
            catch (UriFormatException ex)
            {
                errorLbl.Text = Translate.Text(UIMessages.YouDoNotHaveAccessToSharepointSearchService);
                ShowErrorLabel();
                return;
            }
            catch (WebException ex)
            {
                errorLbl.Text = Translate.Text(UIMessages.CouldntGetResponseFromSharepointServer);
                HttpWebResponse webResponse = ex.Response as HttpWebResponse;
                if ((webResponse != null) && (webResponse.StatusCode == HttpStatusCode.Unauthorized) &&
                    (webResponse.Headers.AllKeys.Contains("WWW-Authenticate")))
                {
                    errorLbl.Text = Translate.Text(UIMessages.YouDoNotHaveEnoughRights);
                    if (AttemptNumber < 3)
                    {
                        SharepointExtension.WriteAuthenticationResponseBasic(Request, Response);
                        AttemptNumber++;
                    }
                }
                this.ShowErrorLabel();
                return;
            }
            catch (SoapException ex)
            {
                if (ex.Detail.InnerText == "ERROR_ALL_NOISE")
                {
                    this.errorLbl.Text = Translate.Text(UIMessages.YourQueryIncludedOnlyCommonWordsAndOrCharactersWhichWereRemovedNoResultsAreAvailableTryToAddQueryTerms);
                }
                else
                {
                    errorLbl.Text = Translate.Text(UIMessages.YouDoNotHaveAccessToSharepointSearchService);
                }
                ShowErrorLabel();
                return;
            }
        }