Importing SharePoint 2010 Site Definitions in Visual Studio 2010

SharePoint Visual How To

Summary:  Learn how to import a Microsoft SharePoint 2010 site definition using Microsoft Visual Studio 2010 and Microsoft SharePoint Designer 2010.

Applies to: Office 2010 | SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010

Provided by:  Ben Hedges, Point8020

Overview

Microsoft Visual Studio 2010 provides a project type that enables you to import a site definition and add code. You can then deploy this site definition to the SharePoint Solutions Gallery and create new sites based on the site definition.

Code It

This Microsoft SharePoint Visual How To describes the following steps to show how to import a site definition in Microsoft SharePoint 2010:

  • Creating a blank site in SharePoint 2010.

  • Editing the blank site in Microsoft SharePoint Designer 2010.

  • Saving the modified site as a SharePoint template.

  • Exporting the SharePoint solution package template (WSP file) from SharePoint 2010 to disk.

  • Opening the WSP file in Visual Studio to customize it.

  • Adding a Web Part to the solution.

  • Uploading the customized WSP file to the SharePoint Solutions Gallery, and then creating a new site based on the customized solution.

To create a blank site in SharePoint 2010

  1. Open the default SharePoint site.

  2. On the Site Actions menu, click New Site.

  3. In the dialog box, click Blank Site.

  4. In the Title box, type BlankTemplate.

  5. In the URL Name box, type BlankTemplate, and then click Create.

To open the site in SharePoint Designer

  1. On the Site Actions menu, click Edit in SharePoint Designer.

  2. In SharePoint Designer 2010, on the navigation menu, click Lists and Libraries.

  3. On the ribbon, click Document Library, and in the drop-down list, click Document Library.

  4. In the Name box, type Resumes, and then click OK.

  5. In the Document Libraries, ensure that Resumes is selected, and then click Edit Columns on the ribbon.

  6. On the ribbon, click Add New Column, and in the drop-down list, click Yes/No (Checkbox).

  7. In the Column Name box, type Interviewed, and then press Enter.

  8. Above the ribbon, click the Save button.

  9. On the navigation menu on the left side of the screen, click BlankTemplate.

  10. On the ribbon, click Save as template. The SharePoint site opens.

  11. In the File name box, type MasterTemplate.

  12. In the Template name box, type MasterTemplate.

  13. Select the Include Content check box, and then click OK.

  14. In the Operation Completed Successfully message box, click OK.

To export the site definition file

  1. In your web browser, open the root website.

  2. On the Site Actions menu, click Site Settings.

  3. On the Site Settings page, in the Galleries section, click Solutions.

  4. In the Solution list, click MasterTemplate, and then click Save.

  5. Enter C:\temp for the download location for the WSP file, and then click Save. Close the download dialog box.

To open the WSP file in Visual Studio

  1. In Visual Studio 2010, on the File menu, point to New, and then click Project.

  2. In the Installed Templates section, click Visual Basic or Visual C#, click SharePoint, and then click 2010.

  3. In the Templates pane, click Import SharePoint Solution Package.

  4. In the Name box, type Recruitment.

  5. In the Location box, type C:\temp, and then click OK.

  6. In the SharePoint Customization Wizard, select your target website.

  7. On the What is the trust level for this SharePoint solution? page, click Deploy as a Sandboxed solution, and then click Next.

  8. On the What is the path for the existing solution package? page, click Browse, select the MasterTemplate.wsp file that you saved earlier, click Open, and then click Next.

  9. On the Select items to import page, click Finish.

  10. In Solution Explorer, right-click the Recruitment project, point to Add, and then click New Item.

  11. In the Add New Item template, click Web Part.

  12. In the Name box, type InterviewStatus, and then click Add.

  13. In the InterviewStatus file, replace the protected override CreateChildControls() method with the following code.

    Protected Overrides Sub CreateChildControls()
        Try
            Dim thisWeb As SPWeb = SPContext.Current.Web
            Dim resumes As SPList = thisWeb.Lists("Resumes")
            Dim interviewedQuery As SPQuery = New SPQuery()
            Dim itemsReturned As SPListItemCollection
            Dim interviewedYes As Integer = 0
            Dim interviewedNo As Integer = 0
            interviewedQuery.ViewFields = "<FieldRef Name='Interviewed'/>"
            interviewedQuery.ViewFieldsOnly = True
            interviewedQuery.Query = "<Where><Eq><FieldRef Name='Interviewed'/>" _
                + "<Value Type='Boolean'>1</Value></Eq></Where>"
            itemsReturned = resumes.GetItems(interviewedQuery)
            interviewedYes = itemsReturned.Count
            interviewedQuery = New SPQuery()
            interviewedQuery.ViewFields = "<FieldRef Name='Interviewed'/>"
            interviewedQuery.ViewFieldsOnly = True
            interviewedQuery.Query = "<Where><Eq><FieldRef Name='Interviewed'/>" _
                + "<Value Type='Boolean'>0</Value></Eq></Where>"
            itemsReturned = resumes.GetItems(interviewedQuery)
            interviewedNo = itemsReturned.Count
    
            If ((interviewedNo + interviewedYes) > 0) Then
                Me.Controls.Add(New LiteralControl( _
                "<H2>Interview Progress</H2><BR>Applicants that have been interviewed:" _
                 & (interviewedYes / (interviewedYes + interviewedNo)) * 100 & " %"))
            Else
                Me.Controls.Add(New LiteralControl( _
                "<H2>Interview Progress</H2><BR>No Resumes have been added."))
            End If
        Catch ex As Exception
            Me.Controls.Add(New LiteralControl("Error: " + ex.Message))
        End Try
    End Sub
    protected override void CreateChildControls()
    {
        try
        {
            SPWeb thisWeb = SPContext.Current.Web;
            SPList resumes = thisWeb.Lists["Resumes"];
            SPQuery interviewedQuery = new SPQuery();
            SPListItemCollection itemsReturned;
            int interviewedYes = 0;
            int interviewedNo = 0;
    
            interviewedQuery.ViewFields = "<FieldRef Name='Interviewed'/>";
            interviewedQuery.ViewFieldsOnly = true;
            interviewedQuery.Query = 
            "<Where><Eq><FieldRef Name='Interviewed'/><Value Type='Boolean'>1</Value></Eq></Where>";
            itemsReturned = resumes.GetItems(interviewedQuery);
            interviewedYes = itemsReturned.Count;
            interviewedQuery = new SPQuery();
            interviewedQuery.ViewFields = "<FieldRef Name='Interviewed'/>";
            interviewedQuery.ViewFieldsOnly = true;
            interviewedQuery.Query = 
            "<Where><Eq><FieldRef Name='Interviewed'/><Value Type='Boolean'>0</Value></Eq></Where>";
            itemsReturned = resumes.GetItems(interviewedQuery);
            interviewedNo = itemsReturned.Count;
    
            if ((interviewedNo + interviewedYes) > 0)
            {
                this.Controls.Add(new LiteralControl(
                "<H2>Interview Progress</H2><BR>Applicants that have been interviewed:"
                  + ((double)interviewedYes / (interviewedYes + interviewedNo)) * 100 + " %"));
            }
            else
            {
                this.Controls.Add(new LiteralControl(
                "<H2>Interview Progress</H2><BR>No Resumes have been added."));
            }
        }
        catch (Exception ex)
        {
            this.Controls.Add(new LiteralControl("Error: " + ex.Message));
        }
    }
  14. In Solution Explorer, expand InterviewStatus, and then open Elements.xml.

  15. In the Elements.xml file, change the URL attribute to InterviewStatus.SummaryPart.

  16. In the Elements.xml file, in the property element, change the Value attribute to Recruitment.

    The Elements.xml file should resemble the following.

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="https://schemas.microsoft.com/sharepoint/" >
        <Module Name="InterviewStatus" List="113" Url="_catalogs/wp">
            <File Path="InterviewStatus\InterviewStatus.webpart" 
            Url="InterviewStatus.Summarypart" Type="GhostableInLibrary">
            <Property Name="Group" Value="Recruitment" />
            </File>
        </Module>
    </Elements>
  17. On the toolbar, click Save.

  18. In Solution Explorer, right-click the Recruitment project, and then click Package.

  1. Open the root SharePoint site.

  2. On the Site Actions menu, click Site Settings.

  3. On the Site Settings page, in the Galleries section, click Solutions.

  4. Select the MasterTemplate check box, and then on the ribbon, click Deactivate.

  5. In the dialog box, click Deactivate.

  6. Select the MasterTemplate check box, and then, on the ribbon, click Delete. Click OK.

  7. On the ribbon, click Upload Solution.

  8. Browse to the Bin\Debug folder for your package; for example, C:\temp\recruitment\recruitment\bin\debug.

  9. Click Recruitment.wsp, and then click Open.

  10. In the Upload document dialog box, click OK.

  11. In the Activate solution dialog box, click Activate.

To deploy the project

  1. On the Site Actions menu, click New Site.

  2. In the list of templates, click MasterTemplate.

  3. In the Title box, type Recruitment.

  4. In the URL box, type Recruitment, and then click Create.

  5. On the Quick Launch menu, click Resumes.

  6. In the Resumes document library, click Add Document, browse to a document to upload, and then click Open. Click OK.

  7. In the dialog box, enter a title for the resume, and select the Interviewed check box to indicate that this person has been interviewed. Click Save.

  8. Add another document, but this time, do not select the Interviewed check box.

  9. On the ribbon, click the Browse tab, and then click Recruitment in the breadcrumb bar.

  10. On the ribbon, click the Page tab, and then click Edit Page.

  11. Click inside the Left Web Part area.

  12. On the ribbon, click the Insert tab, and then click Web Part.

  13. In the Categories section, click Recruitment.

  14. In the Web Parts section, click InterviewStatus.Summarypart, and in the About the Web Part area, click Add.

  15. On the ribbon, click the Page tab, and then click Stop Editing.

    The Web Part is displayed on the page.

Read It

This SharePoint Visual How To demonstrates the following tasks:

  • Creating a blank SharePoint 2010 site as a subsite from the SharePoint master site. This site is opened in SharePoint Designer 2010 for customization.

  • Editing the site in SharePoint Designer and creating a new document library for candidate resumes. The new document library has an additional Yes/No column added that indicates whether the person who presented the resume has been interviewed.

  • Saving the modified site as a template in SharePoint Designer.

  • Locating the modified template in SharePoint by clicking Site Settings, clicking Galleries, and then clicking Solutions. The template is saved as a WSP file.

  • Opening and customizing the WSP file in Visual Studio 2010.

  • Adding a Web Part to the package that uses a Collaborative Application Markup Language (CAML) query to determine the number of resumes that have resulted in an interview and the number that have not resulted in an interview. These results are displayed on a literal control as a percentage.

  • Saving the solution as a WSP file in Visual Studio 2010.

  • Uploading the new template and removing the existing templates.

  • Creating a new site using the customized Recruitment site template.

  • Uploading documents to the Resumes document library and setting the Interviewed column appropriately.

  • Editing a page and selecting the Web Part that was added to the package in Visual Studio 2010.

See It

Watch the video

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/1d26d2dd-a5f6-4bf7-9bee-e803fc63fd9f]

Length: 00:06:01

Click to grab code

Grab the Code

Explore It

About the Author

Ben Hedges is Senior Vice President, Research and Development at Point8020. Ben is especially interested in modeling Talent Management, Compliance, and Learning and Development solutions on SharePoint. Ben has a wealth of experience fulfilling customer requirements by using Microsoft technologies.