6/12/08

Share point Work Flow and the AD pattern

Like most modern developers or solution evangelists or preachers or Code Voodoo masters, I started to mess around with Microsoft Workflows foundation. I dived deeply into the ill fated tool that was once called as Front Page and now renamed as the Share Point designer. In no time I leant Visual Studio is the best tool to develop workflows for Share Point.

The work flow are of two types
Microsoft calls them as Sequential and State Machine.
I call it,
a. Type Z - the one that could Work with mammoth effort,
b. Type Y - the one that would Never Work regardless of your best efforts.

now over to Share Point list and the AD pattern.

Step 0: Create a share point List, add 2 new columns
a) Requestor: (Person or Group) Make sure the display field is Email.
b) Manager: (Text field)
Step 1: Code a method to read the AD.
Ingredients: the LDAP url, login with appropriate permission and pwd.
Our Input : Email [This need not be email, you if you read the following code carefully you will find a way to change it]

public static string getManager(string userName)
{
string domain = "ldap://yourcompanyLdap.company.com";
string login = "[a speical admin account designated for this purpose]";
string password = "[pwd]";
string searchFor = userName;
string returnValue = "unable to find Manager";
string metaType = "Mail"; // ...

DirectoryEntry Root = new DirectoryEntry(domain, login, password, AuthenticationTypes.Delegation);
using (Root)
{
DirectorySearcher dsearcher = new DirectorySearcher(Root);
dsearcher.Filter = metaType + "=" + searchFor;
dsearcher.SearchScope = SearchScope.Subtree;
SearchResult searchresult = dsearcher.FindOne();
if (searchresult != null)
{
DirectoryEntry myDirectoryEntry = searchresult.GetDirectoryEntry();
string strManagerAlias = myDirectoryEntry.Properties["manager"].Value.ToString().Trim();
DirectoryEntry managerEntry = new DirectoryEntry(domain +"/" + strManagerAlias, login, password, AuthenticationTypes.Delegation);

//domainname\samAccount , is need for the Share point Task
returnValue = Root.Properties["name"].Value.ToString() + "\\" +managerEntry.Properties["sAMAccountName"].Value.ToString();
}
}
return returnValue;
}

Step 2:
1) Go ahead and create a new Sequential Work flow using Visual Studio
2) Drag and Drop a “OnWorkFlowActivated” Task
3) Do the Initial rituals of assigning Correlation token etc.
4) Assign a variable for Work-Flow [my code calls it “WorkflowProperties”]
5) Add an Event and code to the code Block.
//Visual Studio should add the below line automatically [Step 4/5]
public Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties WorkflowProperties = new
Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties();

The above represents the initial properties of the workflow instance when it kicks in, this contains details like workflow user, the list and item to which the workflow was tied to.

The following is the event handler code to the work flow activated activity.

public string _requestor =””;
public string _manager=””;

private void OnWorkActivated(object sender, ExternalDataEventArgs e)
{
//populate the Automatic Fields
if (WorkflowProperties.Item["Requestor"] != null)
{
_requestor = WorkflowProperties.Item["Requestor"].ToString();
_requestor = _requestor.Remove(0, _requestor.IndexOf('#') + 1);
//the above ritual is a must ,thanks to share point.
// Autopopulate the Fields.
_manager= getManager(_requestor)
WorkflowProperties.Item["Manager"] = _manager;
WorkflowProperties.Item.Update();
}
}

Step 3: compile and build and deploy it as feature. , enable the feature
Step 4, Add the workflow to the list.

9 of 10 times the above should work and my is disclaimer that this is not the only method, you are welcome to post your ideas.

No comments: