Get A SharePoint Web Based On List Existence

This code sample demonstrates how to get a specific web based on a SharePoint list, returning the specific web. It also includes several layers of exception handling and various properties that can be set.

[csharp]

protected SPWeb ExampleListWeb
{
        get
        {
                if (this.ExampleListWeb == null)
                {
                        try
                        {
                                if (this.ListWebUrl.Trim() != string.Empty)
                                {
                                        try
                                        {
                                                SPSite exampleSite = new SPSite(new Uri(this.Page.Request.Url, this.ListWebUrl, true).ToString());
                                                exampleSite.CatchAccessDeniedException = false;
                                                this.ExampleListWeb = exampleSite.OpenWeb();
                                        }
                                        catch (Exception exception)
                                        {
                                                throw (exception);
                                        }
                                }
                                if (this.ExampleListWeb == null)
                                {
                                        this.ExampleListWeb = SPControl.GetContextWeb(this.Context);
                                }
                                this.ExampleListWeb.AllowUnsafeUpdates = true;
                                this.ExampleListWeb.Lists.IncludeRootFolder = true;
                                this.ExampleListWeb.Lists.ListsForCurrentUser = true;
                        }
                        catch (Exception exception)
                        {
                                throw(exception);
                        }
                }
                return this.ExampleListWeb;
        }
}

[/csharp]

Share

Leave a Reply

Your email address will not be published. Required fields are marked *