Docstoc

Lab

Document Sample
Lab Powered By Docstoc
					Hands-On Lab
Implementing a Custom Workflow Action in
Visual Studio
Lab version:    1.0
Last updated:   10/19/2011
CONTENTS

OVERVIEW .................................................................................................................................................. 3
   System Requirements................................................................................................................................ 3

SETUP ........................................................................................................................................................... 3
       Task 1 – Create SharePoint Site............................................................................................................ 3
       Task 2 – Create SharePoint Online Site ................................................................................................ 4
       Task 3 – Upload Prerequisite Solutions ................................................................................................ 5
       Task 4 – Prepare Code Snippets ........................................................................................................... 6

EXERCISE 1: IMPLEMENT A CUSTOM WORKFLOW ACTION IN VISUAL STUDIO .......................... 7
       Task 1 – Create a Custom Workflow Action in Visual Studio ............................................................. 7
       Task 2 – Deploy Custom Workflow Action to SharePoint ................................................................. 11
       Task 3 – Incorporate Custom Workflow Action into SharePoint Designer Workflow ...................... 12
       Task 4 – Test Custom Workflow Action ............................................................................................ 15

EXERCISE 2: TEST THE CUSTOM ACTION IN SHAREPOINT ONLINE .............................................. 19
       Task 1 – Deploy Custom Workflow Action to SharePoint Online ..................................................... 19
       Task 2 – Save the Updated Workflow as a Template ......................................................................... 20
       Task 3 – Upload the Updated Workflow Template to SharePoint Online .......................................... 21
       Task 4 – Associate the Purchase Request Approval Workflow .......................................................... 23

SUMMARY ................................................................................................................................................. 25
Overview
Lab Time: 30 minutes.
Lab Folder: C:\%Office365TrainingKit%\Labs\3.2.
Lab Overview: Building automated workflows into SharePoint Online solutions allows you to integrate a
business process directly into the collaboration tools your customers use every day. The SharePoint
Online platform provides an integrated way to design, develop and customize workflows while providing
the right tools for each audience.
In this lab, you will use Visual Studio to implement a custom workflow action. You will integrate the
custom workflow action into the Non-Standard Business Purchase Request workflow from Lab 3.1 in
order to create a website once the workflow is approved. After testing the custom workflow action in
SharePoint, you will deploy it to SharePoint Online and use SharePoint Designer 2010 to integrate it into
the Non-Standard Business Purchase Request workflow.

System Requirements
You must have the following items to complete this lab:
       SharePoint 2010
       SharePoint Designer 2010

       Visio 2010
       Visual Studio 2010
       SharePoint Online



Setup
 Note: You only need to perform these steps if you haven’t completed Lab 3.1.

Task 1 – Create SharePoint Site
In this task, you will create a sub site for this lab in your SharePoint on-premise site.
        1. Navigate to your top-level SharePoint site, e.g., http://intranet.contoso.com.
        2. From Site Actions, choose New Site.
        3. Choose the Team Site template.
        4. Enter Lab03 for the name of the site to create.
            a. The site will be created at http://intranet.contoso.com/Lab03.
        5. Click Create to create the site.
        6. After the site is created, navigate back to the top level site; e.g.:
           http://intranet.contoso.com.
        7. From Site Actions, choose Site Settings.
        8. Under the Site Collection Administration section, click Site collection features.
        9. Ensure that the SharePoint Server Enterprise Site Collection Features feature is activated.
             a. This feature is required for the Visio-based workflow visualization feature.
        10. Ensure that the SharePoint Server Publishing Infrastructure feature is activated.
             a. This feature creates the appropriate security groups for approval workflows, e.g. the
                Approvers group.
        11. Navigate back to the site you created for this lab; e.g.: http://intranet.contoso.com/Lab03.
        12. From Site Actions, choose Site Settings.
        13. Under the Site Actions section, click Manage site features.
        14. Activate the SharePoint Server Publishing feature.

Task 2 – Create SharePoint Online Site
In this task, you will create a sub site for this lab in your SharePoint Online site.
        1. Navigate to your top-level SharePoint Online site, e.g., https://contoso.sharepoint.com.
        2. From Site Actions, choose New Site.
        3. Choose the Team Site template.
        4. Enter Lab03 for the name of the site to create.
        5. Click Create to create the site.
        6. After the site is created, navigate back to the top level site; e.g.:
           https://contoso.sharepoint.com.
        7. From Site Actions, choose Site Settings.
        8. Under the Site Collection Administration section, click Site collection features.
        9. Ensure that the SharePoint Server Enterprise Site Collection Features feature is activated.
             a. This feature is required for the Visio-based workflow visualization feature.
        10. Ensure that the SharePoint Server Publishing Infrastructure feature is activated.
             a. This feature creates the appropriate security groups for approval workflows, e.g. the
                Approvers group.
       11. Navigate back to the site you created for this lab; e.g.:
           http://contoso.sharepoint.com/Lab03.
       12. From Site Actions, choose Site Settings.
       13. Under the Site Actions section, click Manage site features.
            a. Activate the SharePoint Server Publishing feature.

         Note: You have to be an administrator on your SharePoint Online website to perform the tasks
         in this lab, e.g., creating and publishing a SharePoint Designer workflow to the SharePoint
         Online site.
         You will also need an account that is a member of the Approvers group in order to approve
         workflow items.


Task 3 – Upload Prerequisite Solutions
In this task, you will prepare for the lab by uploading a solution both locally and to SharePoint Online
that will create the list you need for the rest of the tasks in this lab.
       1. Launch Internet Explorer and navigate to your top-level SharePoint site, for example,
          http://intranet.contoso.com.
       2. Click Site Actions >> Site Settings.
       3. Under Galleries, click on the Solutions link to view the site collection’s Solution
          Gallery.




       4. Click on the Solutions tab in the ribbon to view the Upload Solution button.




       5. Click the Upload Solution button.
       6. Browse to C:\%Office365TrainingKit%\Labs\3.1\Source\After\Visual
          Studio\Workflow.Setup.wsp and click Open and OK.
       7. In the Solution Gallery – Activate Solution dialog, click the Activate button on the Ribbon to
          activate the solution.

         Note: Activating the solution will close the dialog automatically.




       8. Browse to the Lab03 site.
       9. Click Site Actions >> Site Settings and under the Site Actions group choose Manage Site
          Features.
       10. Activate the Workflow.Setup Feature1 feature.
       11. The Non-Standard Business Purchase Requests list should now be visible in the Quick
           Launch.
       12. Upload the same solution to SharePoint Online by navigating to your top-level SharePoint
           Online site, e.g., https://contoso.sharepoint.com and performing steps 2-11.

Task 4 – Prepare Code Snippets
This lab contains code snippets that you will use to complete the solution. You can either copy the
snippets from the lab manual itself or install the code snippets so that you can access them directly from
Visual Studio 2010.
       1. Browse to C:\%Office365TrainingKit%\Assets\Code Snippets\CSharp.
       2. Select all the files in this directory and copy them to your clipboard by pressing [Ctrl]+[a] and
          then [Ctrl]+[c].
       3. Browse to ..\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets
       4. Press [Ctrl]+[v] to paste the code snippet files into this directory.
       5. Browse to C:\%Office365TrainingKit%\Assets\Code Snippets\XML.
       6. Select all the files in this directory and copy them to your clipboard by pressing [Ctrl]+[a] and
          then [Ctrl]+[c].
       7. Browse to ..\Documents\Visual Studio 2010\Code Snippets\XML\My Xml Snippets.
       8. Press [Ctrl]+[v] to paste the code snippet files into this directory.
       9. In this lab, you can use Visual Studio 2010 user tasks created as //TODO comments to
          navigate to sections in the code where you will insert a code snippet.
       10. After you open a project in Visual Studio 2010
       11. From Visual Studio 2010, go to View >> Task List to open the Task List window.
       12. Select Comments in the drop down list.
       13. Pin the Task List window to the bottom of Visual Studio 2010.
       14. You will use the TODO comments in the Task List to navigate to locations in the code where
           you will add code snippets from the lab script.

         Note: To reduce typing, you can right-click where you want to insert source code, select Insert
         Snippet, select My Code Snippets and then select the entry matching the current exercise
         step.



