Retrieving SharePoint Site Information in an ASP.NET Web Application

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

By Gayan Peiris

 

It is a common requirement to access site specific information of a SharePoint site in a central location. Currently the SharePoint site information is available through number of different SharePoint Administration pages. This makes gathering information of a site difficult by making the Administrators remember the various places they need to navigate to access the necessary information. This exercise gets more difficult and frustrating when you have to gather information for multiple sites. Just think about all the navigation you will be doing back and front to access information of different sites.

 

In this article I am looking at displaying the SharePoint site information in an ASP.NET web application. The user will have the ability to enter a SharePoint Portal server site URL or a Windows SharePoint Services site collection URL in a web page. According to the URL been provided, the web page will display a list of sites available in a dropdown list. The web page will then display the site information according to the selected site. Users may enter the specific site URL if they are aware of the URL for the site they are seeking information.

 

The article is looking at SharePoint Object Model to access the site information. The ASP.NET web application is developed in a Visual Studio environment. The SPSite class and SPWeb class from SharePoint Object model is used to retrieve the site information.

 

Entering a Windows SharePoint Services Site Collection URL

Figure 1 displays the site information of a Windows SharePoint Services Site

The Properties of the selected site URL

Figure 1: The Properties of the selected site URL

Collection of the sub sites available from the site collection URL

Figure 2: Collection of the sub sites available from the site collection URL

SPSite Class

The SPSite class represents a collection of sites on a virtual server, including a top-level site and all its sub sites. Each SPSite object, or site collection, is represented within a SPSiteCollection object that consists of the collection of all site collections on the virtual server.

To instantiate a SPSite object for a specific site collection on an ASP.NET page, or for a specific site collection within a console application, developers can use the SPSite constructor by either passing the absolute URL or the site collection GUID.

I have used the absolute URL to instantiate the SPSite object in my code example as displayed below,

SPSite mySiteCollection = new SPSite("Absolute_URL");

The URL can be a SharePoint Portal Server site, a Windows SharePoint Services site or a Windows SharePoint Services site collection.

SPWeb Class

This class represents a SharePoint site. It will contain a collection of information for a single SharePoint site.

The following are a few of the public properties I have used from SPWeb class to display the SharePoint Site details.

Name Description
AllowAnonymousAccess Is anonymous access is allowed for the site.
AllowUnsafeUpdates Whether to allow updates to the database as a result of a GET request.
AllUsers Gets the collection of user objects that represents all users who are either members of the site or who have browsed to the site as authenticated members of a domain group in the site.
AlternateHeader The URL for an alternate .aspx page to use for rendering the top navigation area in SharePoint pages.
AnonymousState The level of access for anonymous users on the site.
AuthenticationMode Value indicating that Windows authentication is the default authentication mode.
Author User object representing the user who created the Web site.
Description The description for the site.
EmailInsertsEnabled Indicates whether document libraries on the virtual server can accept e-mail attachments from public folders.
EventHandlersEnabled Indicates whether event handlers can be used in the site.
HasUniquePerm Value that specifies whether the site has unique permissions.
ID The GUID for the site.
IsADAccountCreationMode Value that indicates whether user accounts are automatically created in Active Directory service when users are invited to the Web site.
IsADEmailEnabled Value that indicates whether Active Directory e-mail is enabled on the site.
IsRootWeb Value that indicates whether the site is the top-level Web site of the site collection.
Language The LCID for the language used on the site.
Name The name of the Web site.
ParentWeb The parent Web site for the site.
PortalMember Value that indicates whether the Web site is a member of a portal.
PortalName The name of a portal used with the site.
PortalUrl The URL to a portal used with the site.
Theme The name of the theme that is applied to the Web site.
ThemeCssUrl The URL for the cascading style sheets file used in the theme for the Web site.
Title Title for the Web site.
Url The absolute URL for the Web site.

Code Example

The web page contains a text box to enter the site URL. By clicking the submit button, web page populates the dropdown list with sub sites details if it exists and displays the site information.

First of all you will need to add the Microsoft.SharePoint.dll to your web application reference list. This will give us the access to the SharePoint Object Model.

Then instantiate the SPSite object as displayed below. I have written this code in Click event of the Submit button. The absolute URL is passed in through the txtWSSSiteUrl text box. This will populate site collection for the given URL.

//Get the site collection
SPSite mySiteCollection = new SPSite(txtWSSSiteUrl.Text);

Then to access an individual site, instantiate the SPWeb object as displayed below. I am passing in the site name as a parameter.

//Get the details of the selected WSS site
SPWeb site = mySiteCollection.AllWebs[siteName];

After constructing the site SPWeb object, the developer can access the information of the site using the public properties of the SPWeb object as displayed below.

AllowAnonymousAccess Property

//Gets a Boolean value that specifies whether anonymous access is allowed for the site.
LbAllowAnonymouseAccessData.Text = site.AllowAnonymousAccess.ToString();

AllowUnsafeUpdates Property

//Gets a Boolean value that specifies whether to allow updates to the database as a result of a GET request.
LbAllowUnsafeUpdatesData.Text = site.AllowUnsafeUpdates.ToString();

AllUsers Property

This property returns a SPUserCollotion object. It’s a collection of users.

//Gets the collection of user objects that represents all users who are either members of the site or who have browsed to the site as authenticated members of a domain group in the site.
SPUserCollection users = site.AllUsers;

The above code gets the collection of users.

//Create a Data Table to hold the user information
DataTable dtUserTable = new DataTable(); //Create DataTable
DataRow drUserRow;

//Add columns to DataGrid
dtUserTable.Columns.Add(new DataColumn("UserName", typeof(string)));
dtUserTable.Columns.Add(new DataColumn("Email", typeof(string)));
dtUserTable.Columns.Add(new DataColumn("IsSiteAdmin", typeof(string)));

Following code is through the user collection and access a user at a time

foreach(SPUser user in users)
{
drUserRow = dtUserTable.NewRow(); //Create DataRow

    drUserRow[0] = user.Name; //Add the users name
    drUserRow[1] = user.Email; //Add the users Emal

    //Is a user is a site administrator
    drUserRow[2] = user.IsSiteAdmin.ToString();

    // Add the DataRow to the DataTable
    dtUserTable.Rows.Add(drUserRow);
}

// Add the data source to the DataGrid
DGUsers.DataSource = dtUserTable;

//Bind the DataGrid
DGUsers.DataBind();

AlternateHeader Property

//Gets or sets the URL for an alternate .aspx page to use for rendering the top navigation area in SharePoint pages.
LbAlternateHeaderData.Text = site.AlternateHeader;

AnonymousState Property

This property returns a WebAnonymousState object.

//Gets or sets the level of access for anonymous users on the site.
Microsoft.SharePoint.SPWeb.WebAnonymousState AnonymousState = site.AnonymousState;
LbLevelOfAccessData.Text = AnonymousState.ToString();

Get the WebAnonymousState object value and convert to string and apply it to the label control.

AuthenticationMode Property

This property returns an AuthenticationMode object.

//Gets a value indicating that Windows authentication is the default authentication mode.
System.Web.Configuration.AuthenticationMode AuthenticationMode = site.AuthenticationMode;
LbAuthenticationModeData.Text = AuthenticationMode.ToString();

Get the AuthenticationMode object value and convert to string and apply it to the label control.

Author Property

This property returns a SPUser object.

//Gets a user object representing the user who created the Web site.
Microsoft.SharePoint.SPUser SiteAuthor = site.Author;
LbAuthorData.Text = SiteAuthor.ToString();

Get the SPUser object value and convert to string and apply it to the label control.

Description Property

//Gets or sets the description for the site.
LbSiteDescriptionData.Text = site.Description;

EmailInsertsEnabled Property

//Gets a Boolean value that indicates whether document libraries on the virtual server can accept e-mail attachments from public folders.
LbEmailAttachmentsData.Text = site.EmailInsertsEnabled.ToString();

EventHandlersEnabled Property

//Gets a Boolean value that indicates whether event handlers can be used in the site.
LbEventHandlersEnabledData.Text = site.EventHandlersEnabled.ToString();

HasUniquePerm Property

//Gets or sets a Boolean value that specifies whether the site has unique permissions.
LbUniquePermissionsData.Text = site.HasUniquePerm.ToString();

ID Property

This property returns a GUID object.

//Gets the GUID for the site.
System.Guid GuidID = site.ID;
LbSiteGUIDData.Text = GuidID.ToString();

IsADAccountCreationMode Property

//Gets a Boolean value that indicates whether user accounts are automatically created in Active Directory directory service when users are invited to the Web site.
LbADUsersData.Text = site.IsADAccountCreationMode.ToString();

IsADEmailEnabled Property

//Gets a Boolean value that indicates whether Active Directory e-mail is enabled on the site.
LbADEmailData.Text = site.IsADEmailEnabled.ToString();

IsRootWeb Property

//Gets a Boolean value that indicates whether the site is the top-level Web site of the site collection.
LbTopLevelSiteData.Text = site.IsRootWeb.ToString();

Language Property

//Gets the LCID for the language used on the site.
LbLanguageIDData.Text = site.Language.ToString();

Name Property

//Gets the name of the Web site.
LbNameData.Text = site.Name;

ParentWeb Property

This property returns a SPWeb object.

//Gets the parent Web site for the site.
Microsoft.SharePoint.SPWeb parentWeb = site.ParentWeb;
LbParentWebData.Text = parentWeb.Name;

PortalMember Property

//Gets a Boolean value that indicates whether the Web site is a member of a portal.
LbPortalMemberData.Text = site.PortalMember.ToString();

PortalName Property

//Gets the name of a portal used with the site.
LbPortalNameData.Text = site.PortalName;

PortalUrl Property

//Gets the URL to a portal used with the site.
LbPortalUrlData.Text = site.PortalUrl;

Theme Property

//Gets the name of the theme that is applied to the Web site.
LbThemeData.Text = site.Theme;

ThemeCssUrl Property

//Gets the URL for the cascading style sheets file used in the
//theme for the Web site.
LbThemeURLData.Text = site.ThemeCssUrl;

Title Property

//Gets the title for the Web site.
LbTitleData.Text = site.Title;

Url Property

//Gets the absolute URL for the Web site.
LbURLData.Text = site.Url;

Deploying the Web Application to the SharePoint Portal Server

  • Create the SPSAdminWeb Virtual directory under the portal web site in IIS

  • Map the virtual directory to the physical file path (this should be done as apart of above step)

  • Open the SharePoint Central Administration pages by clicking Start > Administrative Tools > SharePoint Central Administration

    Central Administrator Page

    Figure 3: Central Administrator Page

  • Click the "Windows SharePoint Services" link on the left hand side

  • Select "Configure Virtual server settings" link from "Virtual Server Configuration" group

  • Select your Portal server from the list

    Virtual Server Settings Page

    Figure 4: Virtual Server Settings Page

  • Click the "Defined managed paths" link under "Virtual Server Management" group in Virtual Server Settings page

  • Type the name of the virtual directory you created in the first step in the "Path" box of the "Add a New Path" section

    Define Managed Paths Page

    Figure 5: Define Managed Paths Page

  • Select the "Excluded path" radio button under the "Type" section

  • Click the "OK" button

  • Then navigate to http://portal_site_name/SPSAdminWeb/SPSAdminPage.aspx

Conclusion

SharePoint Administrators should be able to use this article as a starting point and develop their SharePoint Administration web application according to their requirements.

About the Author

Gayan Peiris is a Technical Architect for Unique World Pty Ltd
(www.uniqueworld.net) in Canberra, Australia. He is a MCSD with MCAD, MCP, Bcom and MIT certifications.
Gayan has designed and developed Microsoft Web and Windows solutions since 1999. His expertise lies in
developing scalable, high-performance applications with Microsoft Enterprise Server Products in .NET
technology. His core skills are ADO.NET, ASP.NET, C#, VB.NET, Web Services, XML, MS SharePoint Portals,
MS Content Management Server, MS Project Server, Live Communication Server, DNA and SQL Server.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read