SharePoint 2010: Full Trust Custom Workflow Activities

Before moving on to some real action let’s get familiar with some of the facts on Activity:

  • An activity is a portion of work that is completed with the help of other resources.
  • Activities are the basic building blocks of workflows.
  • A workflow can be created by using a single activity or a group of activities. SharePoint 2010 enables both out-of-the-box and custom activities.
  • Out-of-the-box activities can be used directly in Microsoft SharePoint Designer 2010; custom activities require some coding.

Now let’s see some facts on Custom Code Activity With a wide selection of out-of-the-box activities, why are custom activities needed at all? This question is especially relevant when you consider that it is also fairly easy to execute code in an existing workflow by adding a CodeActivity and then writing the code in the ExecuteCode event handler. However, custom activities are especially helpful because they wrap custom code in a reusable component. Further, as the following examples show, some essential actions require more functionality than out-of-the-box activities can provide.

  • Looping: Out-of-the-box activities cannot provide direct looping support through collections, such as the lists in a site or the items in a list.
  • Accessing outside content: With out-of-the-box activities, it is not possible to access SharePoint content in other sites or site collections or to access anything at the farm or web application level.
  • Calling across the network: Standard actions do not support calling a remote web service from a SharePoint Designer 2010 workflow.
  • Running with elevated permissions: Out-of-the-box activities run under the current user’s identity and permissions. Although Impersonation steps can help mitigate this, workflow actions are still restricted from running under a more privileged user’s identity and elevated permissions.
  • Obtaining control at the workflow level: By its nature, SharePoint Designer 2010 insulates the workflow builder from the details of the underlying SharePoint workflow architecture.

Enough of the theory for now. Now it’s time to see some action. Steps to Implement Custom Activity:

  • Add a new project using class library project template

1

  • Add new workflow activity as shown below

2 3

  • Add code to the activity as shown below

4 5 Now we will do some investigation on each property and functions used in this code.

  • Dependency properties indicate that the workflow depends on this value. All values that are used as inputs or outputs for the custom workflow activity should be marked as dependency properties. Following are the available dependency properties in our code:
    1. SiteUrlProperty: This property maps with SiteUrl Browsable Property.
    2. ListINameProperty: This property maps with ListName Browsable Property.
    3. ListTemplateProperty: This property maps with ListTemplate Browsable Property.
  • Browsable Properties are the simple properties decorated with a number of Attributes. Browsable Properties must be mapped with corresponding Dependency Properties. Following are the available Browsable Properties in our code:
  1. SiteUrl
  2. ListName
  3. ListTemplate
  • Execute Method: The Execute method must complete by returning a value from the ActivityExecutionStatus enumeration. Enumeration of activity status values corresponds to the life cycle of an activity in a running workflow instance. Once execution of the code is complete, you must call the enumeration Closed for the activity. The following table shows different enumerations for custom activities:

6

  • CreateList Method: This is just a simple helper function which creates the SharePoint List using server side object model.

And that’s the end of our analysis. Now lets continue with the remaining steps

  • Add Strong Name Key to the Project by going to the Project Properties > Signing Tab  as shown below:

7

  • Build the solution

Steps to Implement Custom Activity Deployment Project:

  • Add Empty SharePoint Project as shown below:

8

  • Add the reference of CustomWorkflowActivties.dll in this solution
  • Add SharePoint Mapped Folder TEMPLATE\1033\Workflow
  • Add xml file with the name CreateListWorkflowActivity.Actions, make sure this file must have the .Actions extension otherwise SharePoint Runtime would not get it recognized as an workflow action.
  • Add the configuration entries to the CreateListWorkflowActivity.Actions file as shown below:

9

  • Add AuthorizedType entry to Web.config file of Web Application for this custom workflow action, else it won’t get displayed in SharePoint Designer > Workflows > Action ribbon menu

Note: To make a full-trust workflow activity available to declarative workflows, add an authorizedType entry to the Web.config file for the content web application. This gives your custom activity the same status as the built-in workflow activities that are included with SharePoint 2010. The following code example shows the format of an authorizedType entry. 10

  • Build the solution
  • Deploy the solution

And with this we are done with our development work, now it is the time to test our efforts. Steps to test Custom Activity:

  • Launch SharePoint Designer
  • Connect to SharePoint Site
  • Click on left navigation link “Workflows”

11

  • Click on Site Workflows in the ribbon. Enter Name and Description for the workflow.

12 13

  • Choose Action Menu > Custom Workflow Activities > Create SharePoint List Action

14

  • Configure Create SharePoint List Action by providing following information
  1. List Name
  2. Site Url
  3. List Template