Exercise 1: Implement a Custom
Workflow Action in Visual Studio
Task 1 – Create a Custom Workflow Action in Visual Studio
In this task, you use Visual Studio 2010 to create a custom workflow action that creates a site when the
workflow is approved.
The final step of the Non-Standard Business Purchase Request Approval workflow is to create a
SharePoint site for the user to fill out a trip report to share with others in the company. SharePoint
Designer doesn’t contain a workflow action to create a site; you’ll create a custom action to include in
the workflow.
       1. Launch Visual Studio 2010.
       2. Create a new Empty SharePoint Project in
          C:\%Office365TrainingKit%\Labs\3.2\Source\Before\Visual Studio called
          SPDCustomWorkflowActions.
3. In the SharePoint Customization Wizard dialog, for What local site, enter the name of the
   SharePoint site you created for this lab, e.g., http://intranet.sharepoint.com/Lab03 and
   select Deploy as a sandboxed solution.
4. Click Finish to create the project.
5. Add a new class called CreateSiteAction to the project.
6. Change the CreateSiteAction’s class accessibility to public.
7. Under the existing using statements, insert the following code snippet to add the required
   using statements.
(Code snippet 3.2.1)
C#
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.UserCode;

8. Create a function called CreateSite using the code snippet below.
(Code snippet 3.2.2)
C#
public Hashtable CreateSite(SPUserCodeWorkflowContext context, string
siteName)

     This function accepts an instance of SPUserCodeWorkflowContext for the current site and a
     string parameter for the name of the site to create.
     The workflow uses a Hashtable property bag to return values from the workflows, e.g.,
     whether or not the workflow succeeded (and the exception if there one was thrown).
9. Add the following code snippet to the body of the CreateSite function.
(Code snippet 3.2.3)
C#
Hashtable results = new Hashtable();
try
{
    using (SPSite site = new SPSite(context.CurrentWebUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            web.Webs.Add(
                siteName,
                "Trip Report: " + siteName,
                string.Empty,
                1033,
                "STS",
                    false,
                    false);
          }
      }

      results["success"] = true;
      results["exception"] = string.Empty;
}
catch (Exception e)
{
    results = new Hashtable();
    results["exception"] = e.ToString();
    results["success"] = false;
}

return results;

   The code gets the context to the current Site using the workflow context, and then opens
   the current Web and creates a site within it with the specified name.
   The Hashtable property bag is populated with a success and exception key to indicate the
   workflow’s success or failure and record the exception details if there was an exception.
10. Add a new Empty Element item to the SharePoint project and call it
    CreateSiteActionDefinition. You will use this element to describe the custom workflow
    actions contained in this project.
11. In Solution Explorer, under CreateSiteActionDefinition, open the Elements.xml file.
12. Add a WorkflowActions element inside Elements:
(Code snippet 3.2.4)
XML
<WorkflowActions>

</WorkflowActions>

In this section, you will define the custom actions that are packaged with this feature.
13. Add an Action element at Elements/WorkflowActions:
    a. The Name attribute specifies the name of the action as it will appear in SharePoint
       Designer.
    b. SandboxedFunction indicates that the workflow action is sandboxed.
    c. ClassName and FunctionName refer to the full namespace of the class that contains the
       function describing the action.
