Monday 19 September 2011

Creating a Sequential WorkFlow in Visual Studio with some extra bits

In this article I am going to post how to create a simple sequential workflow with Visual Studio 2010 for Sharepoint 2010. The goal of this project is to change the Title of the documents when someone inserts a bad word. It is basically a parent control for our document library [“Shared Documents”].

What do we need?

  1. Visual Studio 2010.
  2. Sharepoint 2010.
  3. A list called Shared Documents (it comes by default).
  4. 20 minutes of your time.

Step 1
Go so Visual Studio 2010->New Project->Sharepoint->2010->Empty Sharepoint Project, call it netsourcecodeWorkflows, click ok and deploy as farm solution. Go to your project right click->Add->New Item…->Sharepoint->2010->Sequential Workflow-> call it NSCParentalWorkflow.
image

Step 2
Select List Workflow because we will be working with one list. Click Next
image

Step 3
Select Shared Documents. Click Next
image

Step 4
Leave the default values are they are. Click Finish.image

Step 5
Now go your workflow (NSCParentalWorkflow.cs) double click and open the designer. Drag and drop an activity “While” just below OnWorkFlowActivated. Double click in the While activity->Properties->Condition->select Code Condition->Condition—>Type isWorkflowFinish and return.
image

Step 6
Double Click on “onWorkflowActivated1”, that will create an event for you.
image 

Step 7
Now get one of the events from the tool bar menu, for example “onWorkflowItemChanged”, drag and drop inside the while. Single click in the event and go to Properties->Correlation Token->select workflowToken. Now do a double click to create the event. You should see something like this:
image

Step 8
It is time to add the logic behind, but before that, we should find a bunch of bad words for our parental control. Because in our “virtual” company we have English people and Spaniards, we should check both languages. Anyway this is a list of bad words, combined.

  • Fuck (en)
  • Cunt (en)
  • Mother Fucker (en)
  • Piece of Shit (en)
  • Bastard (en)
  • Joder (es)
  • Gilipollas (es)
  • Puta (es)
  • Mierda (es)
  • Cabron (es)

Our workflow will check the Title, if some one tries tries to change it with a bad word it will set the filename and it will send and email to the administrator.

Step 9
Go to NSCParentalWorkflow.cs right click –> View code. Remove all the code inside and paste this one:

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
namespace netsourcecodeWorkFlows.NSCParentalWorkFlow
{
    public sealed partial class NSCParentalWorkFlow : SequentialWorkflowActivity
    {
        public NSCParentalWorkFlow()
        {
            InitializeComponent();
        }
        public Guid workflowId = default(System.Guid);
        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();
        private bool isWorkFlowActive;
        private void OnWhile(object sender, ConditionalEventArgs e)
        {
            e.Result = isWorkFlowActive;
        }
        private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
        {
            isWorkFlowActive = true;
           
            CheckTitle();
        }
        private void onWorkflowItemChanged1_Invoked(object sender, ExternalDataEventArgs e)
        {
            CheckTitle();
        }
        private void CheckTitle()
        {
            string _sTitle = workflowProperties.Item["Title"]!=null?workflowProperties.Item["Title"].ToString():"";
            string _sFileRef = workflowProperties.Item["FileLeafRef"].ToString();
            switch (_sTitle.ToLower())
            {
                case "fuck":
                case "bastard":
                case "cunt":
                case "mother Fucker":
                case "piece of Shit":
                case "joder":
                case "gilipollas":
                case "puta":
                case "mierda":
                case "cabron":
                    {
                        workflowProperties.Item["Title"] = _sFileRef;
                        workflowProperties.Item.Update();
                        isWorkFlowActive = false;
                        SendEmailToAdministrator();
                        break;
                    }
                default: break;
            }          
        }
        public void SendEmailToAdministrator()
        {
            System.Collections.Specialized.StringDictionary _aHeaders = new System.Collections.Specialized.StringDictionary();
            _aHeaders.Add("to", "admin@netsourcecode.com");
            _aHeaders.Add("from", "admin@netsourcecode.com");
            _aHeaders.Add("subject", "This guy is inserting bad words into our DMS");
            string _sBody = SPContext.Current.Web.CurrentUser.Email;
            Microsoft.SharePoint.Utilities.SPUtility.SendEmail(workflowProperties.Web, _aHeaders, _sBody);
        }
    }
}

Step 10
Deploy the project, add an item, and try to edit a document and insert a bad word. The Title will revert itself to the file name.


Click the Icon to download the Icon.
image

4 comments:

Anonymous said...

Generally speaking, customer feedback based improvements made
everything much easier to handle. The Olympus E-3 contains a two and a half inch
LCD screen, which is just a little bit smaller
than its competitors, however, this didn't bother me. The increased features of the camera in fact, justify the price hike.

Also visit my homepage :: 5d mark iii

Anonymous said...

Another new aspect is on the front, right above the display on the
upper right corner. Samsung is indeed implementing some great tactics to come up in this tough
competitive phone markets. Are you looking for more information on
Samsung galaxy unlocked.

Visit my homepage: samsung galaxy note

Anonymous said...

The Chromebook obviously still requires these updates but they are managed in a
very different way. The introduction of the Samsung chromebook into the marketplace signifies a shift away from the traditional concept of a laptop where the
devices itself stores all of the information that you require.
To record an alarm image manually, security operator can use.

Anonymous said...

There is some great anti-virus and security software
for Android and many address the two issues in one. 0 Ice Cream Sandwich is about to be released
in Korea and Japan. _To present to three other people to
join in the support.

My site: samsung galaxy s3