Home > SharePoint 2010, SharePoint Designer 2010, Visual Studio 2010 > Walkthrough 3: SharePoint Designer Workflows imported to Visual Studio 2010

Walkthrough 3: SharePoint Designer Workflows imported to Visual Studio 2010

 

Exercise Duration : 20 minutes

Exercise Overview

This exercise shows how to create a custom workflow activity used by a SharePoint Designer reusable workflow and deploy them together as a single *.WSP. It also shows how to import that whole workflow model into Visual Studio 2010.

Feature Overview 

SharePoint Designer allows creating a workflow that is reusable across multiple lists and multiple sites. The workflow can be exported as a SharePoint Solution File (*.WSP). 

Task 1: Create and Prepare Project 

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 template.

5. Use SPDWorkflowDemo 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 Deploy as a farm solution, and press Finish.

 

image

Task 2: Create a new Workflow Activity for use by the Reusable Workflow

1. Right-click on the SPDWorkflowDemo solution in the Solution Explorer and select Add | New Project.

2. Under the Visual C# | Workflow project templates, select  the Workflow Activity Library template.   

3. Name the project SPDActivityDemo and press OK to add the project to the SPDWorkflowDemo solution. 

 

image

4. Right-click on the SPActivityDemo project in the Solution Explorer and select Add Reference.

5. Under the .Browse tab, browse to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI  and select both Microsoft.SharePoint.dll and Microsoft.SharePoint.WorkflowActions.dll.

6. Press OK to add these two references to the SPDActivityDemo project.

7. Right-click on the Activity1.cs (under the SPActivityDemo project in the Solution Explorer) and select rename.

8. Rename the file CreateDocumentLibrary.cs.

9. Select Yes when the Microsoft Visual Studio dialog appears to allow Visual Studio to re-factor the code.

 

 

Task 3: Add code to create a document library based on parameters passed to our activity.

 

1. Right-click on the CreateDocumentLibrary.cs activity and select View Code.

2. Change the CreateDocumentLibrary base class from SeqeuenceActivity to : Activity

 

public partial class CreateDocumentLibrary: Activity

 

3. Add the following using statements to the activity:

 

using Microsoft.SharePoint;

using Microsoft.SharePoint.Workflow;

using Microsoft.SharePoint.WorkflowActions;

 

4. Add a new DependencyProperty to the CreateDocumentLibrary class named UrlProperty of type string. (Hint – type wdp inside of the class definition and then tab twice – this will create the DependencyProperty using the built-in Workflow Dependency Property snippet)

This will be the location where the document library will be created.

 

public static DependencyProperty UrlProperty = DependencyProperty.Register("Url", typeof(string), typeof(CreateDocumentLibrary));

[DescriptionAttribute("Url of base site")]

[CategoryAttribute("Input Property")]

[BrowsableAttribute(true)]

[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]

[ValidationOption(ValidationOption.Required)]

public string Url

{

    get

    {

        return ((string)(base.GetValue(CreateDocumentLibrary.UrlProperty)));

    }

    set

    {

        base.SetValue(CreateDocumentLibrary.UrlProperty, value);

    }

}

 

5. Add a new DependencyProperty  of type string to the activity named DocLibNameProperty.

This will be the name of the document library created by the activity.

 

 

public static DependencyProperty DocLibNameProperty = DependencyProperty.Register("DocLibName", typeof(string), typeof(CreateDocumentLibrary));

[DescriptionAttribute("Used as doc lib name")]

[CategoryAttribute("Input Property")]

[BrowsableAttribute(true)]

[DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]

[ValidationOption(ValidationOption.Required)]

public string DocLibName

{

    get

    {

        return ((string)(base.GetValue(CreateDocumentLibrary.DocLibNameProperty)));

    }

    set

    {

        base.SetValue(CreateDocumentLibrary.DocLibNameProperty, value);

    }

}

 

6. Add the following code inside of the CreateDocumentLibrary class:

 

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

    CreateDocLib();

    return ActivityExecutionStatus.Closed;

}

 

private void CreateDocLib()

{

    using (SPSite sps = new SPSite(Url))

    {

        using (SPWeb spw = sps.RootWeb)

        {

            Guid ID = spw.Lists.Add(DocLibName, DocLibName + " Document Library", SPListTemplateType.DocumentLibrary);

            SPList spdl = spw.Lists[ID];

            spdl.OnQuickLaunch = true;

            spdl.Update();

        }

    }

}

 

Task 4: Configure activity for deployment.

 

1. To deploy the assembly to the GAC with your SharePoint project you need to configure the project to have a strong-name.

2. Right-click on the SPActivityDemo project and select Properties.

3. Click on the Signing tab in the properties page.

4. Select New under the Choose a strong name key file text from the combo box.

5. Type key as the Key file name value, uncheck Protect my key file with a password and press OK.

 

image

6. Build your project (CTRL-SHIFT-B or use Build | Build Solution) and fix any errors.

