Thursday, July 26, 2012

The request failed with HTTP status 401: Unauthorized.

There are two methods to work around this issue, use one of the following methods, as appropriate for your situation.

Method 1: Specify host names (Preferred method if NTLM authentication is desired)

To specify the host names that are mapped to the loopback address and can connect to Web sites on your computer, follow these steps:
  1. Set the
    DisableStrictNameChecking
    registry entry to 1. For more information about how to do this, click the following article number to view the article in the Microsoft Knowledge Base:
    281308 Connecting to SMB share on a Windows 2000-based computer or a Windows Server 2003-based computer may not work with an alias name
  2. Click Start, click Run, type regedit, and then click OK.
  3. In Registry Editor, locate and then click the following registry key:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0
  4. Right-click MSV1_0, point to New, and then click Multi-String Value.
  5. Type BackConnectionHostNames, and then press ENTER.
  6. Right-click BackConnectionHostNames, and then click Modify.
  7. In the Value data box, type the host name or the host names for the sites that are on the local computer, and then click OK.
  8. Quit Registry Editor, and then restart the IISAdmin service.

Method 2: Disable the loopback check (less-recommended method)

The second method is to disable the loopback check by setting the DisableLoopbackCheck registry key.

To set the DisableLoopbackCheck registry key, follow these steps:
  1. Set the
    DisableStrictNameChecking
    registry entry to 1. For more information about how to do this, click the following article number to view the article in the Microsoft Knowledge Base:
    281308 Connecting to SMB share on a Windows 2000-based computer or a Windows Server 2003-based computer may not work with an alias name
  2. Click Start, click Run, type regedit, and then click OK.
  3. In Registry Editor, locate and then click the following registry key:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa
  4. Right-click Lsa, point to New, and then click DWORD Value.
  5. Type DisableLoopbackCheck, and then press ENTER.
  6. Right-click DisableLoopbackCheck, and then click Modify.
  7. In the Value data box, type 1, and then click OK.
  8. Quit Registry Editor, and then restart your computer.

Tuesday, July 17, 2012

Download SharePoint 2010 build level and version numbers

Very interesting url - 
http://blogs.technet.com/b/sharepointjoe/archive/2011/02/01/sp2010-sharepoint-2010-build-level-and-version-numbers.aspx

Monday, July 16, 2012

SharePoint 2013 Beta is released Today (16-July-12)

Microsoft released SharePoint 2013 today which requires
1. Windows Server 2012
2. SQL Server 2012
3. Visual Studio 2012

This will support to Office 2013.

Installation details -
http://www.gokanozcifci.be/subsite/install-and-configure-sharepoint-13-preview.html?goback=.gde_2252791_member_135256809

Training details -
http://www.criticalpathtraining.com/Pages/default.aspx


TechNet official site-
http://technet.microsoft.com/en-us/sharepoint/fp142366


Friday, June 15, 2012

The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator

One of the major reasons that this List View Threshold (LVT) feature was created is to protect the server from unintentional load that may either bring it down, or at least cause other users higher latency or failures. Changing this limit (default 5000) is quite simple, but I wouldn't recommend it unless you are positive that it will not negatively affect your system. One valid example of when you might want to do this is if you are using your farm to serve heavily cached content, that only gets updated once a day, and do not want the limit to apply for that. Even in that case, I'd recommend that you test this thoroughly before changing it. There's an awesome white paper out there that describes in full details what effects this has on the server, with a lot of pretty graphs and such to depict the performance implications.  Here it is: Designing Large Lists and Maximizing List Performance (http://technet.microsoft.com/en-us/library/ff608068(office.14).aspx). Also here's a link to the help topic that explains the basic limits and what they mean: http://office2010.microsoft.com/en-us/sharepoint-server-help/manage-lists-and-libraries-with-many-items-HA010378155.aspx?redir=0  
If you've got your mind set on changing the LVT or another resource throttling setting, here's how to do it:
1- Login to Central Admin
2- Go to Application Management -> Manage Web Applications
3- Pick the Web application for which you want to change the LVT (If you only have 1 web app plus the central admin one, the one you want to pick is the 1 web app; changing this for the central admin does you no good)
4- In the ribbon above, click General Settings. That will bring down a menu, from which you should pick Resource Throttling
5- Change the LVT (first item in this list) to another value and press OK, but please try to keep it to a reasonable number!

Following those steps will take you to the page where you can also edit a bunch of other settings. Here's a list of them, and a brief description of what they do and best practices or recommendations on how to set them:

- List View Threshold for Auditors and Administrators: This is by7 default a "higher limit". Queries that are run by an auditor or administrator that specifically (programmatically) request to override the LVT will be subject to this limit instead. It's 20,000 by default as opposed to the 5,000 for the LVT. I wouldn't raise this past 20,000 for the same reasons of not raising the LVT.  If you'd like to read more about how to use this, take a look at this post.

 - Object Model Override: If you commonly use custom code on your deployment, and have a need for overriding the LVT to a higher limit, then it may be a good idea to allow the object model override, and give auditor or administrator permissions to the application that will perform the queries. This setting is on by default, but you may disable it if you do not need it. A good example of when you might want to use this is if you've implemented some code that will perform caching of a larger set of results that are accessed often for, say, several minutes.  If you are not planning on caching the content, and are planning on running these queries often, then I wouldn't recommend using this method to get around the LVT as it will adversely affect your server's performance. In short: "tread lightly". If you'd like to read more about how to use this, take a look at this post.

- List View Lookup Threshold: This feature limits the number of joins that a query can perform. By number of joins, I mean the number of Lookup, Person/Group, or Workflow Status fields that are included in the query.  So for example, if you have a view that displays 6 lookup columns, and filters on another 3 distinct lookup columns then by default that view won't work, since the List View Lookup Threshold is 8, and the view is attempting to use 9 lookups.  I would recommend that you do not increase this number beyond 8, because through thorough testing we've observed that there's a serious non-gradual performance degradation that shows up above 8 joins. Not only does the throughput that the server can handle drop significantly at that point, but the query ends up using a disproportionately large amount of the SQL Server's resources, which negatively affects everybody else using that same database.  If you'd like to read more about this, take a look at the "Lookup columns and list views" section of this white paper: http://technet.microsoft.com/en-us/library/ff608068(office.14).aspx 

- Daily Time Window for Large Queries: This feature allows you to set a time every day where users can 'go wild'. Some people call it "happy hour", but I really think it would be a very unhappy hour for the server so I avoid that terminology :-). There are a few things that you should carefully consider before deciding what time to set this to:
  1. It should be an off-peak hour, or at least a time during which you expect the least load, so as to affect the least number of individuals. If you pick the time to be in the middle of the work day for the majority of your users, then even those who are not using the large list may be affected negatively. 
  2. Try to keep it to a reasonable timeframe such that people can actually use it to fix their lists, rather than bug the farm admin (possibly you!) about it. If, for example, you set it to be "2-3 am", then it's unlikely that the users will be very happy about that. They won't want to wake up at 2 am just to delete this large list they no longer need, so they're more tempted to ask the farm admin to handle it for them.
  1. Remember that operations started during the window won't just abort once the window ends.. So if your window lasts till 9am, and at 9 you need the server to be crisp and clear because you get a huge load spike, people who started their list delete at 8:59 may negatively affect that experience.
  1. Consider different time zones. This is especially important if your organization or customers (if you're hosting SharePoint for others) are heavily geographically distributed. Setting it to 6pm may seem like a good idea for your own location, but would not be great in say, Sydney, Australia.

- List Unique Permissions Threshold: This is the number of unique permissions allowed per list. If you have a folder that you break inheritance on for permissions, and set some permissions for it (and all the items inside it), then that counts as 1 against your List Unique Permissions Threshold. Unlike the LVT and other settings, this threshold is not triggered by viewing the content or performing some other operation on it, but explicitly when changing permissions. If you can afford to, then I would recommend reducing this number. It defaults to 50,000 and that is a lot of unique permissions! Your list is very likely to encounter problems with permissions before it reaches this number, so preemptively tweaking it to what might work in your environment is a good idea.

Thanks for reading.

Thursday, June 2, 2011

SSO between MOSS2007 and Cognos 8.4

Recently I Implemented single sign-on between MOSS 2007 and Cognos 8.4. I followed the guidelines given by IBM team here
http://public.dhe.ibm.com/software/dw/dm/cognos/security/general/integrating_microsoft_sharepoint_portal_2007_or_sharepoint_services_3.0_with_c8v3.pdf

but it is not given detail steps what to do on MOSS server and cognos server in case of both software are installed on different servers.

Below are my findings and would like to share with all of you.

Note: - Here assuming that MOSS2007 and Cognos 8.4 are installed on separate servers and both softwares are working fine.

1.1 Set Virtual Directory Properties

1.1.1 CognosR3

Create virtual directory in IIS (CognosR3 name is just example given in this document). Set its properties as below

· Local path – Webcontent folder

· Read, Log visits and index this resource is checked

· Application Name – cognosR3

· Execute Permissions – None

· Application Pool - DefaultAppPool

Go to ‘Security Directory’ tab and set below properties

· Enable anonymous access - selected

1.1.2 Cgi-Bin

Create virtual directory as cgi-bin under cognosR3 directory in IIS and set its properties as below

· Local path – cgi-bin folder

· Log visits and index this resource is checked.

· Application Name – cgi-bin

· Execute Permissions – Scripts and Executables

· Application Pool - DefaultAppPool

Go to ‘Security Directory’ tab and set below properties

· Enable anonymous access - selected

1.2 Set Environment tab

In Cognos configuration, set the below properties

· Gateway URI – http:// address:port number>/cognosR3/cgi-bin/Cognos.cgi

· Allow namespace override – True

Note – Replace all ‘localhost’ with IP address in all URLs.

1.3 Set Portal Services

In Cognos configuration, open ‘Portal services’ and set below properties

· Web Content URI - http:// address>/cognosR3/cgi-bin/cognosisapi.dll/wsrp/cps4/portlets/nav?b_action=cps.wsdl&wsdl (Optional)

· Trusted Signon Namespace ID – ADS

· Shared secret – Training (Optional)

1.4 Set Namespaces

1.4.1 ADS

In Cognos configuration, create ‘ADS’ as new namespace and set its properties as below

· Type – NTLM

· Namespace ID – ADS

· NTLM domain name –

· Advanced properties – singleSignonOption (Optional)

1.4.2 CPS Trust

In Cognos configuration, create ‘CPSTrust’ as new namespace and set its properties as below

· Type – Custom Java Provider

· Namespace ID – CPSTrust

· Java class name – com.cognos.cps.auth.CPSTrustedSignon

Note: - After setting all properties in Cognos configuration, save and restart it.


2.1 Copy SharePoint folder

Copy ‘SharePoint’ folder from Cognos server location “:\Program Files\Cognos\c8\cps\SharePoint”

To SharePoint server at below location

C:\Inetpub\wwwroot\WSS\VirtualDirectories\

Note: - Please take the backup of ‘bin’ and ‘wpresources’ folders. During copying the folders, you may get a warning that you are about to overwrite existing files/folders. Select ‘Yes to all’.

2.2 Config files

2.2.1 Wss_cognostrust.config

Copy below file to location “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG”

2.2.2 Web.config

Open web.config. sample below file and make the changes in existing web.config file which are commented in // IBM COGNOS // lines

2.3 Dwp files

Open dwp files from location (“C:\Inetpub\wwwroot\WSS\virtualDirectories\\wpcatalog”) and make changes in tags below

http:// address>/cognosR3/cgi-bin/cognosisapi.dll/wsrp/cps4/portlets/nav?b_action=cps.wsdl&wsdl

Note: -Need to make changes in all .dwp files

2.4 Reset IIS

After making all changes, need to reset IIS and open SharePoint site.


2.5 Add web parts

Open SharePoint page and edit it. Add Cognos Navigator web part. It should display home page from Cognos site as below.

Sunday, March 6, 2011

Workflow in SharePoint 2010

SharePoint 2007 introduced workflow for collaborative applications and many organizations have benefited by moving manual processes into SharePoint workflow. The primary reason why we use workflow in software design is to manage long running processes. And while computer hardware is designed to complete processes as fast as possible, waiting for people to complete forms, make decisions, or share results of external actions always takes longer than the millisecond response time required to keep a CPU busy. SharePoint workflow is the ideal way to automate these manual processes which previously required paper or the use of complex software to record and monitor any long running activities.

Windows Workflow Foundation was first introduced in 2005 and was originally included in the Microsoft .NET Framework 3.0. Today, SharePoint 2010 offers many new improvements for workflow developers and builds on the popularity of workflow in SharePoint 2007. SharePoint workflow is recommended for long-running human workflows where we are often waiting for people to complete tasks. This article will introduce you to the key concepts and describe the new features available. First, I will talk about the new features in SharePoint 2010 for workflow developers and then I will walk through a couple of new user scenarios.

Workflow Model Design Tools Improvements

SharePoint 2010 provides more choices in tooling to better fit the needs of those creating and maintaining workflow models. The tools for workflow development include Visual Studio 2010, SharePoint Designer 2010 and Visio 2010.

It should be noted that SharePoint 2010 requires .NET Framework 3.5. The next version of the .NET Framework 4.0 is currently in development but product timelines meant SharePoint 2010 was not dependent on it.

Visual Studio 2010 includes many tooling improvements for SharePoint projects, specifically for workflow projects. The new SharePoint Solution (WSP) packaging helps deploy your workflows easily onto the SharePoint server, allowing you to add ASPX workflow initiation and association forms to Visual Studio 2010 workflow projects (Figure 1). In a walkthrough later in this article, I will show you how to add an initiation form.


Figure 1 The Visual Studio 2010 SharePoint Project Templates

SharePoint Designer 2010 also has a new graphical workflow designer for designing workflows and deploying them directly to SharePoint. It can also be used as a prototyping tool before workflows are further developed in Visual Studio 2010.

Another improvement in SharePoint Designer 2010 is that it now allows you to edit the out-of-the-box workflows that come with SharePoint. It will also use InfoPath forms if InfoPath is installed or the Data Form Web Part if InfoPath is not installed; previously, only ASPX forms were used by SharePoint Designer 2007. Since you can use InfoPath forms now, it makes it very easy to edit those forms later with InfoPath to improve them. If you have new columns specified in a workflow, they will automatically be added to the schema of the list that you associate with the workflow. These are known as association columns and can be used throughout the SharePoint Designer workflow. User profile data can be bound to properties in workflow, making it possible to get information about the SharePoint user profile. For example, you could look up the name of a direct manager for approvals.

SharePoint Designer 2010 includes a new task process designer that creates an activity in the workflow but allows flexibility in how you manage the approval process all within that one activity. This includes defining escalation processes, delegation, defining approval requirements and semantics of what happens if the underlying document is changed during the approval.

Workflow models can be created in Visio 2010 for further editing in SharePoint Designer 2010. Visio 2010 supports Business Process Modeling Notation. It also supports sub-processes and containers to break up the diagram and validation to analyze a diagram to ensure it is properly constructed.

New Workflow Models in SharePoint 2010

Site Workflows

Site workflows release workflow developers from requiring a list item or document item to run workflows against. This is useful for when the process that the workflow is implementing does not start as a result of a list item or document. This is such a common scenario that developers will create dummy lists and items in order to just to add workflows. In Visual Studio 2010, you simply pick Site Workflow when creating the workflow project item, as shown in Figure 2.


Figure 2 The Site/List Workflow Dialog Page

You can get to the activated site workflows by choosing Site Workflows from the Site Actions menu in SharePoint. It shows you new workflows you can start, any running site workflows and completed workflows. Since site workflows don’t have a list item or document to start from, they must be started manually through the SharePoint user interface or via the SharePoint API.

Reusable Declarative Workflows

In SharePoint Designer 2010, you can create reusable declarative workflows. In SharePoint 2007, a workflow model created in SharePoint Designer could only be associated with one list. These reusable workflows do not rely on a specific list but can be associated with any list. The workflow design can be reused and associated with multiple lists. Also, the Save as Template command can be used to create a WSP file containing a reusable workflow which can be moved to another SharePoint server or to Visual Studio 2010. In SharePoint Designer, this means you don’t have access to the list fields to access data since no specific list structure is connected.

Runtime and Hosting Improvements

High Privilege Workflows

In SharePoint 2007, workflows ran as the identity of the logged on user. In SharePoint 2010, with high privilege workflows you can specify an account to run a workflow as to give it additional needed permissions (see Figure 3). When creating a workflow in SharePoint Designer 2010, individual steps can be added that enable it to run as the author of the workflow instead of the logged on user.


Figure 3 The SharePoint Designer Workflow Showing a High Privilege Impersonation Step

SPTimer Location

SharePoint executes workflow instances in one of two places depending on the last action. If the last action in the workflow was waiting on a user input, the workflow continues to execute on the Web front end where the user completed that input. If the workflow is continued from a delay timer or from an event being received elsewhere, it executes within the SPTimer service. Using a new option in SharePoint Central Administration, you can now set the preferred server where the SPTimer service runs. To do this, click in the Manage Content Databases menu of the Application Management section of SharePoint Central Administration. Then click on your content database and scroll down to the setting for Preferred Server for Timer Jobs (as shown in Figure 4). You can also manually stop the SPTimer service on any servers you don’t want it to run on.


Figure 4 Setting of Preferred Server for Workflow Timer Jobs

Workflow Messaging Improvements

SharePoint 2010 adds four new workflow Event Receivers for list based workflows. The four workflow event receivers available are Starting, Started, Postponed and Completed (as shown in Figure 5). These are similar to other SharePoint event receivers and they execute code on the server in response to the event. To make development easier, Visual Studio 2010 includes event receiver project types to make using these events fairly simple. The new events attach to a specific list and fire whenever the event occurs on workflow instances associated with that list, which means that these event receivers can’t fire on site workflows. Event receivers also require a list.


Figure 5 The Event Receivers That Can Be Built in Visual Studio 2010

SharePoint 2007 made it easy to involve people in workflows, but it was difficult to send and receive messages with external systems. The recommended approach was to use task items to send a message to the external system using a Web service and have the external system update the task to return the result.

SharePoint 2010 adds support for pluggable workflow services. These will be familiar to Windows Workflow Foundation (WF) developers and are defined by creating an interface containing methods and events. This interface is connected to a pair of activities called CallExternalMethod and HandleExternalEvent. A tool comes with WF in the .NET Framework called WCA.exe to generate strongly-typed activities for sending and receiving based on the interface. These generated activities can be used in SharePoint workflow also. They do not require the interface to be set as properties and instead can be dragged directly from the toolbox to the workflow design surface.

The service code that integrates with SharePoint workflow is loaded into the GAC for access by the workflow. It needs to inherit from the SPWorkflowService base class and it needs to be referenced in the web.config.

Using these new activities, you can send asynchronous messages to an external system right from within the SharePoint workflow. I will investigate creating and using these activities later in this article in the second walkthrough.

Walkthrough of Building a Simple Workflow in Visual Studio 2010

The key thing in SharePoint workflow is to interact with people, so the simplest thing to do is assign a task to someone and wait for their response. The following shows how the new Visual Studio 2010 SharePoint tools make that task assignment really easy to build.

Step 1 - Create a new SharePoint Sequential Workflow project

Creating a new SharePoint workflow project is easy. Choose sequential workflow on the new project template selector as shown previously in Figure 1. This can also be done with the state machine workflow style.

The new workflow project wizard has four pages. The first is a page that all SharePoint tools project templates have in common, where you identify the URL of the local SharePoint site you want to use to deploy and debug your solution, as shown in Figure 6.


Figure 6 First Page of the New Workflow Project Wizard

The second is a page where you supply the name of the workflow and choose whether to associate it with a list or as a site. This page is shown earlier in Figure 2.

The third page is where you decide if Visual Studio will automatically associate the workflow for you or if you can do it manually after it has been deployed. If you choose to leave the box checked, it has selections for the list to associate with (if you previously chose a list-based workflow), the workflow history list to use and the task list to use (see Figure 7). Typically, the history and task list will not need changing.


Figure 7 Third Page of the New Workflow Project Wizard

The fourth and final page is where you select how the workflow can be started (see Figure 8). You should not unselect all three of these or your workflow will be very difficult to start. For a site workflow, you can only choose manually starting the workflow. For a list-based workflow, you can also choose to start the workflow instance when a document is created or to start when a document is changed.


Figure 8 Fourth Page of the New Workflow Project Wizard

The new blank workflow model looks like Figure 9 in the design surface in Visual Studio 2010. The workflow activated activity is derived from the HandleExternalEvent activity and provides initialization data from SharePoint to the workflow instance. I will cover more of the HandleExternalEvent activity later.


Figure 9 The Default Blank Workflow Showing the WorkflowActivated Activity

Step 2 – Add an initiation form to the workflow

An initiation form can be shown to the user when they start the workflow. It allows the workflow to gather parameters before it gets started. This can be added in Visual Studio 2010 easily by right clicking on the workflow item in Solution Explorer and choosing Add then New Item, as shown in Figure 10. Select the Workflow Initiation Form template and the new form is automatically associated with the workflow. It is an ASPX form that you edit in HTML (see Figure 11).


Figure 10 Adding a Workflow Initiation Form

One method called GetInitiationData needs editing in the code behind the ASPX file. This method only returns a string; if there are multiple values, it is recommended you serialize them into an XML fragment before returning them. Once the workflow instance is running, it is easy to get to this string just by referencing workflowProperties.InitiationData. The multiple values will need to be de-serialized from the XML fragment if they were serialized in GetInitiationData.


Figure 11 The Default Workflow ASPX Initiation Form

For the initiation form, a single text field will be added and then that field will be returned from the GetInitiationData method. The tags in the following code are added inside the first asp:Content tag, like so:

 

The GetInitiationData method already exists and just needs to have code added to return the MyID.Text property. The following code shows the updated method code:

private string GetInitiationData()
{
// TODO: Return a string that contains the initiation data that will be passed to the workflow. Typically , this is in XML format.
return MyID.Text;
}

Step 3 – Add a workflow log activity

The LogToHistoryList activity is extremely easy to use. Each workflow instance created has a history list that can display in the SharePoint user interface. The activity takes a single string parameter and adds an item to that list. It can be used for reporting the status of workflow instances to users in production. Simply drag the LogToHistoryList activity from the toolbox to the workflow design and set the description property (see Figure 12).


Figure 12 The Visual Studio Toolbox Showing SharePoint Workflow Activities

Properties in workflows are called dependency properties. These are bound at runtime to another activities dependency property, such as a field, a property or a method. This process is often called wiring up and it is what allows activities to work together in a workflow even though they don’t have specific type information for each other at compile time. Each property in the property window in Visual Studio is wired to a class field or class property in the workflow class. Fields are the simplest to create and the dialog that comes up when wiring up the workflow property allows for creating new fields, creating new properties or wiring to existing ones.


Figure 13 Dependency Property Binding to a Created Field

Figure 13 shows adding a new field with the default name. The following shows the added code to the MethodInvoking event handler for the activity to set the property to the workflow initiation data:


public String logToHistoryListActivity1_HistoryDescription1 = default(System.String);

private void logToHistoryListActivity1_MethodInvoking(object sender, EventArgs e)
{
logToHistoryListActivity1_HistoryDescription1 = workflowProperties.InitiationData;
}

You can actually set the HistoryDescription property directly in code since this property doesn’t connect between activities, but it is a good simple activity in order to learn a little about dependency properties.

Step 4 – Add a CreateTask activity

This next step is the main part of human workflow interaction known as the SharePoint task item. Create a task, assign it to a person and then wait for the person to make changes to that task. The CreateTask activity has to be dragged onto the workflow design surface and then configured with all the required properties. Figure 14 shows the CreateTask properties window just after the activity is dragged on to it.


Figure 14 The Properties Pane Showing the CreateTask Activity

The first thing needed here is a correlation token for the task. A correlation token is used for message correlation in workflow. It provides a unique identifier that enables a mapping between a task object in a specific workflow instance and the workflow runtime in SharePoint. This is used so that when SharePoint receives a message for the workflow instance it can locate the correct task within the correct workflow instance. The CorrelationToken property must be configured and it’s not recommended to use the WorkflowToken for tasks, although this isn’t prevented in the tool. Enter the new name for the correlation token as TaskToken and press enter. Then expand the (+) symbol which appears and click the drop down to the right of OwnerActivityName and choose the workflow.

The TaskId must be configured and a new GUID specified for the task id. This is done by selecting the TaskId property and clicking the […] ellipsis to bring up the property editor. Click the Bind to a new member tab, choose Create Field, and then click OK. The same must be done for the TaskProperties property by again selecting the property, clicking on the ellipsis and adding a new field. Next, double click on the new CreateTask activity on the workflow design surface to bring up the createTask1_MethodInvoking handler code and set the properties in code.

The new task must be given a title, and for good measure I will set the task description to the string I got from the initiation form. Once all those properties have been set, this is the added code:

public Guid createTask1_TaskId1 = default(System.Guid);
public SPWorkflowTaskProperties createTask1_TaskProperties1 = new
Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();

private void createTask1_MethodInvoking(object sender, EventArgs e)
{
createTask1_TaskId1 = Guid.NewGuid();
createTask1_TaskProperties1.Title = "Hello";
createTask1_TaskProperties1.Description = workflowProperties.InitiationData;
}

Step 5 – Add the OnTaskChanged and CompleteTask activities

Use the While activity to wait for multiple changes to the task until you see the changes you want. The While activity must contain another activity, such as the OnTaskChanged activity. The Listen activity can also be used to listen for multiple events at one time by adding a Listen activity with actual event receiver activities inside the Listen branches. Alternatively, you can just wait until the task is deleted with the OnTaskDeleted activity.

A common requirement is to escalate or timeout waiting for an event. This is done by using a Listen activity to listen for both the task changed message and a timer message by using a Delay activity. The first message to be received by either contained activity resumes the workflow and the other activity stops waiting. The Delay activity is sent a message when the timeout occurs.

There are lots of options described above but the simplest is to use OnTaskChanged which will wait for any change to the task item and then continue on. To configure the OnTaskChanged, you need to wire up the CorrelationToken and the TaskId properties to the same fields as the CreateTask.

Last, add a CompleteTask activity and again set the CorrelationToken and TaskId properties. As shown in Figure 15, the activities that wait for a message are green while activities that send a message are blue. This is consistent throughout SharePoint workflow.


Figure 15 The Completed Simple Task Workflow Model

Step 6 – Deploy and Test the Workflow

Now the workflow is almost complete, so press F5 and wait for the deployment. Pressing F5 will compile the workflow, package the workflow into a WSP, deploy that WSP to SharePoint, activate the SharePoint features, attach the Visual Studio 2010 debugger to SharePoint and start Internet Explorer with the SharePoint site.

Once the SharePoint site appears, choose Site Workflows from the Site Actions menu. You will see all of the site workflows and you can click on yours to start it. Once it is selected, you will see your workflow initiation form, as shown in Figure 16.


Figure 16 The Workflow Initiation Form

Other improvements can be added to this basic starter workflow model, such as using a content type to define the task you assign to a person, using email to notify the user they have been assigned a task, and even using InfoPath forms to create more complex forms for users to complete in email or online.

Walkthrough of Building a Pluggable Workflow Service

New in SharePoint 2010 is the ability to add pluggable workflow services to SharePoint. This has always been possible in Windows Workflow Foundation, but the SharePoint-hosted workflow runtime was locked down and previously didn’t allow this. Primarily pluggable workflow services are designed for sending and receiving messages out of a workflow instance. But they are also valuable for any long-running system operation such as a long-running calculation.

This is the mechanism which the workflow runtime uses to manage long-running work as a generalization of messaging. It simply executes the short-running work within activities and suspends the workflow until long-running actions have completed or a message is received to indicate a person completed a task. So while waiting for a person to respond, the workflow instance is dehydrated and does not take up CPU or RAM resources on the server it is executing on. (Dehydrated simply means the state of the workflow instance is written to a database record in such a way that it can be rehydrated back and continue on later.)

All types of long-running work are handled the same way by workflow. First, it starts the long-running work or sends a message and then it suspends the workflow until the long-running work is finished or the response is received. The correlation token is used to ensure the correct workflow instance is resumed when a response is received.

As an example, consider two common examples of long-running work. One example is a synchronous Web service request where the caller is blocked until a response is received on the channel. This is not suited to run within a workflow activity. The workflow service needs to wait for the Web service call to complete and then to send a message back to the workflow instance with the response. Another example is a CPU intensive work item that needs to run but because it may take longer than a second it needs to run outside of the workflow activity. Again, you can use a workflow service and create a thread in the workflow service to execute the CPU intensive work item. The new thread can send a message back once it is finished.

Both of these examples are implemented with the same pattern. The following walkthrough shows how to create a new SharePoint Sequential Workflow and call a pluggable workflow service that factors prime numbers to identify how many prime numbers there are under 100,000,000. This work takes too long to execute within the main line of an activity.

Step 1 – Create a Pluggable Workflow Service

To create a pluggable workflow service in Visual Studio 2010, you need to implement the service as an interface and class in your project. Figure 17 shows an example of a pluggable workflow service which can be added to your project in a new class file called MyService.cs. In the code, the interface definition is in IMyService, the interface implementation in class MyService and the MessageOut method runs most of the logic through an anonymous method delegate on a separate thread. In the separate thread, the call to RaiseEvent will send the message back to the waiting HandleExternalMessage activity.

Figure 17 The Pluggable Workflow Service

// Interface declaration
[ExternalDataExchange]
public interface IMyService
{
event EventHandler MessageIn;
void MessageOut(string msg);
}

// Arguments for event handler
[Serializable]
public class MyEventArgs : ExternalDataEventArgs
{
public MyEventArgs(Guid id) : base(id) { }
public string sAnswer;
}

// Class for state
class FactoringState
{
public SPWeb web;
public Guid instanceId;
public FactoringState(Guid instanceId, SPWeb web)
{
this.instanceId = instanceId;
this.web = web;
}
}

// Interface implementation
class MyService : Microsoft.SharePoint.Workflow.SPWorkflowExternalDataExchangeService,
IMyService
{
public event EventHandler MessageIn;
public void MessageOut(string msg)
{
ThreadPool.QueueUserWorkItem(delegate(object state)
{
FactoringState factState = state as FactoringState;
DateTime start = DateTime.Now;
int topNumber = 100000000;
BitArray numbers = new System.Collections.BitArray(topNumber, true);

for (int i = 2; i < topNumber; i++)
{ if (numbers[i]) { for (int j = i * 2; j < topNumber; j += i)
numbers[j] = false; } }
int primes = 0; for (int i = 2; i < topNumber; i++)
{ if (numbers[i]) primes++; }

string sAnswer = "Found " + primes + " in " +
Math.Round(DateTime.Now.Subtract(start).TotalSeconds, 0) + " seconds";

// Send event back through CallEventHandler
RaiseEvent(factState.web, factState.instanceId, typeof(IMyService),
"MessageIn", new object[] { sAnswer });
}, new FactoringState(WorkflowEnvironment.WorkflowInstanceId,
this.CurrentWorkflow.ParentWeb));
}

// Plumbing that routes the event handler
public override void CallEventHandler(Type eventType, string eventName,
object[] eventData, SPWorkflow workflow, string identity,
System.Workflow.Runtime.IPendingWork workHandler, object workItem)
{
var msg = new MyEventArgs(workflow.InstanceId);
msg.sAnswer = eventData[0].ToString();
msg.WorkHandler = workHandler;
msg.WorkItem = workItem;
msg.Identity = identity;
// If more than one event - you'd need to switch based on parameters
this.MessageIn(null, msg);
}
public override void CreateSubscription(MessageEventSubscription subscription)
{ throw new NotImplementedException(); }
public override void DeleteSubscription(Guid subscriptionId)
{ throw new NotImplementedException(); }

Step 2 – Update the web.config

Once the pluggable workflow service is constructed, it needs to be registered with the workflow runtime that runs in SharePoint. This is done by editing the web.config section for workflow services. The web.config editing required should be done using a feature receiver that calls SPWebApplication.WebConfigModifications.Add. The following code shows the additional configuration entry to be added:


The assembly name and public key information can be obtained using GACUTIL.EXE /l.

Step 3 – Create the workflow model

The rest of the work is easy. Add Call External Method and Handle External Event activities to the workflow and configure them to point to this service, as shown in Figure 18. The Call External Method activity will call MessageOut in the service and the Handle External Event activity will wait for the event MessageIn. This involves setting the InterfaceType and EventName properties in each of the two activities.


Figure 18 The Completed Workflow Using Pluggable Workflow Services


Figure 19 Completed Prime Calculating Pluggable Workflow Service

Step 4 – Run the workflow

When this workflow is run, it will start a separate thread in the pluggable workflow service to do the prime number calculations. After 10-15 seconds of running, a workflow history item appears as shown in Figure 19. It shows that there are 5,761,455 prime numbers under 100,000,000.

As shown, there is some code to write for pluggable workflow services and more code to write for each system you connect to. There are two options for reducing the amount of code required to communicate with external services. One is by interfacing with BizTalk Server and making use of the BizTalk adapter library for connecting other systems. The other is using the business connectivity services in SharePoint 2010 which provide a way to expose data from another system through external lists.

Easy and Successful

The new tools in Visual Studio 2010, SharePoint Designer 2010 and Visio 2010 make workflow in SharePoint 2010 much easier for developers to get started with and to be successful. The new features in the SharePoint 2010 workflow host make SharePoint both more robust and applicable to solving a wider variety of workflow problems in software.

Workflows are now easy to build and deploy to SharePoint using the standard SharePoint WSP file packaging tools that come with Visual Studio 2010. Being able to deploy to standard SharePoint sites that are common in many large companies means workflows can be adopted more easily. Workflows in SharePoint make it easy to build long-running processes that communicate with people through tasks and e-mails. Workflows in SharePoint also now make it easier to communicate with systems through pluggable workflow services, business connectivity services and with BizTalk Server.