Posted by: R Manimaran | February 27, 2011

SharePoint WebPart Custom Properties: Password Field as Property

SharePoint WebPart Custom Properties: Password Field as Property

Creating custom properties for the web part is just easy. Declare the properties as we did in C# and decorate those properties with attributes will make them available in Toolbox of the web part.


[WebBrowsable(true),
Personalizable(true),
Category("Custom WebPart"),
DisplayName("Property."),
WebDisplayName("Property"),
WebDescription("Custom Property")]

public string Property1
{
get
{
return _property1;
}
set
{
_property1 = value;
}
}
string _property1;

But declaring like this will show the TextBox. But the requirement is to create a property which allows the user with masked Text (TextBox TextMode: Password).

It can be achieved by using ToolPart

1. Create a class and inherit the WebPart class

2. Create a property for our password property


private string _password;

public string Password

{

get

{ return _password; }

set

{ _password = value; }

}

3. Override GetToolParts()


public override ToolPart[] GetToolParts()

{

ToolPart[] toolparts = new ToolPart[3];

WebPartToolPart wptp = new WebPartToolPart();

CustomPropertyToolPart customtp = new CustomPropertyToolPart();

PasswordToolPart passtp = new PasswordToolPart("Password");

toolparts[0] = passtp;

toolparts[1] = wptp;

toolparts[2] = customtp;

return toolparts;

}

4. Create a new Class namely PasswordToolPart.cs and inherit the ToolPart


class PasswordToolPart:ToolPart

{

public TextBox txtPassword=null;

private void PasswordToolPart_Init(object sender, EventArgs e)

{

txtPassword = new TextBox();

txtPassword.TextMode = TextBoxMode.Password;

txtPassword.ID = "txtPassword";

SyncChanges();

}

public PasswordToolPart(string strTitle)

{

this.Title = strTitle;

this.Init += new EventHandler(PasswordToolPart_Init);

}

public override void ApplyChanges()

{

base.ApplyChanges();

}

public override void SyncChanges()

{

EnsureChildControls();

}

protected override void CreateChildControls()

{

base.CreateChildControls();

Controls.Add(txtPassword);

}

}

5. Build the project and deploy the web Part.

Advertisements

Responses

  1. Great example, thanks!

    I have your example working in SharePoint 2010. However, I would like to group the input field with the other custom settings under the “settings” heading. Can you tell me what to change?

    Thank you,
    Michael

  2. Where Sharepoint to store the password? And in what form?
    Can I use this field to store AD credentials?

    • Hi Bill,
      SharePoint does not have any field type to store your password. If so, you have to write a custom DataType. here in Webpart i have created a Custom Property and masked the values while typing. But in background it will have the real value. What is meant by storing AD Credentials? can you explain that more..


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: