Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Server Technologies
SDK Documentation
General Reference
 How to: Implement the Object Model ...
How to: Implement the Object Model in a Custom Web Part

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:

    Visual Basic
    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.Utilities
    

    C#
    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.

    Visual Basic
    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
    

    C#
    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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content      
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker