We can create custom webparts to work with site and list data. This programming task will show you how to create a simple Web part to search for a list of more than 10 pieces of data under the current website, and display its title and the number of list items.
Procedure
1. Create a webpart. Assume that you have created an application named simplewebpart.Program.
2. Open webcustomcontrol1.cs or. VB in the simplewebpart application and add the following namespace reference.
VB
Imports Microsoft. SharePoint
Imports Microsoft. Sharepoint. Utilities
C #
Using Microsoft. SharePoint;
Using Microsoft. Sharepoint. utilities;
3. Original removal in the exampleHtmlcontrolObject, and the corresponding variable declaration, _ mybutton_click event,CreatechildcontrolsMethod.
4. Use the followingCodeReplace the content in the renderwebpart method:
VB
Dim Mysite As Spweb = Spcontext. Current. Web
Output. Write (response code. htmlencode (mysite. Title ))
DimSubsitesAsSpwebcollection=Mysite. Webs
DimSiteAsSpweb
For Each Site In Subsites
Output. Write (response code. htmlencode (site. Title) & " <Br> " )
Dim Lists As Splistcollection = Site. Lists
Dim List As Splist
For EachListInLists
If List. itemcount > 10 Then
Output. Write (response code. htmlencode (list. Title) & " :: "
& List. itemcount & " <Br> " )
End If
Next List
Next Site
C #
Spweb mysite = Spcontext. Current. Web;
Output. Write (response code. htmlencode (mysite. Title ));
Spwebcollection subsites=Mysite. webs;
Foreach(Spweb siteInSubsites)
{
Output. Write (response code. htmlencode (site. Title)+ "<Br>");
Splistcollection lists=Site. lists;
Foreach(Splist listInLists)
{
If (List. itemcount > 10 )
{
Output. Write (response code. htmlencode (list. Title) + " : " +
List. itemcount + " <Br> " );
}
}
}
The sample code above first outputs the title of the current website, then traverses all the sub-websites and prints their titles, and then traverses all the lists under the website to find a list with more than 10 list items, print the title and number of items.
5. Click build solution in the build menu to compile the webpart.
6. Improve the WSS trust level in the web. config file.
The Web. config file is usually located under \ inetput \ wwwroot \ WSS \ virtualdirectories \ 80. Find the following line:
< Trust Level = "Wss_minimal" Originurl = "" />
Replace it:
< Trust Level = "Wss_medium" Originurl = "" />
7. Restart IIS to make changes to the trust level take effect.
Drag the webpart on any Web Part Page or home page to view the displayed list.