1

I have tried multiple different ways to get this code to work. I will post both methods of code. Neither ones work. The idea is to get all the "Sites a user can access", and create a hyperlink within a visual web part. We need to get each web application the site collections, and then each web below NOT including subsites.

The environment is SharePoint 2013 OnPremise. This is a FarmSolution.

protected override void CreateChildControls()
    {
        base.CreateChildControls();
        SPServiceCollection services = SPFarm.Local.Services;

        foreach (SPService eachService in services)
        {
            if (eachService is SPWebService)
            {
                SPWebService webService = (SPWebService)eachService;
                foreach (SPWebApplication webApp in webService.WebApplications)
                {
                    if (!webApp.DisplayName.Contains("MySites"))
                    {
                        SPSiteCollection siteColl = webApp.Sites;
                        foreach (SPSite site in siteColl)
                        {
                            site.CatchAccessDeniedException = false;
                            if (site.RootWeb.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open))
                                try
                                {
                                    HyperLink hyp = new HyperLink();
                                               hyp.ID = "dynamicLinks";
                                               hyp.Text = "Link";
                                               hyp.CssClass = "SitesAccessLinks";
                                               hyp.NavigateUrl = site.Url.ToString();
                                               this.Controls.Add(hyp);
                                               foreach (SPWeb childweb in site.RootWeb.Webs)
                                               {
                                                   if (childweb.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.Open))
                                                   {
                                                       try
                                                       {
                                                           HyperLink clink = new HyperLink();
                                                           clink.ID = "DynamicLinks";
                                                           clink.Text = "link";
                                                           clink.CssClass = "WebAccessLinks";
                                                           clink.NavigateUrl = childweb.Url.ToString();
                                                           this.Controls.Add(clink);
                                                       }
                                                       finally
                                                       {
                                                           childweb.Dispose();
                                                       }
                                                   }
                                                   site.CatchAccessDeniedException = false;
                                               }
                                }
                                finally
                                {
                                    site.Dispose();
                                }

The code above returns a permissions issue. Without debugging, the website returns 403 The website declined to show this webpage. The code below does as well.

    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        SPServiceCollection services = SPFarm.Local.Services;

        foreach (SPService eachService in services)
        {
            if (eachService is SPWebService)
            {
                SPWebService webService = (SPWebService)eachService;
                foreach (SPWebApplication webApp in webService.WebApplications)
                {
                    if (!webApp.DisplayName.Contains("MySites"))
                    {
                        SPSiteCollection siteColl = webApp.Sites;
                        foreach (SPSite site in siteColl)
                        {
                            try
                            {
                                site.CatchAccessDeniedException = false;
                                using (SPSite indiSite = new SPSite(site.Url))
                                {
                                    using (SPWeb oWebSite = indiSite.OpenWeb())
                                    {
                                      if (!oWebSite.IsRootWeb)
                                        {

                                            if (oWebSite.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, SPBasePermissions.EnumeratePermissions))
                                            {
                                                SPBasePermissions permissionToCheck = SPBasePermissions.Open;
                                                Label lbl9 = new Label();
                                                lbl9.Text = "WOLOLololo";
                                                this.Controls.Add(lbl9);
                                                if (oWebSite.DoesUserHavePermissions(SPContext.Current.Web.CurrentUser.LoginName, permissionToCheck))
                                                {
                                                    Label hyp = new Label();
                                                    hyp.ID = "dynamicLinks";
                                                    hyp.Text = oWebSite.Title.ToString();
                                                    hyp.CssClass = "SitesAccessLinks";
                                                    ///   hyp.NavigateUrl = "https://google.com";
                                                    this.Controls.Add(hyp);
                                                }
                                            }
                                            else
                                            {
                                                Label lbl11 = new Label();
                                                lbl11.Text = "Can't enumerate";
                                                this.Controls.Add(lbl11);
                                            }
                                        }
                                        else
                                        {
                                            Label lbl8 = new Label();
                                            lbl8.Text = oWebSite.Title.ToString();
                                            this.Controls.Add(lbl8);
                                        }
                                    }
                                }
                            }

                            finally
                            {
                                site.CatchAccessDeniedException = true;
                            }
2

The best way to list sites a user has access to is do a search for: contentclass:STS_Web

If you really really want to implement the slow way then you need to use SPSecurity.RunWithElevatedPrivileges as you'll come across web applications/site collection the current user doesn't have access to

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.