Sunday 18 March 2012

Create custom EditorPart for Sandbox webpart - SharePoint 2010


Firstly to create Sandbox Visual Webparts i have installed
Visual Studio 2010 SharePoint Power Tools extension
and i am using Visual Webpart (Sandbox) item in an Epty Solution to create a visual Sandbox webpart.

In order to create a custom EditorPart for my Sandbox webpart see the example below. I have added a textbox to provide list name for my webpart.

Code -
//Declare the properties
public override object WebBrowsableObject
{
get
{
return this;
}
}

//Task list name string
private String _taskList = null;

[Personalizable(), WebBrowsable()]
public String taskList
{
get { return _taskList; }
set { _taskList = value; }
}

//Create an editor part to set the custom task list
public override EditorPartCollection CreateEditorParts()
{
ArrayList editorArray = new ArrayList();
CustomProperty edPart = new CustomProperty();
edPart.ID = this.ID + "_editorPart1";
editorArray.Add(edPart);
EditorPartCollection editorParts = new EditorPartCollection(editorArray);
return editorParts;
}

private class CustomProperty : EditorPart
{
TextBox _tbTaskList;
public CustomProperty()
{
Title = "Custom WebPart Settings";
}

public override bool ApplyChanges()
{
CustomWebPart part = (CustomWebPart)WebPartToEdit;
//Update the custom WebPart control with the task list
part.taskList = tbTaskList.Text;
return true;
}

public override void SyncChanges()
{
CustomWebPart part = (CustomWebPart)WebPartToEdit;
String currentList = part.taskList;
}

protected override void CreateChildControls()
{
Controls.Clear();

//Add a new textbox control to set the task list
_tbTaskList = new TextBox();

Controls.Add(_tbTaskList);
}


protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("List Name :");
writer.WriteBreak();
_tbTaskList.RenderControl(writer);
writer.WriteBreak();
}

//Return the list name from the textbox
private TextBox tbTaskList
{
get
{
EnsureChildControls();
return _tbTaskList;
}
}

CustomWebPart in above code is my webpart name.

Ads by Google

1 comment:

  1. Can you provide me the fill sample code of the same?

    My Email ID: sharmila.k88@gmail.com

    ReplyDelete