15

  • The activity will look like as below after configuration

16

  • Save the Workflow by selecting Save option in the ribbon

17

  • Check for the errors in Workflow before publishing it by selecting Check for Errors option in the ribbon
  • Publish the Workflow by selecting Publish option in the ribbon
  • Navigate to the SharePoint Site
  • Click Site Settings > View All Site Content

18

  • Click on Site Workflows Link

19

  • Start the workflow

20 21 22

  • On completion of Workflow, go to View Site Content > Lists, and you should be able to see the new list of type Announcement

24 Hope this will help someone in need…

Advertisements

How to Get Public Key for SharePoint Projects

Visual Studio Version: 2010

SharePoint Foundation development projects often involve a mixture of imperative coding and XML markup. Frequently, you add your new assembly’s Public Key Token to a project file, such as an XML configuration file.

If your Visual Studio project is based on any of the SharePoint 2010 project templates in Visual Studio, you can simply insert the placeholder $SharePoint.Project.AssemblyPublicKeyToken$ where the Public Key Token should be in most kinds of project files (but not .cs or .vb files or other compilable files).

When you build and deploy the project, Visual Studio will replace the placeholder with the Public Key Token in the copies of the files that are actually deployed.

If you need to insert the Public Key Token in a file that does not support the placeholders, you will need a way to obtain the Public Key Token. This topic explains how to add an item to the Visual Studio Tools menu that can be used to obtain the Public Key Token of an assembly under development.

  • Launch Visual Studio and click on External Tools under Tools Menu as shown below:

1

  • Click on Add button

2

  • Enter Title of your choice
  • Enter Command as the path to sn.exe file, you can find this command line utility at the following location C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\x64\sn.exe
  • Enter Arguments as -T $(TargetPath), this will be replaced by the selected project later on

Make sure “Use Output Window” checkbox is checked so that you can be able to get the Public Key on the Output Window with in Visual Studio.

3

  • Select the project for which you want to find out the Public Key

4

  • Click on Get Public Key under Tools menu

5

  • Public Key Token is now available on the output window

6

That’s so simple yet powerful.

It is really useful for a SharePoint Developer to have these kind of tools handy.

Hope this will help someone in need…

SharePoint 2010: How to Read SharePoint List using SharePoint REST API and JSON

In this article we will see how JSON can be leveraged to access the SharePoint Lists using SharePoint REST API.

In order to execute this article we would need a SharePoint List which can be accessed using REST API later on.

For this demonstration we have a list by the name “LOB”, with one of the list columns by the name “LOB”.

1

The next step is to visit the Project Structure for this demonstration.

Project Structure:

2

Based on the above project structure following are required artifacts to be developed in order to see JSON in action:

  • SharePoint Mapped Folder: GetSharePointListInJASON
  • Application Page : SharePointListInJASON.aspx

In order to develop this functionality we need to add the Application Page which will contain the logic to access SharePoint List using REST API and JSON.

3

Definition of SharePointListInJASON.aspx

First of all we need to create a link which on click triggers the function “getListItems”.

4

Now let’s dive deep into the execution of getListItems function.

getListItems function executes as follows:

  • Specify the valid Url calling the REST Service
  • Create a WebRequest Object using ASP.Net 3.5 Ajax API
  • Specify the Request Type, in this case it is GET
  • Specify the Accept Header which defines the data format of the Response, in this case it is JSON
  • Specify the Callback function which will be executed by the runtime once the request is completed

onCompletedCallBack call back function executes as follows:

  • Parse the JSON response into a set of objects by using the JavaScript eval() function
  • Loop through the object to retrieve the required values
  • Prepare Html markup based on the retrieved values and display it on the page.

5

And we are done.

Now build the solution and deploy it.

Navigate to the SharePointListInJASON.aspx Application Page which resides under layout directory in SharePoint Root (14 Hive) as shown below:

6

Click the link and see the list items coming as a part of the response.

7

Now let’s do some investigation on some of the internals of this asynchronous service call by using a Web Proxy Tool known as Fiddler:

If we follow the Fiddler, we can see traffic capture for this asynchronous service call as shown below:

8

If we inspect the different attributes of this request we find the following important information about this asynchronous service call:

9

If we inspect the response object, we will find the incoming JSON structure as follows, this information is very important to understand how to use the response the response object in code. Properties exposed by the response object are highlighted as follows:

10

That’s the end of this simple implementation employing SharePoint Rest API and JSON.

Hope this will help someone in need…

SharePoint 2010: Delegate Controls in Action

