Retrieving SharePoint Site Information in an ASP.NET Web Application

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.

NameDescription
AllowAnonymousAccessIs anonymous access is allowed for the site.
AllowUnsafeUpdatesWhether to allow updates to the database as a result of a GET request.
AllUsersGets 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.
AlternateHeaderThe URL for an alternate .aspx page to use for rendering the top navigation area in SharePoint pages.
AnonymousStateThe level of access for anonymous users on the site.
AuthenticationModeValue indicating that Windows authentication is the default authentication mode.
AuthorUser object representing the user who created the Web site.
DescriptionThe description for the site.
EmailInsertsEnabledIndicates whether document libraries on the virtual server can accept e-mail attachments from public folders.
EventHandlersEnabledIndicates whether event handlers can be used in the site.
HasUniquePermValue that specifies whether the site has unique permissions.
IDThe GUID for the site.
IsADAccountCreationModeValue that indicates whether user accounts are automatically created in Active Directory service when users are invited to the Web site.
IsADEmailEnabledValue that indicates whether Active Directory e-mail is enabled on the site.
IsRootWebValue that indicates whether the site is the top-level Web site of the site collection.
LanguageThe LCID for the language used on the site.
NameThe name of the Web site.
ParentWebThe parent Web site for the site.
PortalMemberValue that indicates whether the Web site is a member of a portal.
PortalNameThe name of a portal used with the site.
PortalUrlThe URL to a portal used with the site.
ThemeThe name of the theme that is applied to the Web site.
ThemeCssUrlThe URL for the cascading style sheets file used in the theme for the Web site.
TitleTitle for the Web site.
UrlThe 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.



Downloads

Comments

  • Dead pics

    Posted by Ash on 03/15/2013 03:37am

    Any chance you could get those images back and maybe providing more detail on implementing the custom ASP.NET application? I've been at this for days and I keep getting Server Error in '/' Application Runtime Errors when I try to access http://sharepoint/SPSAdminWeb/SPSAdminWeb.aspx. I've done everything your post suggested, but the only difference is that I'm using Sharepoint 2010.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Agile methodologies give development and test teams the ability to build software at a faster rate than ever before. Combining DevOps with hybrid cloud architectures give teams not just the principles, but also the technology necessary to achieve their goals. By combining hybrid cloud and DevOps: IT departments maintain control, visibility, and security Dev/test teams remain agile and collaborative Organizational barriers are broken down Innovation and automation can thrive Download this white paper to …

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds