Share via


Implementing the Windows SharePoint Services 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 ten items on subsites in the current Web site.

  • Create a Web Part as described in Creating a Basic Web Part. This example assumes that you have created a SimpleWebPart application.

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

    [Visual Basic .NET]
    
    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.Utilities
    Imports Microsoft.SharePoint.WebControls
    
    [C#]
    
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using Microsoft.SharePoint.WebControls;
    
  • Remove the HtmlControl objects used in the example, including declarations for their variables, the _mybutton_click handler, and the CreateChildControls method.

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

    [Visual Basic .NET]
    
    Dim mySite As SPWeb = SPControl.GetContextWeb(Context)
    
    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 = SPControl.GetContextWeb(Context);
    
    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 out their titles, and then through all the lists in each subsite to print out the list title and number of items for cases where there are more than ten list items in a list.

  • On the Build menu, click Build Solution.

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

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

    with the following:

    <trust level="WSS_Medium" originUrl="" />
    
  • Reset Microsoft Internet Information Services (IIS) for changes in trust level to 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.