(Code snippet 3.2.5)
XML
<Action Name="Create Site"
    SandboxedFunction="true"
    Assembly="$SharePoint.Project.AssemblyFullName$"
    ClassName="SPDCustomWorkflowActions.CreateSiteAction"
    FunctionName="CreateSite"
    AppliesTo="all"
    UsesCurrentItem="true"
    Category="Sandboxed Workflow Actions">
</Action>

14. Add a RuleDesigner element at Elements/WorkflowActions/Action. This describes the
    parameters that you have to provide when adding this action to the workflow design
    surface.
(Code snippet 3.2.6)
XML
<RuleDesigner Sentence="Create Site with name %1 (exceptions logged to %2)">
    <FieldBind Field="siteName" Text="Site Name" Id="1"
        DesignerType="TextBox" />
    <FieldBind Field="exception" Text="Exception" Id="2"
        DesignerType="ParameterNames" />
</RuleDesigner>

When you add the CreateSite action to the SharePoint Designer workflow design surface, you’re
able to associate the siteName parameter with the Title of the item that was created in the
Non-Standard Business Purchase Requests list.
15. Finally, add a set of Parameter elements at Elements/WorkflowActions/Action.
(Code snippet 3.2.7)
CAML
<Parameters>
  <Parameter Name="__Context"
Type="Microsoft.SharePoint.WorkflowActions.WorkflowContext,
              Microsoft.SharePoint.WorkflowActions"
                      Direction="In"
                      DesignerType="Hide" />
  <Parameter Name="siteName"
                      Type="System.String, mscorlib"
                      Direction="In"
                      DesignerType="TextBox"
                      Description="Name of the site to create" />
  <Parameter Name="exception"
                      Type="System.String, mscorlib"
                      Direction="Out"
                      DesignerType="ParameterNames"
                      Description="Exception encountered"/>
</Parameters>
            a. The __Context parameter is the instance of the workflow context that it used as a
               parameter to the CreateSite function.
            b. The siteName parameter is also used by the CreateSite function and represents the
               name of the site to create.
            c. The exception parameter is returned by the workflow action and comes from the
               Hashtable property bag returned by the CreateSite function.
       16. When you added the Empty Element item to the project, a feature called Feature1 was
           created automatically.
       17. Open Feature1.
       18. Make sure the Scope of Feature1 is set to Site to ensure that the feature is deployed at the
           site collection level.




       19. Save your changes.

Task 2 – Deploy Custom Workflow Action to SharePoint
In this task you will deploy the custom workflow action to SharePoint.
       1. From the Solution Explorer window, right-click the SPDCustomWorkflowActions project and
          select Package to package the solution WSP.
       2. Launch Internet Explorer and navigate to your top-level SharePoint site collection; e.g.:
          http://intranet.contoso.com.
       3. Click Site Actions >> Site Settings.
       4. Under Galleries, click on the Solutions link to view the site collection’s Solution
          Gallery.
       5. Click on the Solutions tab in the ribbon to view the Upload Solution button.
       6. Click the Upload Solution button.
       7. Browse to C:\%Office365TrainingKit%\Labs\3.2\Source\Before\Visual
          Studio\SPDCustomWorkflowAction\bin\Debug\SPDCustomWorkflowActions.wsp and click
          Open and OK.
       8. In the Solution Gallery – Activate Solution dialog, click the Activate button on the Ribbon to
          activate the solution.
      9. Click the Activate button on the ribbon to activate the solution.

Task 3 – Incorporate Custom Workflow Action into SharePoint Designer Workflow
      1. Browse to your SharePoint site; e.g.: http://intranet.contoso.com/Lab03.
      2. Click Site Actions >> Edit in SharePoint Designer to edit the site in SharePoint Designer
         2010.
      3. Click the Workflows link in the left navigation.




      4. In the right-pane, open and edit the Non-Standard Business Purchase Approval workflow.




      5. Click the Edit workflow link to edit the workflow.
6. In the “Yes” branch of the If condition, move the mouse under the Email:Current
   Item:Created By link until you see an orange bar.




