Поделиться через


How to: Implement the Object Model in a Custom Web Part

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.

You can create custom Web Parts to work with site or list data. This programming task shows how to create a simple Web Part that displays the titles and number of items for all lists that contain more than 10 items on subsites in the current Web site.

To create a Web Part that displays titles and number of items for lists

  1. Create a Web Part as described in Walkthrough: Creating a Basic SharePoint Web Part. This example assumes that you have created a SimpleWebPart application.

  2. Open WebCustomControl1.cs or WebCustomControl1.vb for the SimpleWebPart application, and add directives for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities, as follows:

    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.Utilities
    
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    
  3. Remove the HtmlControl objects used in the example, including declarations for their variables, the _mybutton_click handler, and the CreateChildControls method.

  4. Replace the contents of the RenderWebPart method with the following code block.

    Dim mySite As SPWeb = SPContext.Current.Web
    
    output.Write(SPEncode.HtmlEncode(mySite.Title))
    
    Dim subSites As SPWebCollection = mySite.Webs
    Dim site As SPWeb
    
    For Each site In subSites
        output.Write(SPEncode.HtmlEncode(site.Title) & "<BR>")
        Dim lists As SPListCollection = site.Lists
        Dim list As SPList
    
        For Each list In lists
    
            If list.ItemCount > 10 Then
                output.Write(SPEncode.HtmlEncode(list.Title) & " :: " 
                    & list.ItemCount & "<BR>")
            End If
        Next list
    Next site
    
    SPWeb mySite = SPContext.Current.Web;
    
    output.Write(SPEncode.HtmlEncode(mySite.Title));
    
    SPWebCollection subSites = mySite.Webs;
    
    foreach(SPWeb site in subSites)
    {
    
       output.Write(SPEncode.HtmlEncode(site.Title) + "<BR>");
    
       SPListCollection lists=site.Lists;
    
       foreach(SPList list in lists)
       {
    
          if (list.ItemCount>10) 
          {
              output.Write(SPEncode.HtmlEncode(list.Title) + " : " + 
                 list.ItemCount + "<BR>");
          }
       }
    }
    

    The example first writes out the title of the current Web site. It then iterates through all the subsites to print their titles, and through all the lists in each subsite to print the list title and number of items for cases in which more than ten list items are in a list.

  5. On the Build menu, click Build Solution.

  6. Increase the trust level in Windows SharePoint Services from minimal (default) to medium by opening the web.config file at \\Inetpub\wwwroot\wss\VirtualDirectories\80 and replacing the following line:

    <trust level="WSS_Minimal" originUrl="" />
    

    with the following:

    <trust level="WSS_Medium" originUrl="" />
    
  7. Reset Microsoft Internet Information Services (IIS) to make the changes in trust level take effect.

The Web Part can be imported through the user interface into a Web Part Page or into the home page for viewing the list data.

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