7. Right click on the SPDWorkflowDemo project and click Add | SharePoint Mapped Folder.

image

8. Browse to Template\1033\Workflow and select OK to add the mapped folder to the SPWorkflowDemo

9. Right-click on the  SPDWorkflowDemo folder in the Solution Explorer (under the Workflow folder).  

10. Right-click on the Workflow folder and select Add | New Item.

11. Select XML File from the list of Installed Templates, name the file SPDActivityDemo.ACTIONS¸ and select OK to add the file.

image

 

12. Replace the contents of SPDActivityDemo.ACTIONS with the following XML

 

<WorkflowInfo>

  <Actions Sequential="then" Parallel="and">

    <Action Name="Create Document Library"

  ClassName="SPDActivityDemo.CreateDocumentLibrary"

  Assembly="SPDActivityDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1a4a7a2c3215a71b"

  AppliesTo="all"

  Category="Labs">

      <RuleDesigner Sentence="Document Library Name %1 to site %2.">

        <FieldBind Field="DocLibName" Text="Document Library Name"  

           DesignerType="TextArea" Id="1"/>

        <FieldBind Field="Url" Text="Url of base site" Id="2" 

           DesignerType="TextArea"/>

      </RuleDesigner>

      <Parameters>

        <Parameter Name="DocLibName" Type="System.String, mscorlib" 

      Direction="In" />

        <Parameter Name="Url" Type="System.String, mscorlib" 

      Direction="In" />

      </Parameters>

    </Action>

  </Actions>

</WorkflowInfo>

10. IMPORTANT – Update the PublicKeyToken within the .ACTIONS file with the public key token from SPDActivityDemo.dll. Do not use the PublicKeyToken in this example. Ensure you generate your own. 

11. To find the PublicKeyToken start a Visual Studio 2010 Command Prompt from the Start | All Programs | Visual Studio 2010 | Visual Studio Tools |Visual Studio Command Prompt (2010) menu

12. Type the following command:

 

sn -tc:\SPHOLS\SPDWorkflowDemo\SPDActivityDemo\bin\Debug\SPDActivityDemo.dll | clip

13. In the of SPDActivityDemo.ACTIONS file highlight the value of the PublicKeyToken ("1a4a7a2c3215a71b") and paste (CTRL-V).

14. Remove all the pasted text except the value of your public key.  Make sure there are no extra spaces or line feed characters after the public key token (the quote needs to be next to the value of the public key token).

15. Highlight the whole value of the Assembly attribute and copy that value to the clipboard (CTRL-C).

 

 

Task 5: Add the SPDActivityDemo activity to be deployed with SPDWorkflowDemo.

1. In the Solution Explorer, under the SPDWorkflowDemo project, double-click on Package folder

2. In the Package designer click on Advanced (at the bottom of the designer in the middle pane).

3. Click on the Add button.

4. Click on the ellipses button in the Add Custom Assembly dialog.  Browse to the SPDActivityDemo.dll file (C:\SPHOLS\SPDWorkflowDemo\SPDActivityDemo\bin\debug).

5. Within Safe Controls add the following:

 

Assembly Name: SPDActivityDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=YOURPUBLICKEY – ensure you use a Public Key Token you generated using the sn.exe utility. (Hint – you can paste the value you copied to the clipboard in the last step here).

Name Space: SPDActivityDemo

Safe: Checked

Type Name : *

6. Select OK to add the assembly as part of the SharePoint project deployment package.

 

image

 

Task 6: Configure a Feature using Feature Designer

1. Right-click on SPDWorkflowDemo project Features folder and select Add Feature.

2. Right-click on the Feature1 node and select Rename. Rename it to SPDWorkflowDemoFeature. 

3. In the Feature designer change the value of the Scope combo box to WebApplication.

4. Change the feature title to SPDWorkflowDemoFeature. 

 

image

Task 7: Add and code a Event Receiver for the Feature

1. Right-click on the SPDWorkflowDemoFeature in the Solution Explorer and select Add Event Receiver 

 

image

2. Add a using statement to the top of the code file that appears in the editor:

 

using Microsoft.SharePoint.Administration;

 

3. Add the following code to your FeatureReceiver class declaration: 

 

 

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

    SPWebApplication wappCurrent = (SPWebApplication)properties.Feature.Parent;

    SPWebConfigModification modAuthorizedType = new SPWebConfigModification();

    modAuthorizedType.Name = "AuthType";

    modAuthorizedType.Owner = "SPDActivityDemo";

    modAuthorizedType.Path = 

    "configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes";

    modAuthorizedType.Type =

    SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;

    modAuthorizedType.Value = @"<authorizedType Assembly=""SPDActivityDemo, 

    Version=1.0.0.0, Culture=neutral, PublicKeyToken=YOURPUBLICKEYTOKEN"" 

    Namespace=""SPDActivityDemo"" TypeName=""*"" Authorized=""True"" />";

    wappCurrent.WebConfigModifications.Add(modAuthorizedType);

    wappCurrent.WebService.ApplyWebConfigModifications();

}

 