7. Click the orange bar.
8. Click the Actions item on the Workflow tab in the ribbon, and insert a Create Action activity
   from the Sandboxed Workflow Actions category.




9. The CreateSite action is added to the logic of the “Yes” branch of the If condition.




10. Click the Site Name link and associate it with the Current Item’s Title property in the list.




11. Save the workflow.
    The revised workflow should now look like this:
12. Click Publish on the Workflow tab in the ribbon to publish the workflow to your SharePoint
    site.




SharePoint Designer will validate the workflow and publish it to the Lab03 site in SharePoint.
13. Browse to your SharePoint Online site; e.g.: https://contoso.sharepoint.com/Lab03.
14. From the Quick Launch, browse to the Non-Standard Business Purchase Request list.
15. In the ribbon, open the List tab and click Workflow Settings.




16. Select Purchase Request from the drop down list.
17. You can see that a new version of the Non-Standard Business Purchase Approval workflow
    has been associated with the list.
Task 4 – Test Custom Workflow Action
      1. Browse to your SharePoint site; e.g.: http://intranet.contoso.com/Lab03.
      2. Open the Non-Standard Business Purchase Requests list from the Quick Launch.
      3. Click the Add new item link to create a new item in the list.
      4. Create a new item in the list, for example:

           ◦   Title: SharePoint Conference
           ◦   Requested By: << Enter your account here >>
           ◦   Description: SharePoint Conference 2010 in Anaheim, CA
           ◦   Price: 4999
5. Select the newly created item in the list and click Workflows in the ribbon.




    The workflows screen displays all the workflows that you can start on this list item.
6. Click the Non-Standard Business Purchase Approval workflow to start an instance of the
   workflow on the list item.




7. The workflow initiation form appears and prompts the user to enter the business rationale
   for their request. Enter “I’d like to learn all about SharePoint Online”.




8. Click Start to start the workflow.
9. Open the workflows of the list item to see the status of the workflow. You can now see an
   instance of the Non-Standard Business Purchase Approval workflow under the Running
   Workflows section.
10. Click on the workflow instance.
11. To approve the workflow, you would typically have to log out and log back in with a user
    who is a member of the Approvers group. In this case, you are already logged in as an
    administrator so you can approve the workflow.
12. On the same screen, in the Tasks section, click the title of the approval task.




13. The Workflow Task dialog contains information about the task that was assigned to the
    Approvers group.
14. Click Approve to approve the request.
15. Go back to the Non-Standard Business Purchase Requests list and open the workflows page
    for the request list item.




You can see the instance of the Non-Standard Business Purchase Approval workflows in the
Completed Workflows section.


16. Click on the workflow instance.
The workflow visualization is updated to show the path that the user took through the workflow.
The checkmark on each step indicates that the step was completed successfully.
       Notice that the custom action that you added in the previous exercise is now visible in the
       workflow visualization.
       17. From Site Actions, select View All Site Content.
       18. The site that was created by the custom workflow action appears in the Sites and
           Workspaces section.
       19. Open the site.




        A trip report site was created by the custom workflow action for the user to fill out a trip report.


Exercise 2: Test the Custom Action in
SharePoint Online
Task 1 – Deploy Custom Workflow Action to SharePoint Online
In this task you will deploy the custom workflow action to SharePoint Online.
       1. From the Solution Explorer window, right-click the SPDCustomWorkflowActions project and
          select Package to package the solution WSP.
       2. Launch Internet Explorer and navigate to your top-level SharePoint Online site; e.g.:
          http://contoso.sharepoint.com.
       3. Click Site Actions >> Site Settings.
       4. Under Galleries, click on the Solutions link to view the site collection’s Solution
          Gallery.
       5. Click on the Solutions tab in the ribbon to view the Upload Solution button.
       6. Click the Upload Solution button.
       7. Browse to C:\%Office365TrainingKit%\Labs\3.2\Source\Before\Visual
          Studio\SPDCustomWorkflowAction\bin\Debug\SPDCustomWorkflowActions.wsp and click
          Open and OK.
       8. In the Solution Gallery – Activate Solution dialog, click the Activate button on the Ribbon to
          activate the solution.




       9. Click the Activate button on the ribbon to activate the solution.

Task 2 – Save the Updated Workflow as a Template
In this task, you use SharePoint Designer to save your completed workflow as a template. This will
create a .wsp file, which can be used to move the workflow from a development environment to a test
or production environment. You will upload this file to SharePoint Online. The file can also be imported
into Visual Studio for developers to investigate how the workflow is assembled.

 Note: If you import the .wsp into Visual Studio, you will only be able to import it as a farm solution,
 which cannot be re-deployed to SharePoint Online.

       1. Open SharePoint Designer
       2. From the left navigation, select Workflows >> Non-Standard Business Purchase Approval
       3. Select Save as Template.
       4. The .wsp will be saved to the Site Assets library of your local SharePoint site, e.g.,
          http://intranet.contoso.com/Lab03/SiteAssets.
       5. Open the Site Assets library, select Non-Standard Business Purchase Approval.wsp and click
          the Preview in Browser icon. You will be prompted to save the file; save the file to the
          desktop.




 Note: If you completed Lab 3.1, the file Non-Standard Business Purchase Approval.wsp will already
 exist in the Site Assets folder. The new file will be saved as Non-Standard Business Purchase
 Approval2.wsp; after you save this file to your desktop, rename it back to Non-Standard Business
 Purchase Approval.wsp.


Task 3 – Upload the Updated Workflow Template to SharePoint Online
In this task, you will upload the updated Non-Standard Business Purchase Request workflow template to
SharePoint Online.
       1. Browse to your top-level SharePoint Online site; e.g.: https://contoso.sharepoint.com.
       2. Click Site Actions >> Site Settings.
       3. Under Galleries, click on the Solutions link to view the site collection’s Solution Gallery.
4. The Non-Standard Business Purchase Approval.wsp will already be in the Solution Gallery if
   you completed Lab 3.1.
5. Select and the solution and click Deactivate to deactivate it.




6. Click on the Solutions tab in the ribbon to view the Upload Solution button.




7. Click the Upload Solution button.
8. Browse to your desktop and select Non-Standard Business Purchase Approval.wsp and click
   Open and OK.
9. In the Solution Gallery – Activate Solution dialog, click the Activate button on the ribbon to
   activate the solution.
        Note: Activating the solution will close the dialog automatically.

      10. Browse to the Lab03 site; e.g.: https://contoso.sharepoint.com/Lab03.
      11. Click Site Actions >> Site Settings and under the Site Actions group choose Manage Site
          Features.
      12. Activate the Workflow template "Non-Standard Business Purchase Approval" from web
          template "Lab03" feature.

Task 4 – Associate the Purchase Request Approval Workflow
      1. Browse to your SharePoint Online site; e.g.: https://contoso.sharepoint.com/Lab03.
      2. Open the Non-Standard Business Purchase Requests list from the Quick Launch.
3. Select List >> Workflow Settings from the ribbon




4. Select Purchase Request from the drop down list.
5. Click Remove a workflow.
6. Choose No New Instances, and click OK. This prevents any new instances of the old version
   of the Non-Standard Business Purchase Request Approval workflow to be created on the list.




7. Select Purchase Request from the drop down list, and click Add a workflow.
       8. In the next screen, select the Non-Standard Business Purchase Approval Workflow
          workflow template name the workflow Non-Standard Business Purchase Approval - Custom
          Action.




       9. Click Save on the next screen.
       10. The updated version of the workflow is now associated with the list.
       11. Follow the steps in Exercise 1 >> Task 4 to test the custom workflow action.



Summary
In this lab you created a sandboxed custom workflow action in Visual Studio 2010 to have the workflow
create a site when the item is approved.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:6
posted:10/19/2011
language:English
pages:25