asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search










Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Retrieving SharePoint Site Information in an ASP.NET Web Application
By Gayan Peiris
Rating: 3.9 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

     

    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


    Figure 1: The Properties of the selected site 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


      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


      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


      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.

    You can download a zip file containing the code from this article from here.

    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.

    www.gayanpeiris.com

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 28, 2005 - N-Tier Web Applications using ASP.NET 2.0 and SQL Server 2005 - Part 2
    In the second part of his series on building N-tier web applications using ASP.NET 2.0 and SQL Server 2005, Thiru Thangarathinam covers the business logic and user interface layers. In the process, he also examines some new features in ASP.NET 2.0 that greatly simplify the development process.
    [Read This Article]  [Top]
    Jul 14, 2005 - An Innovative Technique for Creating Reusuable Page Templates in ASP.NET 1.x
    Code reusuability is one of the major goals of any good object-oriented programmer. While the ASP.NET framework has made code reusuability easier and more elegant than it was in classic ASP, one area where reusuability could be improved is at the UI level. This article outlines a technique that you can use in ASP.NET 1.x that allows every page in your web application to inherit not only the functionality of a base page, but its UI as well.
    [Read This Article]  [Top]
    Jun 23, 2005 - Monitoring SharePoint Usage through an ASP.NET Web Application
    In this article, Gayan Peiris looks at creating an ASP.NET web application that will display the usage details of a selected SharePoint site. Building such an application enables SharePoint administrators to gather all SharePoint usage data from a central location.
    [Read This Article]  [Top]
    Dec 23, 2004 - Automated Deployment for Side By Side .NET Web Apps for Visual Studio .NET 2003
    In this article, David Every outlines a step-by-step account of how he solved the problems he encountered while implementing an auto-deployment process. He also describes how to create a stable process for automated remote .NET deployment featuring "side-by-side" capability.
    [Read This Article]  [Top]
    Sep 29, 2004 - Developing Web Parts with ICellConsumer Interface
    Most default SharePoint Server Web Parts can be connected across organizations. The third article in this series shows how to develop connectable Web Parts that consume information provided by other Web Parts.
    [Read This Article]  [Top]
    Aug 10, 2004 - Implementing and Promoting Daily Builds
    Automatic daily builds is a well known software engineering best practice. This article introduces a strategy for implementing and promoting daily builds and offers tips and tricks for preventing and fixing breaks.
    [Read This Article]  [Top]
    Jun 21, 2004 - Using Open Source .NET Tools for Sophisticated Builds
    Building an application can be more than pressing F5. With an increasing number of quality packages being released, developers for the .NET platform now have options to create a very sophisticated build process. Aaron Junod describes a sample build environment and shows how a number of tools can work together to make reliable, predictable, and value-added builds.
    [Read This Article]  [Top]
    Jun 24, 2003 - Programming for the Palm Part 1 - Creating the Palm Application
    The first part of this three part series walks through the process of creating a mobile blog application using a BASIC development environment for Palm OS devices called NS Basic. Subsequent articles will focus on synchronizing the data to the desktop using C# and creating an installer.
    [Read This Article]  [Top]
    Jun 18, 2003 - Online Database Functions Testing Tool
    This short article provides source code for a classic ASP online database functions testing application and shows how to configure and use the tool for either SQL Server or Oracle.
    [Read This Article]  [Top]
    May 22, 2003 - Rewrite.NET -- A URL Rewriting Engine for .NET
    In this article Robert Chartier shows you how to use functionality in the .NET Framework to rewrite requested URLs on the fly.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers