Wss3sdk: how to create a web application in a Sharepoint site

Source: Internet
Author: User
This programming task describes how to create a custom web application running under Windows SharePoint Services in Visual Studio 2005. Program Or website. This example creates a tool that lists all the SharePoint sites that a specified user can access and the group information of the user in each website in the form of a report.

Note:
"Web Applications" have different meanings based on different contexts. This section describes a browser-based ASPX page. It has nothing to do with Internet Information Server (IIS) "website" or "application pool. Therefore, it does not representSpwebapplicationClass.

For how to create a web application that coexist with Windows SharePoint Services, see section "how to modify the configuration so that the application can coexist with Windows SharePoint Services ".

Procedure

Create a tool for reporting users' SharePoint websites and Groups

1. in Visual Studio 2005, create a new Web application and add a reference to Microsoft. Sharepoint. dll.
2. In the design view, drag a label, a text box, and a button from the toolbox to the default. aspx page. You can clickViewIn the menuToolboxTo open the toolbox.

Note:
If you want to create a web application to modify the content database, you need to go to the ASPX pageSource codeAdd a formdigest control in the form tag.

3. Double-click the added button to open the backgroundCodeFile. Here is default. aspx. CS.
4. Reference The following namespace at the beginning of the file:
VBImportsMicrosoft. SharePoint
ImportsMicrosoft. Sharepoint. Utilities

C #UsingMicrosoft. SharePoint;
UsingMicrosoft. Sharepoint. utilities;

By referencingMicrosoft. SharePointThe namespace that can return the specified user and the group to which the user belongs. By referencingMicrosoft. Sharepoint. UtilitiesNamespace, which can display string encoding.

5. Then, we instantiate the site object through spsite mysite = spcontext. Current. Site. (DIM mysite as spsite = spcontrol. Context. Current. Site in Visual Basic)
Now, we can use the allwebs attribute of spsite, The allusers attribute of spweb objects, and the groups attribute of spuser in the foreach loop to return the user object corresponding to the user name in the text box, and its group information.
In the default. aspx. CS default class, add the following method to return the users and groups of all websites in the current website set:

VB

Protected   Sub Getsitesandgroups ()
Dim Userlist As   String   = Response Code. htmlencode (textbox1.text) +  
" Is a user in the following webs: <br> "

Dim Mysite As Spsite = Spcontext. Current. Site
Dim Allwebs As Spwebcollection = Mysite. allwebs

Dim Subweb As Spweb
For   Each Subweb In Allwebs
Dim Listgroups As   String   =   ""

' Use allusers not users to ensure you find the user
Dim Allusers As Spusercollection = Subweb. allusers
Dim User As Spuser
For   Each User In Allusers
If User. loginname. toupper () = Textbox1.text. toupper ()
Then
Dim Allgroups As Spgroupcollection = User. Groups
Dim Group As Spgroup
For   Each Group In Allgroups
Listgroups + = Response Code. htmlencode (group. Name) +  
" "
Next Group
Userlist + = Subweb. serverrelativeurl. tostring () +  
" -- "   + Listgroups +   " <Br> "
End   If
Next User
Next Subweb
Label1.text = Userlist
End sub   ' Getsitesandgroups

C #

Protected   Void Getsitesandgroups ()
{
String Userlist = Response Code. htmlencode (textbox1.text) +  
" Is a user in the following webs: <br> " ;

Spsite mysite=Spcontext. Current. Site;
Spwebcollection allwebs=Mysite. allwebs;

Foreach(Spweb subwebInAllwebs)
{
StringListgroups= "";

/* Use allusers not users to ensure you find the user */
Spusercollection allusers = Subweb. allusers;
Foreach (Spuser user In Allusers)
{
If (User. loginname. toupper () = Textbox1.text. toupper ())
{
Spgroupcollection allgroups = User. Groups;
Foreach (Spgroup Group In Allgroups)
{
Listgroups + = Response Code. htmlencode (group. Name) +   " " ;
}
Userlist + = Subweb. serverrelativeurl. tostring () +  
" -- "   + Listgroups +   " <Br> " ;
}
}
}
Label1.text = Userlist;
} The above sample code traverses all users on all websites in the current website to find the items that match the loginname of the specified user and the username entered in the text box, collect and display the group information of the user under the website.
6. To avoid receiving Access Denied errors when running the above Code, the user running the getsitesandgroups method must have full control permissions. You can useRunwithelevatedprivileges To ensure that the current user can access the user and group information of all websites in the website set.
VB

Protected   Sub Button#click (sender As   Object , E As Eventargs)
Dim Elevatedgetsitesandgroups As   New Spsecurity. codetorunelevated (getsitesandgroups)
Spsecurity. runwithelevatedprivileges (elevatedgetsitesandgroups)
End sub   ' Button#click

C #

Protected   Void Button#click ( Object Sender, eventargs E)
{
Spsecurity. codetorunelevated elevatedgetsitesandgroups =   New Spsecurity. codetorunelevated (getsitesandgroups );
Spsecurity. runwithelevatedprivileges (elevatedgetsitesandgroups );
} 7. Build Click Generate solution.

Open the web page in the browser. After you enter the logon name of a user in the website set, the tag displays the website and Group of the specified user.

8. To access the page we just created outside Visual Studio 2005, you can use the following URL http: // SERVER_NAME/[sites/] [site_name/] _ layouts/web_application_name/default. aspx.

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.