Pages

Showing posts with label Sharepoint Workflowget manager name from activedorectory. Show all posts
Showing posts with label Sharepoint Workflowget manager name from activedorectory. Show all posts

Monday, June 15, 2009

Visual Studio 2005/2008 Sharepoint Workflow with Active Directory

Hello Friends

Business Case:

In Project, Requirement is like develop one workflow steps like

 1>     Create task and Assigned To is its manager which is decided by Active Directory

2>     Tasks are created like Originator à Project Manager à Director àDeputy à Superintendent. (Means we have to go to that level until whose manager does not exit.)

3>     For every user we have to create one task .If it is approved or deferred we have to send mail. If it is approved we have to send mail to person who has created task for the manager and create a new task for next level.

4>     Now if a person does not look at that task up to some duration we have to send email to person for overdue task and extend its due date for one day.

5>     If person deferred task workflow must be terminated. And email also sends for task is deferred or cancelled.

6>     Main requirement is that we have to search for manager from Active Directory.

Solution:

Develop a Sequential workflow.

Let us go into detail

1>     Create new project Project Type Sharepointà Sequential Workflow.

2>     Now Select activity that is “TaskCreated”.

3>     Then we have to add one while activity where we put one condition check that manager name is empty or not and if not then workflow must be stopped.

4>     Now there is one limitation of while activity is we add only one activity under while activity so I add one sequence activity and in that I add conditionactivitygroup there.

5>     In that I add one activity which is “CreateTask” Activity .Then on I add “OntaskCreated” Activity.

6>     Then I have to do like when task status changed from not started to Completed or Deferred we have to wait So I add one condition activity group which has condition like up to taskiscompleted == true.

7>     In group of activities I took 2 Sequence Activity. First Sequence activity is as under


























 8>     And another one is as follows.








































9>     In last once again we have to write one code activity which takes managername of current user.

10>  Workflow is completed

11>  How we can take manager name from Active Directory.

For that we have to use one directory Add reference

using System.DirectoryServices;

and also for regx you have to use

using System.Text.RegularExpressions;



        private string getManagersName(string employeesName)

        {

            string managersName = string.Empty;



            // Strip off the domain reference: eg: somecompany\\someusername. "somecompany\\" is the domain reference

            string cleanUserName = employeesName.Substring(employeesName.LastIndexOf('\\') + 1);



            // Setting the Active Directory folder to the root foler

            DirectoryEntry adFolderObject = new DirectoryEntry();



            // Creating the AD Searcher Object

            DirectorySearcher adSearcherObject = new DirectorySearcher(adFolderObject);



            // Setting the Search Scope to the AD Root tree and the subtree

            adSearcherObject.SearchScope = SearchScope.Subtree;



            // Get the UserName of the person who uploaded the document from the Document Library

            employeesName = workflowProperties.Item.GetFormattedValue("Created By");

             cleanUserName = Regex.Replace(employeesName, @"<(.|\n)*?>", string.Empty);



            // Set the Active Directory Search Filter to the, now stripped, username

            adSearcherObject.Filter = "(CN=" + cleanUserName + ")";



            // Execute the Active Directory Search

            SearchResult adObject = adSearcherObject.FindOne();



            // The Active Directory Search will return several properties for the Manager Object.

            // Store them in an array

            string[] properties;



            // Ensure that the user has a value in the Manager field

            if (adObject.Properties["Manager"].Count > 0)

            {

                // The properties are "," delimited, so we will split the properties on that character

                properties = adObject.Properties["Manager"][0].ToString().Split(',');



                // The Manager Name field starts with "CN=...", so stripping that off leaves us with the

                // Managers Name

                managersName = properties[0].Substring(3);



                // Get the Domain of the Users Active Directory Root

                string domain = adFolderObject.Name.Substring(3);



                // Convert the manager string to a SPU User Field

                managersName = workflowProperties.Web.AllUsers[domain + "\\" + managersName].ToString();

            }



            // return the managers name or an empty string, if there is no manager

            return managersName;

        }



12>  Second thing When you have list field type as “People Or Group” if you access fieldvalue and you want that thing display as “domainname\username”.You must have to use

employeesName = employeesName.Substring(employeesName.IndexOf(@"\") + 1);



13>  When you want to access some users which are in list and you want to use that name in task assigned list so you have to do one change and that is go to that edit that column and make Show Field as “Account”.







































Now I will give you concept wise description.

ConditionedActivityGroup:

The ConditionedActivityGroup activity is a CompositeActivity, meaning the ConditionedActivityGroup activity is group of other activities. The ConditionedActivityGroup activity runs and re-executes child activities based on the evaluation of their when conditions. All rules are evaluated whenever required based on their data and state change dependencies. All of activities until an until condition is true of ConditionActivityGroup.

UntilCondition = this._IsTaskCompleted == True

SequenceActivity :

The SequenceActivity is a CompositeActivity, meaning the SequenceActivity can contain other activities.

The SequenceActivity class coordinates a set of child activities in an ordered manner, one at a time. The SequenceActivity is completed when the final child activity is finished .There is one when condition is attached with Sequence Activity. If we do not specify that condition then it executes all activities under Sequence activity one at time but if we wish to executes all activities must run under some condition we can specify that thing under when condition and when it is true then and only then all activities run.

First Sequence Activity When Condition : None

Second Sequence Activity When Condition : this._IsTaskCreated == True && this._IsTaskCompleted != True

DelayActivity:

The Delay activity causes the containing branch of the workflow to wait for a specified amount of time

I need that otherwise workflow goes into infinite loop I have to tell workflow to wait for 2 minutes. Then check code condition for due date.