Thursday, 9 August 2012

Create a Web Part for a Sandboxed Solution


1. Open the VS2010
2. Click on file, new and project
3. Select Visual C# ,SharePoint,2010 and Empty SharePoint Project.
4. Name the project [SBSSample] and the location.
5. Click on ok
6. In the SharePoint Customization Dialog, change the local site to use for debugging to http://mohan:8087
7. Leave the trust level for the SharePoint solution as Deploy as a sandboxed solution.
8. Click Finish.

9. Right-click on the SBSSample project in the Solution Explorer and select Add | New item.
10. In the Add New Item dialog, select to add a new Web Part and name it SBWebPart..



11. Open SBWebPart.cs and add the following code.

            using System.Web.UI.HtmlControls;

12. Add the following variables to the SBWebPart class

            DropDownList ddlProjects = new DropDownList();
TextBox tbDescription = new TextBox();
TextBox tbDueDate = new TextBox();

13. Add the following new methods within the SBWebPart class

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!Page.IsPostBack)
GetProjectDetails();
}
/* Populate the text boxes with the selected project details */
private void GetProjectDetails()
{
EnsureChildControls();
if (_ddlProjects.SelectedValue != "-- Select a Project --")
{
SPList pList = SPContext.Current.Web.Lists["Projects"];
int nProjectID = Convert.ToInt32(ddlProjects.SelectedValue);
SPListItem spliProject = pList.GetItemById(nProjectID);
tbDescription.Text = spliProject["Description"].ToString();
DateTime dueDate = Convert.ToDateTime(spliProject["Due_x0020_Date"]);
tbDueDate.Text = dueDate.ToShortDateString();
}
else
{
tbDescription.Text = String.Empty;
tbDueDate.Text = String.Empty;
}
}

protected override void CreateChildControls()
{
base.CreateChildControls();
Panel parent = new Panel();
parent.Style.Add("border", "solid 1px Navy");
parent.Style.Add("background-color", "#EEEEEE");
parent.Style.Add("width", "250px");
ddlProjects.ID = "ddlProjects";
ddlProjects.AutoPostBack = true;
ddlProjects.SelectedIndexChanged += new
EventHandler(ddlProjects_SelectedIndexChanged);
PopulateProjects();
parent.Controls.Add(_ddlProjects);
Panel panel = new Panel();
Label label = new Label();
label.Text = "Description";
panel.Controls.Add(label);
parent.Controls.Add(panel);
panel = new Panel();
panel.Controls.Add(_tbDescription);
parent.Controls.Add(panel);
label = new Label();
label.Text = "Due Date";
panel = new Panel();
panel.Controls.Add(label);
parent.Controls.Add(panel);
panel = new Panel();
panel.Controls.Add(_tbDueDate);
parent.Controls.Add(panel);
panel = new Panel();
Button bUpdateProject = new Button();
bUpdateProject.Text = "Update Project";
bUpdateProject.Click += new EventHandler(bUpdateProject_Click);
panel.Controls.Add(bUpdateProject);
parent.Controls.Add(panel);
Controls.Add(parent);
}

private void PopulateProjects()
{
SPList splProjects = SPContext.Current.Web.Lists["Projects"];
_ddlProjects.Items.Add("-- Select a Project --");
foreach (SPListItem spli in splProjects.Items)
{
_ddlProjects.Items.Add(new ListItem(spli.Title, spli.ID.ToString()));
}
}
void ddlProjects_SelectedIndexChanged(object sender, EventArgs e)
{
GetProjectDetails();
}
/* Update the current project */
void bUpdateProject_Click(object sender, EventArgs e)
{
EnsureChildControls();
int nProjectID = Convert.ToInt32(ddlProjects.SelectedValue);
SPListItem spliProject =
SPContext.Current.Web.Lists["Projects"].GetItemById(nProjectID);
spliProject["Description"] = tbDescription.Text;
spliProject["Due_x0020_Date"] = tbDueDate.Text;
spliProject.Update();
}

14. Build and Deploy

1.Right-click the SBSSample project and select the Package to create a .wsp file.
2. Open Internet Explorer and browse to http://mohan:8087
3. Open the Site Actions menu and select Site Settings.
4. Under the Galleries section select Solutions.
5. Select the Solutions tab.
6. On the Solutions tab, select Upload Solution.
7. In the Upload Document dialog that pops up, browse to the .wsp file at C:\Samples\ SBSSample.wsp.
8. Click OK to upload the SBSSample.wsp to SharePoint. Leave the Overwrite existing files box checked.
9. Click Activate in the Solution Gallery - Activate Solution dialog. The Sandboxed Solution web part is now ready to be used.
10. Open the Site Actions menu and select More Options.
11. Under the Page section of the Create dialog, select Web Part Page. Click Create.
12. Name the new Web Part Page SBSolutionDemoPage, set the Layout to Full Page, Vertical. And the Save Location to Shared Documents.
13. Click Create to create the new Web Part Page.
14. Select the middle area of the new Web Part Page and then click the new Insert tab that appears in the top toolbar.
15. Select Web Part and then under Categories Custom and under Web Parts choose SBWebPart.
16. Click Add to add the Sandboxed Solution web part to the page.
17. In the Ribbon, click Page. Next, click Stop Editing in the toolbar


 

No comments:

Post a Comment