Wss3sdk: using object models in custom webparts

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.