Share via


How to: Create a Web Application in a SharePoint Web Site

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

This programming task describes how to create a custom Web application, or Web Site, in Visual Studio 2005 that operates within the context of Windows SharePoint Services. The example creates a tool that reports a list of all the SharePoint Web sites to which a specified user belongs, as well as a list of the groups to which the user belongs for each Web site.

Note

The phrase "Web application" has varying meanings depending on the context. This topic concerns browser-based applications that run from an aspx page. It is not about Web applications in the sense of Internet Information Services (IIS) "Web sites" or "Application Pools". Hence, it is not about the things represented by the SPWebApplication class in the Windows SharePoint Services 3.0 object model.

Procedure

To create a tool for reporting a user's SharePoint Web sites and groups

  1. In Visual Studio 2005, create a new Web application and set a reference to Microsoft.SharePoint.dll. For information about how to perform these steps, or about how to create a Web application on a front-end Web server from a remote computer that is not running Windows SharePoint Services, see Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio.

  2. In Design view, use the Toolbox to add a label, a text box, and a button to Default.aspx. To open the Toolbox, click Toolbox on the View menu.

    Note

    To create a Web application that modifies data in the content database, you need to add a FormDigest control and a page directive to the .aspx file that contains the form. For information about how to add this control, see Security Validation and Making Posts to Update Data.

  3. To open the code-behind file named Default.aspx.cs, double-click the button you added in the previous step.

  4. At the beginning of the file, add using directives (Imports statements in Visual Basic) that reference the Microsoft.SharePoint and Microsoft.SharePoint.Utilities, as follows:

    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.Utilities
    
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    

    You must reference the Microsoft.SharePoint namespace in order to return the specified user and the groups to which the user belongs. You must reference the Microsoft.SharePoint.Utilities namespace in order to encode strings that are displayed in the example.

  5. At the beginning of the class definition return the site collection object for the current request context as follows:

    Partial Class _Default
       Inherits System.Web.UI.Page
    
       Dim oSite As SPSite = SPContext.Current.Site
       ...
    
    public partial class _Default : System.Web.UI.Page
    {
       SPSite oSite = SPContext.Current.Site;
       ...
    
  6. Create a method that uses the AllWebs property of the SPSite class, the AllUsers property of the SPWeb class, and the Groups property of the SPUser class within nested foreach statements, in order to return a user object for the user name specified in the text box, as well as the list of groups to which that user belongs.

    Within the default class in Default.aspx.cs, add the following method to return the users and groups for each Web site in the current site collection:

    Protected Sub GetSitesAndGroups()
       Dim strUser As String = SPEncode.HtmlEncode(TextBox1.Text) _
          & " is a user in the following groups:<BR>"
    
       Dim collWebs As SPWebCollection = oSite.AllWebs
    
       For Each oWebsite As SPWeb In collWebs
          Dim strGroups As String = ""
    
          'Use AllUsers not Users to ensure you find the user
          Dim collUsers As SPUserCollection = oWebsite.AllUsers
    
          For Each oUser As SPUser In collUsers
             If oUser.LoginName.ToUpper() = TextBox1.Text.ToUpper() Then
                Dim collGroups As SPGroupCollection = oUser.Groups
    
                For Each oGroup As SPGroup In collGroups
                   strGroups += SPEncode.HtmlEncode(oGroup.Name) & "  "
                Next oGroup
    
                strUser = strUser _
                   & oWebsite.ServerRelativeUrl.ToString() _
                   & " -- " & strGroups & "<BR>"
             End If
          Next oUser
    
          oWebsite.Dispose()
       Next oWebsite
    
       Label1.Text = strUser
    End Sub
    
    protected void GetSitesAndGroups()
    {
       string strUser = SPEncode.HtmlEncode(TextBox1.Text) +
          " is a user in the following groups:<BR>";
    
       SPWebCollection collWebs = oSite.AllWebs;
    
       foreach (SPWeb oWebsite in collWebs)
       {
          string strGroups = "";
    
          /*Use AllUsers not Users to ensure you find the user*/
          SPUserCollection collUsers = oWebsite.AllUsers;
          foreach (SPUser oUser in collUsers)
          {
             if (oUser.LoginName.ToUpper() == TextBox1.Text.ToUpper())
             {
                SPGroupCollection collGroups = oUser.Groups;
    
                foreach (SPGroup oGroup in collGroups)
                {
                   strGroups += SPEncode.HtmlEncode(oGroup.Name) + 
                      "  ";
                }
    
                strUser += oWebsite.ServerRelativeUrl.ToString() +
                   " -- " + strGroups + "<BR>";
             }
          }
    
          oWebsite.Dispose();
       }
    
          Label1.Text = strUser;
    }
    

    The code example iterates through all the users of all the Web sites within the current site collection, and if the value of the LoginName property for a given user matches the user name that is entered in the text box, then the names of all the user's groups for the site are collected and displayed.

  7. To run the code added in the previous step and avoid receiving an access denied error message, the user implementing the GetSitesAndGroups method must have Full Control permission. Add the following code to the Button1_Click event handler, which uses the RunWithElevatedPrivileges method to allow the current user to iterate through the users and groups of each Web site within the current site collection.

    Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim elevatedGetSitesAndGroups As New SPSecurity.CodeToRunElevated(AddressOf GetSitesAndGroups)
       SPSecurity.RunWithElevatedPrivileges(elevatedGetSitesAndGroups)
    End Sub
    
    protected void Button1_Click(object sender, EventArgs e)
    {
       SPSecurity.CodeToRunElevated elevatedGetSitesAndGroups = new SPSecurity.CodeToRunElevated(GetSitesAndGroups);
       SPSecurity.RunWithElevatedPrivileges(elevatedGetSitesAndGroups);
    }
    
  8. On the Debug menu, click Start Debugging, or press F5. If a Debugging Not Enabled dialog box appears, make sure Add a new Web.config file with debugging enabled is selected, and click OK.

    The browser opens the page. When you type the logon name of a user on a site within the current site collection, the label displays the Web sites and groups to which the specified user belongs.

  9. To run the application after creating it in Visual Studio 2005, navigate to http://Server_Name/_layouts/Web_Application_Name/Default.aspx.

See Also

Concepts

Working with List Objects and Collections

Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio

Security Validation and Making Posts to Update Data

Elevation of Privilege