We can following noteworthy points on Delegate Controls in SharePoint:

  • SharePoint provides a mechanism that allows developers to customize out-of-the-box functionality in pages, without the need for modifying the pages that come with SharePoint. This mechanism is provided with delegate controls.
  • A delegate control defines a region in an aspx page that allows the content to be replaced with our custom content. This custom content is created via a SharePoint feature, and when deployed and activated, replaces the standard content at runtime.
  • By declaring a control as an element in a Feature and giving it a priority through the Sequence attribute of the Control element, SharePoint selects the declared control candidate and instantiates it as a child of the delegate control. At run time, this delegate control accepts the union of control elements declared at the server farm, Web application, site collection, and Web site levels.
  • The control that has the lowest sequence number is added to the control tree through the DelegateControl.
  • Delegate controls for which the AllowMultipleControls property equals true can host more than one child control.
  • If there are multiple delegate control candidates, they are all added as children at runtime in the order specified by the Sequence attribute. For example, by default the AllowMultipleControls property is used in the AdditionalPageHead delegate control to allow multiple child controls to be added to any page that contains the delegate control.

Now let see the delegate control in action.

In this article we will see the steps involved in replacing the SharePoint global navigation with Control ID “GlobalNavigation”. We will discuss the significance of this Control ID attribute in detail later in this article.

Project Structure:

1

Based on the above project structure following are required artifacts to be developed in order to get the delegate control working:

  • Custom Control   :          GlobalNavigation.ascx
  • Module                :          GlobalNavigation
  • Web Scoped Feature :  CustomDelegateControl

Custom Control: First of all we need to create a custom control that will be used to replace the existing SharePoint Delegate Control.

Definition of GlobalNavigation.ascx

2

GlobalNvaigation.ascx file contains the UI logic for the delegate control, in this case this is simply declaring a label control that will contains the urls of all lists in current site.

Definition of GlobalNavigation.ascx.cs

3

GlobalNavigation.ascx.cs file contains the logic to display all the lists from current site.

Element.xml

4

The information in Element.xml file is very important to understand, as this information tells SharePoint which Delegate Control needs to be replaced.

  • Id Attribute contains the value of the OOB SharePoint Delegate Control, make sure you specify correct value, else this won’t work.
  • Sequence Attribute contains the value of the sequence in the control tree, make sure this value is less than the sequence value for OOB SharePoint Delegate Control else OOB SharePoint Delegate Control won’t get replaced by the custom control.
  • ControlSrc Attribute contains the path of the custom control relative to the ControlTemplates Directory under SharePoint Root Folder (14 Hive).

Web Scoped Feature:

5

Now we are done with all the deployable, and it’s time to create a feature which will deploy the artifacts in SharePoint Farm.

We need to create a Web Scoped feature, which allows us to activate this Global Navigation Control on the specific sites instead of the complete site collection, though this can be designed based on the business requirements.

Now build the solution and deploy.

Final Output:

6

And we are good to go.

Hope this will help someone in need…

SharePoint 2010: Connected Web Parts in action

Connected WebParts are best suited for the scenarios where data communication between two WebParts is required at run time.

Following are the most basic steps involves in the development of Connected Web Parts:

Step1 : Creating WebPart connection interface

Step2 : Creating Provider WebPart using WebPart Project Item

Step3 : Creating Consumer WebPart using WebPart Project Item

Now lets see the connected WebPart in action.

  • Create Empty SharePoint Project

1

2

  • Creating WebPart connection interface : Add a class file which contains the definition of the interface.

3

  • Interface Definition

4

  • Creating Provider WebPart using WebPart Project Item : Add new project item based on WebPart project item type

5

6

  • Provider.cs: This file implements the business logic for Provider WebPart. This class will implement ConnectionProvider Property

78

  • Element.xml: This file will expose the deployment information of Provider WebPart to SharePoint

9

  • Creating Consumer WebPart using WebPart Project Item

10

11

  • Consumer.cs: This file implements the business logic for Provider WebPart. This class will implement ConnectionConsumer Property

1213

  • Element.xml: This file will expose the deployment information of Consumer WebPart to SharePoint
  • Creating Site Level Feature for the deployment of connected WebParts

14.1

  • Build and Deploy solution
  • Test Connected WebParts
  •      Create a new WebPart Page

15

16

17

  •      Add Provider and Consumer WebParts on the WebPart Page

18

  •     Create connection between Provider and Consumer WebParts

19

20

  •    Enter string in the Provider WebPart and click Send Button, the string value is transferred to the Consumer WebPart using connection between Provider and Consumer WebPart.

21

  •  Value transferred to Consumer WebPart on button click.

22

Hope this will help someone in need…