4. Note: You will need to update the YOURPUBLICKEYTOKEN section in the above code with the correct key from the SPDActivityDemo.ACTIONS file.

5. Build and Deploy the SPDWorkflowDemo project by right-clicking on the SPDWorkflowDemo project in the Solution Explorer and select Deploy.

 

Task 8: Create a re-usable workflow using SharePoint Designer

1. Start SharePoint Designer 2010  from the start menu under All Programs | Microsoft Office | Microsoft SharePoint Designer 2010.

2. Click the Open Site button, and put in the address of the site you used to deploy the SPDWorkflowDemo project to as the Site Name.  Press Open twice. Once to open the site, the second to cause SharePoint Designer to open the site at the root level.  

 

image

 

3. Click on the Workflows node in the navigation pane on the left.

4. Click the Workflows tab in the ribbon.

5. Click the Reusable Workflow button in the Workflows ribbon.

image

 

6. Enter SPDWorkflow as the name of the workflow, and press OK to create the workflow.

 

image

 

7. Click the Workflow ribbon, then click Actions and scroll down to Labs and click Create a Document Library. Note: This is the activity you created in Visual Studio 2010 previously

image

 

8. Click the Fx button and select Current Item:Title as the document library name and type in http://servername as the Url of the base site. Click Save

image

 

9. Save the workflow (click the Save button or press CTRL-S).

10. Click the Workflows in the Navigation pane on the left again.

11. Highlight the SPDWorkflow in the right pane.

12. Click the Workflow Ribbon again.

13. Click Save as Template to save the .WSP file in the Site Assets Library SharePoint list.

image

 

14. Go back to Visual Studio 2010.

15. In the Server Explorer (if not visible, go to View | Server Explorer), expand SharePoint Connections 

16. Find your site. If your site isn’t visible, right-click on SharePoint Connections and select Add Connection.  Type in the URL of your site and press OK.

17. In your site, expand Lists and Libraries | Document Libraries

18. Right-click on Site Assets and select View in Browser.

 

image

 

19. Once the Site Assets library appears in the browser, click on the menu to the right of SPWorkflow.

20. Click Send To | Download a Copy

 

image

21. Save the SPDWorkflow.wsp file to the C:\SPHOLS directory.

 

Task 9: Import the reusable workflow into Visual Studio

1. Return to Visual Studio 2010.

2. Right-click on the SPDWorkflowDemo solution, and select Add | New Project.

3. From the SharePoint | 2010 project templates, pick the Import Reusable Workflow project template.

4. Name the project SPDWorkflowImport. 

5. Press OK.

6. On the SharePoint Customization Wizard dialog, make sure the site is the same as the site you deployed the SPDWorkflowDemo project to earlier.

7. Select Deploy as farm solution.

8. Press Next.

9. In the next dialog, browse to C:\SPHOLS and select SPDWorkflow.wsp as the template.

image

 

 

10. Click Next.

11. On the next dialog, press Finish.

12. Right-click on the SPDWorkflowImport project in the Solution Explorer and select Add Reference.

13. Select the Projects tab and select to reference the SPDActivityDemo project.  This is necessary because the newly imported workflow uses the custom activity from that project.

14. Double click on Package folder in the SPDWorkflowDemo project.

15. In the Package designer select the Converted workflows feature in the Items in the Solution pane

16. Click the add button ( > ) to move that feature to Items in the Package pane.  This will cause this feature to be deployed with the solution.

 

image

17. Right-click on the SPDWorkflowDemo project in the Solution Explorer and select Deploy.

 

Task 10: Associate the workflow with a list in SharePoint

1. Open the site you’ve deployed this project to (HINT – you can right-click on the site in the Server Explorer as a short-cut).

2. Create a new custom list named Customers. 

3. Configure the list workflow settings as follows:

 

image

4. Create a new customer list item with the Title of Contoso. A new document library of the same name should be created automatically.

image

 

Exercise Summary

In this walkthrough you built a new Workflow activity for use in SharePoint Designer workflows. You built a reusable workflow in SharePoint Designer and used the custom activity. You then imported that reusable workflow into Visual Studio 2010 and deployed the final workflow back into SharePoint.

  

Advertisements
  1. November 27, 2012 at 11:15 pm

    Greetings, I think your website could be having browser
    compatibility problems. When I look at your site in Safari, it looks fine however,
    if opening in IE, it has some overlapping issues.
    I simply wanted to give you a quick heads up! Aside from that, wonderful blog!

  2. July 22, 2013 at 1:06 pm

    Wow that was unusual. I just wrote an incredibly long comment but after I clicked submit my
    comment didn’t show up. Grrrr… well I’m not writing all
    that over again. Regardless, just wanted to say
    fantastic blog!

  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: