Partager via


Creating a Web Application in a SharePoint Web Site

This programming task describes how to create a Web application in Visual Studio .NET that operates within the context of Windows SharePoint Services. The example creates a tool for reporting a list of all the SharePoint Web sites to which a specified user belongs, as well as a list of that user's membership in the security groups for each Web site.

Note  For information about how to create a Web application that coexists with Windows SharePoint Services, see Modifying Configuration Settings for an Application to Coexist with Windows SharePoint Services.

  • In Visual Studio .NET, create a new Web application and set a reference to Microsoft.SharePoint.dll. For information on how to perform these steps, see Getting Started with Customizing a Team Web Site in Visual Studio .NET.

  • In Design View, use the Toolbox to add a label, a text box, and a button to WebForm1.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 containing the form. For information on how to add this control, see Security Validation and Making Posts to Update Data.

  • Double-click the button to open the code-behind file named WebForm1.aspx.cs.

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

    [Visual Basic .NET]
    
    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.WebControls
    Imports Microsoft.SharePoint.Utilities
    
    [C#]
    
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.Utilities;
    

    The Microsoft.SharePoint.WebControls namespace must be referenced in order to instantiate a site object that corresponds to the current site context. The Microsoft.SharePoint namespace must be referenced in order to return the specified user and the groups to which the user belongs. The Microsoft.SharePoint.Utilities namespace must be referenced in order to encode strings that are displayed in the example.

    After you instantiate the site object using SPSite mySite = SPControl.GetContextSite(Context) (in Visual Basic .NET, use Dim mySite As SPSite = SPControl.GetContextSite(Context)), you can use the AllWebs property of the SPSite class, the Users property of the SPWeb class, and the Roles 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 site groups to which that user belongs.

    Note  The Context value that is passed in the GetContextSite method is inherited from System.Web.UI.Page.

  • Within the Button1_Click handler that Visual Studio .NET creates on the code-behind page, add the following code block:

    [Visual Basic .NET]
    
    Dim userList As String = SPEncode.HtmlEncode(TextBox1.Text) & " is a user in the following webs:<BR>"
    Dim mySite As SPSite = SPControl.GetContextSite(Context)
    Dim allSites As SPWebCollection = mySite.AllWebs
    Dim subSite As SPWeb
    
    For Each subSite In  allSites
    
        Dim listGroups As String = ""
        Dim allUsers As SPUserCollection = subSite.Users
        Dim user As SPUser
    
        For Each user In  allUsers
    
            If user.LoginName = TextBox1.Text Then
    
                Dim allGroups As SPRoleCollection = user.Roles
                Dim group As SPRole
    
                For Each group In  allGroups
    
                    listGroups += SPEncode.HtmlEncode(group.Name) & "  "
    
                Next group
    
                userList += subSite.ServerRelativeUrl.ToString() & " :: " & listGroups & "<BR>"
            End If
    
        Next user
    
    Next subSite
    
    Label1.Text = userList 
    
    [C#]
    
    string userList = SPEncode.HtmlEncode(TextBox1.Text) + " is a user in the following webs:<BR>";
    
    SPSite mySite = SPControl.GetContextSite(Context);
    SPWebCollection allSites = mySite.AllWebs;
    
    foreach (SPWeb subSite in allSites)
    {
        string listGroups = "";
    
        SPUserCollection allUsers = subSite.Users;
    
        foreach (SPUser user in allUsers)
        {
    
            if (user.LoginName == TextBox1.Text)
            {
                SPRoleCollection allGroups = user.Roles;
    
                foreach (SPRole group in allGroups)
                {
                    listGroups += SPEncode.HtmlEncode(group.Name) + "  ";
                }
    
                userList += subSite.ServerRelativeUrl.ToString() + " -- " + listGroups + "<BR>";
            }
        }
    }
    
    Label1.Text =  userList;
    

    The code example iterates through all the users of all the subsites for the current top-level site, 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 site groups for the site are collected and displayed.

  • On the Build menu, click Build Solution.

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

  • To run the application after creating it in Visual Studio .NET, navigate to http://Server_Name/[sites/][Site_Name/]_layouts/Web_Application_Name/webform1.aspx.