Exercise 9-4: Building an Identity Web Part


Exercise 9-4: Building an Identity Web Part

Another issue that comes up early in a SharePoint Services deployment is the management of display names and e-mail addresses associated with individual sites. Because all top-level sites are independent, it is easy for the site creator to misspell a name or address. Furthermore, end users have no simple way to correct their information. In this exercise, you will create a simple web part that will allow users to change their personal information directly in a site. Figure 9-3 shows a view of the final project.


Figure 9-3: Displaying personal information

Creating the Project

This web part project will be written in C#. Open Visual Studio and create a new web part project in C# named SPSIdentity . When the project is created, rename the class file and the web part description file as SPSIdentity.dwp and SPSIdentity.cs respectively. Then, open SPSIdentity.dwp and change the file to appear as shown in Listing 9-29.

Listing 9-29: The Web Part Description File
start example
 <?xml version="1.0" encoding="utf-8"?> <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >     <Title>Your Information</Title>     <Description>A web part that shows identity information</Description>     <Assembly>SPSIdentity</Assembly>     <TypeName>SPSIdentity.Reporter</TypeName> </WebPart> 
end example
 

Before you begin to modify the web part code, you must add a reference to the SharePoint Services namespace. Once the reference is added, open the SPSIdentity.cs file for editing. You will add several using statements to the file, modify the class name, and remove the default property. Change your web part to appear as shown in Listing 9-30.

Listing 9-30: Starting the Project
start example
 using System; using System.ComponentModel; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml.Serialization; using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint.WebPartPages; using Microsoft.SharePoint.WebControls; namespace SPSIdentity {     [DefaultProperty(""),     ToolboxData("<{0}:Report runat=server></{0}:Report>"),     XmlRoot(Namespace="SPSIdentity")]     public class Reporter : Microsoft.SharePoint.WebPartPages.WebPart     { 
end example
 

Creating the Child Controls

This web part has no properties. It simply displays the user's information and allows the user to make changes. The user interface for this web part is more involved than others you have created, but it still follows the same general development principles. Add the code from Listing 9-31 to the project to create the user interface for the web part.

Listing 9-31: Creating the Child Controls
start example
 protected Label userNameLabel; protected TextBox displayNameText; protected TextBox emailText; protected Button updateButton; protected Label messageLabel; protected override void CreateChildControls() {     //UserName Label     userNameLabel = new Label();     userNameLabel.Width = Unit.Percentage(100);     userNameLabel.Font.Size = FontUnit.Point(10);     userNameLabel.Font.Name = "arial";     Controls.Add(userNameLabel);     //DisplayName Text     displayNameText = new TextBox();     displayNameText.Width = Unit.Percentage(100);     displayNameText.Font.Name = "arial";     displayNameText.Font.Size = FontUnit.Point(10);     Controls.Add(displayNameText);     //E-Mail Text     emailText = new TextBox();     emailText.Width = Unit.Percentage(100);     emailText.Font.Name = "arial";     emailText.Font.Size = FontUnit.Point(10);     Controls.Add(emailText);     //Submit Button     updateButton = new Button();     updateButton.Font.Name = "arial";     updateButton.Font.Size = FontUnit.Point(10);     updateButton.Text = "Change";     Controls.Add(updateButton);     updateButton.Click +=new EventHandler(update_Click);     //Message Label     messageLabel = new Label();     messageLabel.Width = Unit.Percentage(100);     messageLabel.Font.Size = FontUnit.Point(10);     messageLabel.Font.Name = "arial";     Controls.Add(messageLabel); } 
end example
 

Rendering the Web Part

When the web part runs, it displays the logon name, display name, and e-mail address for the current user. The display name and e-mail address are presented in text boxes so that they can be edited directly. Add the code from Listing 9-32 to display the current user information.

Listing 9-32: Displaying Current User Information
start example
 protected override void RenderWebPart(HtmlTextWriter output) {     //Get current user information before the context is changed     SPSite site = SPControl.GetContextSite(Context);     SPWeb web = site.OpenWeb();     SPUser user = web.CurrentUser;     //Show user information     userNameLabel.Text = user.LoginName;     displayNameText.Text = user.Name;     emailText.Text = user.Email;     //Create output     output.Write("<TABLE Border=0>");     output.Write("<TR>");     output.Write("<TD>User name: ");     userNameLabel.RenderControl(output);     output.Write("</TD>");     output.Write("</TR>");     output.Write("<TR>");     output.Write("<TD> Display name: ");     displayNameText.RenderControl(output);     output.Write("</TD>");     output.Write("</TR>");     output.Write("<TR>");     output.Write("<TD>e-Mail: ");     emailText.RenderControl(output);     output.Write("</TD>");     output.Write("</TR>");     output.Write("<TR>");     output.Write("<TD>");     updateButton.RenderControl(output);     output.Write("</TD>");     output.Write("</TR>");     output.Write("<TR>");     output.Write("<TD>");     messageLabel.RenderControl(output);     output.Write("</TD>");     output.Write("</TR>");     output.Write("</TABLE>");     //close     web.Close();     site.Close(); } 
end example
 

Updating the User Information

The user interface for the web part displays a button that can be clicked to edit the user information. End users simply type the changes directly into the web part and click the button. The changes are then written back to SharePoint Services. Add the code from Listing 9-33 to update the credentials.

Listing 9-33: Updating the User Information
start example
 private void update_Click(object sender, EventArgs e) {     //Get current user information before the context is changed     SPSite site = SPControl.GetContextSite(Context);     SPWeb web = site.OpenWeb();     SPUser user = web.CurrentUser;     //Update current user information     user.Email=emailText.Text;     user.Name=displayNameText.Text;     user.Update();     web.Close();     site.Close(); } 
end example
 

Using the Web Part

Using the web part is no different than using any other web part. Give the web part a strong name and compile it. Add the web part to the <SafeControls> section of the web.config file and import it into a page. The web part should then display the information for the current user. The best way to use this part is to place it on a top-level site to ensure the credentials are correct for the entire site collection.




Microsoft SharePoint[c] Building Office 2003 Solutions
Microsoft SharePoint[c] Building Office 2003 Solutions
ISBN: 1590593383
EAN: N/A
Year: 2006
Pages: 92

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net