Home > SharePoint 2010, Visual Studio 2010 > Walkthrough 2: Building a Web Part for a Sandboxed Solution

Walkthrough 2: Building a Web Part for a Sandboxed Solution

 

Exercise Duration: 20 minutes

Exercise Overview

This exercise demonstrates creating a web part that renders and updates list data that is deployed as a Sandboxed Solution.

Feature Overview  

A sandboxed solution can be deployed to a site by a site administrator, without requiring intervention from the farm administrator. The solution has full access to the immediate site and restricted access to system resources and other sites.

Task 1: Create Sandboxed Solution Project with a webpart.

1. Open Visual Studio 2010 from the Start | Programs | Visual Studio 2010 menu.

2. Create a new project by using File | New Project.

3. Pick the SharePoint | 2010 templates.

4. From the SharePoint | 2010 templates select the Empty Project project template.

5. Use SBSolutionDemo as the name.

6. Set the location to be C:\SPHOLs.

7. Press OK to create the project.

image

8. When the SharePoint Customization Wizard dialog appears, select the site you want to deploy the Web Part to.

9. Select the Deploy as a sandboxed solution radio button

10. .Press the Finish button to complete the project creation process.

image

 

11. Right-click on the SBSolutionDemo project in the Solution Explorer and choose Add | New Item.

12. Select the Web Part (not the Visual Web Part) template from the SharePoint | 2010 templates in the Add New Item dialog.  Name it SBWebPart.  

image

13. Press OK to close the dialog and add the item to the project.

 

Task 2: Add code to provide querying and rendering functionality.

1. In the Solution Explorer, double-click on the SBWebPart.cs file to open it in the editor window.

2. Add the following using statement after the other using statements.

 

 

using System.Web.UI.HtmlControls;

 

3. Add the following variables to the class declaration.

 

DropDownList _ddlProjects = new DropDownList();

TextBox _tbDescription = new TextBox();

TextBox _tbDueDate = new TextBox();

 

4. Add the following new methods within the class declaration.

 

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;

    }

}

 

5. Add the following CreateChildControls implementation beneath the previous code (replace the existing CreateChildControls method).

 

/* Build the UI and setup events */

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);

}

 

6. Add the following code beneath the CreateChildControls method:

 

 

/* Populate the projects drop down */ 

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()));

    }

}

 

7. Add the following event handling code beneath the PopulateProjects method:

 

 

/* Change projects handler */

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();

}

 

Task 3: Build and Deploy the Sandboxed Solution.

1. Right-click on your project in the Solution Explorer and select Deploy. 

2. Browse to your server. Click Site Actions | Site Settings  .  Then click on  Solutions (under Galleries), you will see your sandboxed solution deployed and activated. 

 

image

 

3. Add the webpart to a webpart page.

Click the Edit button to put the page in edit mode. 

Click Insert

Click Web Part

Click Miscellaneous

Click the SBWebPart Title

Click Add 

 

image

 

4. Now you will see the web part running in the Sandboxed Solution. Click the drop down to select a project. The description and due date fields are both updatable.

 

image

 

Exercise Summary

In this walkthrough you built and deployed a Web Part that runs in the context of a Sandboxed Solution.

  

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: