.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Building custom conditions for SharePoint Designer

Posted By:Manning       Posted Date: January 24, 2011    Points: 75    Category: SharePoint    URL: http://www.dotnetspark.com  

Building custom conditions for SharePoint Designer can introduce power into your SharePoint Designer workflows. For instance, you could write a custom condition that checks an external data source or system before executing on a block of activities
 

This article is taken from the book SharePoint 2010 Workflows in Action. The author discusses empowering SharePoint Designer workflows by building custom conditions.

Get 40% off any version of SharePoint 2010 Workflows in Action with the checkout code dnspark40. Offer is only valid through www.manning.com.

Building custom conditions for SharePoint Designer can introduce power into your SharePoint Designer workflows. For instance, you could write a custom condition that checks an external data source or system before executing on a block of activities. The best part is that it's easy to do! All you have to do is write a .NET method that returns a Boolean and make that method available in SharePoint Designer as a condition by adding an element in an ACTIONS file.

In this article, you'll learn how to add a custom condition that you can use to check for URL availability. It's convenient that you can create a subsite from a SharePoint Designer workflow, but how will you know that the URL you've specified is available? There already may be a subsite that's using that URL. Your custom activity will take the proposed URL as a parameter and confirm that a site is not using that URL. If not, the condition will return True; otherwise, it will return False. Follow the steps in table 1 to create and publish your condition to SharePoint Designer.

Table 1 Create a custom condition and deploy it into SharePoint Designer

Action
Steps
Result
Create a new class file named SubSiteExistsCondition. 1.  Add a new class into the CustomActivities project named SubSiteExistsCondition.cs.
2.   Make the class public and add using statements for the following namespaces:
Microsoft.SharePoint.WorkflowActions;
3.  Create a public static method named SubSiteExists with the following code:

public static bool SubSiteExists(
  WorkflowContext context, string listId,
  int itemId, string url)
{
  if (context.Web.Webs.Names.Contains(url))
  return true;
  else
  return false;
}
A new class will be created with a single method that returns True or False. SharePoint Designer uses this Boolean return type to determine the outcome of the condition.

The first thing you'll see in the SubSiteExists method are three parameters you didn't think you would need. The first three parameters are required parameters. The fourth is optional, but needed for your example. You check to see if the current site has a subsite with a URL (Name) that is the same as the URL that is passed into the method in the fourth parameter. If so, return True. If no, return False. Next, all you need to do is add a few elements into our ACTIONS file and you'll be set to go!

Table 1 Create a custom condition and deploy it into SharePoint Designer, continued

Action
Steps
Result
Add a Conditions element into the ACTIONS file. 1.  Above the Actions element, add the Conditions element found in listing 1.
2.  Again, update the PublicKeyToken to reflect your token.
In addition to your custom action, the ACTIONS file will also be publishing a custom condition pointing to the SubSiteExists method.

Listing 1 Conditions Element


  
    
       #2
    
    
      
    
  


#1 Specify method name

#2 Use token instead of parameter name

The XML for Conditions is nearly identical to the XML for actions. There are only a few small differences. First, in addition to declaring the assembly and namespace, you also must declare the method name that will execute your condition (#1). Second, notice how you're not referencing the field name or what you'd expect to see, the URL (#2). Rather, you're using a _1_ token to tell Designer to pass the string into the first optional parameter. Even though URL is the fourth parameter in the method, it is still the first optional parameter. Additional parameters would follow a similar pattern to _2_, _3_, and so on.

That should be the last step. Build and deploy your project and create a new workflow in SharePoint Designer (or edit existing). You'll need to close and reopen the connection to get the latest ACTIONS file. When you finish, add the If Sub Site Exists condition and an Else branch. Inside the If branch, add a Logger to log that the URL has already been taken. In the Else branch, add a Create Sub Site action along with a Logger to log that the site was created successfully. When you're finished, your template should look something like figure 1. Go ahead and publish and test your workflow.



Figure 1 Custom conditions can also be deployed in SharePoint Designer. This condition checks to see if a URL has already been taken by another subsite.

Summary


Activities can be published in SharePoint Designer to help add more robust functionality for workflows created using that tool. This is done by publishing an ACTIONS file into the file system on each server in the farm. When SharePoint Designer connects to a site, it first downloads this ACTIONS file so it knows what actions to make available to the user building the workflow. Custom conditions also can be deployed into SharePoint Designer. A custom condition is a public static method that returns a Boolean. Add the condition into the ACTIONS file and point it to your method, and you're rolling.

Get 40% off any version of SharePoint 2010 Workflows in Action with the checkout code dnspark40. Offer is only valid through www.manning.com.


SharePoint 2010 Workflows in Action


Phil Wicklund
MEAP Release: May 2010
Softbound print: December 2010 (est.) | 400 pages
ISBN: 9781935182719

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend