SharePoint Object Model
SharePoint object model consists of a group of object that will help programmer to add programming logic to a SharePoint site. The base namespace for this is Microsoft.SharePoint.
The classes that will be typically used are SPWebApplication, SPSite, SPWeb, SPList, SPListCollection and so on.
How to create a web application in Sharepoint 2007?
Open Visual Studio 2005/2008/2010, create a new Web application and set a reference to Microsoft.SharePoint.dll. If you are using Visual Studio 2010 then change the target framework to 3.5.
In Design view, use the Toolbox to add a label, a text box, and a button to Default.aspx. To open the Toolbox, click Toolbox on the View menu.
To open the code-behind file named Default.aspx.cs, double-click the button you added in the previous step.
At the beginning of the file, add using directives (Imports statements in Visual Basic) that reference the Microsoft.SharePoint and Microsoft.SharePoint.Utilities, as follows:
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
At the beginning of the class definition return the site collection object for the current request context as follows:
public partial class _Default : System.Web.UI.Page
{
SPSite oSite = SPContext.Current.Site;
{
SPSite oSite = SPContext.Current.Site;
}
Create a method that uses the AllWebs property of the SPSite class, the AllUsers property of the SPWeb class, and the Groups property of the SPUser class within nested foreach statements, in order to return a user object for the user name specified in the text box, as well as the list of groups to which that user belongs.
Within the default class in Default.aspx.cs, add the following method to return the users and groups for each Web site in the current site collection:
protected void GetSitesAndGroups()
{
string strUser = SPEncode.HtmlEncode(TextBox1.Text) +
" is a user in the following groups:<BR>";
SPWebCollection collWebs = oSite.AllWebs;
foreach (SPWeb oWebsite in collWebs)
{
string strGroups = "";
/*Use AllUsers not Users to ensure you find the user*/
SPUserCollection collUsers = oWebsite.AllUsers;
foreach (SPUser oUser in collUsers)
{
if (oUser.LoginName.ToUpper() == TextBox1.Text.ToUpper())
{
SPGroupCollection collGroups = oUser.Groups;
foreach (SPGroup oGroup in collGroups)
{
strGroups += SPEncode.HtmlEncode(oGroup.Name) +
" ";
}
strUser += oWebsite.ServerRelativeUrl.ToString() +
" -- " + strGroups + "<BR>";
}
}
oWebsite.Dispose();
}
Label1.Text = strUser;
}
{
string strUser = SPEncode.HtmlEncode(TextBox1.Text) +
" is a user in the following groups:<BR>";
SPWebCollection collWebs = oSite.AllWebs;
foreach (SPWeb oWebsite in collWebs)
{
string strGroups = "";
/*Use AllUsers not Users to ensure you find the user*/
SPUserCollection collUsers = oWebsite.AllUsers;
foreach (SPUser oUser in collUsers)
{
if (oUser.LoginName.ToUpper() == TextBox1.Text.ToUpper())
{
SPGroupCollection collGroups = oUser.Groups;
foreach (SPGroup oGroup in collGroups)
{
strGroups += SPEncode.HtmlEncode(oGroup.Name) +
" ";
}
strUser += oWebsite.ServerRelativeUrl.ToString() +
" -- " + strGroups + "<BR>";
}
}
oWebsite.Dispose();
}
Label1.Text = strUser;
}
protected void Button1_Click(object sender, EventArgs e)
{
SPSecurity.CodeToRunElevated elevatedGetSitesAndGroups = new SPSecurity.CodeToRunElevated(GetSitesAndGroups);
SPSecurity.RunWithElevatedPrivileges(elevatedGetSitesAndGroups);
}
On the Debug menu, click Start Debugging, or press
F5. If a Debugging Not Enabled dialog box appears, make sure
Add a new Web.config file with debugging enabled is selected, and click
OK. The browser opens the page. When you type the logon name of a user on a site within the current site collection, the label displays the Web sites and groups to which the specified user belongs.
To run the application after creating it in Visual Studio 2005/2008/2010, navigate to
http://Server_Name/_layouts/Web_Application_Name/Default.aspx.
No comments:
Post a Comment