Download Example at
here
Step implement
Create new user in AD
Edit workflow
Demo Application
Implement
Create new user in AD
Create new user SharepointDeveloper
Create new user SharepointManager
Create new user HRManager
Right click to user SharepointDeveloper | tab Organization
set
Job Title: Sharepoint Developer
Manager: choose SharepointManager
Right click to user SharepointManager | tab Organization set
Job Title: Sharepoint Manager
Right click to user HRManager | tab Organization set
Job Title: HR Manager
Edit workflow
Download example in article (CREATE WORKFLOW WITH CUSTOM
TASK FORM IN SHAREPOINT 2010)
Open workflow then continue as follows:
Configurate properties for createTaskWithContentType2
Configure properties for onTaskChanged2 (Image 10)
Configure properties for logToHistoryListActivity2
Add reference to System.DirectoryServices.dll
Add reference to System.DirectoryServices.AccountManagement.dll
Using 2 libraries
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;
declare 2 variable
private string
userSharepointManager, userHRManager;
replace code in createTaskWithContentType1_MethodInvoking
private void
createTaskWithContentType1_MethodInvoking(object
sender, EventArgs e)
{
createTaskWithContentType1_TaskId1 = Guid.NewGuid();
createTaskWithContentType1_ContentTypeId1 = "0x0108010059177a2760644cd3b054dd3f929e0380";
userSharepointMagaer = workflowProperties.OriginatorUser.LoginName;
#region Config for only one user from AD
and recursive to another user
PrincipalContext principalContext = null;
principalContext = new PrincipalContext(ContextType.Domain,
"devsqit.com", "Administrator", "Sequent#123");
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName,
userSharepointMagaer);
UserPrincipal userPrincipalManager1 =
GetManager(principalContext, userPrincipal);
if (userPrincipal != null)
{
if (userPrincipalManager1 != null)
{
userSharepointMagaer = "DEVSQIT\\"
+ userPrincipalManager1.SamAccountName;
UserPrincipal objHRManager =
getUserByJobTitle(principalContext, "HR
Manager");
if
(objHRManager != null)
{
userHRManager = "DEVSQIT\\" +
getUserByJobTitle(principalContext, "HR
Manager").SamAccountName;
}
}
}
#region Grant read permission for users
in current web
try
{
SPWeb objWeb = SPContext.Current.Web;
if (!string.IsNullOrEmpty(userSharepointMagaer))
{
objWeb.EnsureUser(userSharepointMagaer);
}
if (!string.IsNullOrEmpty(userHRManager))
{
objWeb.EnsureUser(userHRManager);
}
objWeb.Update();
}
catch (Exception)
{
}
#endregion
#endregion
createTaskWithContentType1_TaskProperties1.AssignedTo =
userSharepointMagaer;
createTaskWithContentType1_TaskProperties1.Title = "Custom From ASPX Task List ";
}
Paste 2 method here
public UserPrincipal
getUserByJobTitle(PrincipalContext
principalContext, string jobTitle)
{
UserPrincipal u = new
UserPrincipal(principalContext);
PrincipalSearcher search = new PrincipalSearcher(u);
foreach (UserPrincipal
result in search.FindAll())
{
if (result != null)
{
// get the DirectoryEntry behind the UserPrincipal
object
DirectoryEntry dirEntryForUser =
result.GetUnderlyingObject() as DirectoryEntry;
try
{
if (dirEntryForUser != null)
{
//
check to see if we have a manager name - if so, grab it
if (dirEntryForUser.Properties["title"] != null)
{
string jobTitleOfUser = dirEntryForUser.Properties["title"][0].ToString();
if (jobTitleOfUser == jobTitle)
{
return result;
}
}
}
}
catch
{
}
}
}
return null;
}
public UserPrincipal
GetManager(PrincipalContext ctx, UserPrincipal user)
{
UserPrincipal result = null;
if (user != null)
{
// get the DirectoryEntry behind the
UserPrincipal object
DirectoryEntry dirEntryForUser =
user.GetUnderlyingObject() as DirectoryEntry;
try
{
if (dirEntryForUser != null)
{
// check to see if we have a manager name - if so, grab it
if (dirEntryForUser.Properties["manager"]
!= null)
{
string managerDN = dirEntryForUser.Properties["manager"][0].ToString();
// find the manager UserPrincipal via the managerDN
result = UserPrincipal.FindByIdentity(ctx, managerDN);
}
}
}
catch
{
result = null;
}
}
return result;
}
Replace code in createTaskWithContentType2_MethodInvoking
private void
createTaskWithContentType2_MethodInvoking(object
sender, EventArgs e)
{
createTaskWithContentType2_TaskId1 = Guid.NewGuid();
createTaskWithContentType2_ContentTypeId1 = "0x0108010059177a2760644cd3b054dd3f929e0380";
createTaskWithContentType2_TaskProperties1.AssignedTo = userHRManager;
createTaskWithContentType2_TaskProperties1.Title = "Custom From ASPX Task List ";
}
Paste code here to onTaskChanged2_Invoked method
private void
onTaskChanged2_Invoked(object sender, ExternalDataEventArgs e)
{
onTaskChanged2_AfterProperties1.PercentComplete = 1F;
}
/////////////////////////////// *** All code *** /////////////////////
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;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;
namespace
MSTechSharing_TaskFormASPX.MSTechSharing_TaskFormASPX_Workflow
{
public sealed partial class MSTechSharing_TaskFormASPX_Workflow : SequentialWorkflowActivity
{
public MSTechSharing_TaskFormASPX_Workflow()
{
InitializeComponent();
}
private string
userSharepointManager, userHRManager;
public Guid
workflowId = default(System.Guid);
public SPWorkflowActivationProperties
workflowProperties = new SPWorkflowActivationProperties();
public String
createTaskWithContentType1_ContentTypeId1 = default(System.String);
public Guid
createTaskWithContentType1_TaskId1 = default(System.Guid);
public SPWorkflowTaskProperties
createTaskWithContentType1_TaskProperties1 =
new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
public SPWorkflowTaskProperties
onTaskChanged1_AfterProperties1 =
new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
public SPWorkflowTaskProperties
onTaskChanged1_BeforeProperties1 =
new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
private void
createTaskWithContentType1_MethodInvoking(object
sender, EventArgs e)
{
createTaskWithContentType1_TaskId1 = Guid.NewGuid();
createTaskWithContentType1_ContentTypeId1 = "0x0108010059177a2760644cd3b054dd3f929e0380";
userSharepointManager = workflowProperties.OriginatorUser.LoginName;
#region Config for only one user from AD
and recursive to another user
PrincipalContext principalContext = null;
principalContext = new PrincipalContext(ContextType.Domain,
"devsqit.com", "Administrator", "Sequent#123");
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, IdentityType.SamAccountName,
userSharepointManager);
UserPrincipal userPrincipalManager1 =
GetManager(principalContext, userPrincipal);
if (userPrincipal != null)
{
if (userPrincipalManager1 != null)
{
userSharepointManager = "DEVSQIT\\"
+ userPrincipalManager1.SamAccountName;
UserPrincipal objHRManager =
getUserByJobTitle(principalContext, "HR
Manager");
if (objHRManager != null)
{
userHRManager = "DEVSQIT\\" +
getUserByJobTitle(principalContext, "HR
Manager").SamAccountName;
}
}
}
#region Grant read permission for users
in current web
try
{
SPWeb objWeb = SPContext.Current.Web;
if (!string.IsNullOrEmpty(userSharepointManager))
{
objWeb.EnsureUser(userSharepointManager);
}
if (!string.IsNullOrEmpty(userHRManager))
{
objWeb.EnsureUser(userHRManager);
}
objWeb.Update();
}
catch (Exception)
{
}
#endregion
#endregion
createTaskWithContentType1_TaskProperties1.AssignedTo =
userSharepointManager;
createTaskWithContentType1_TaskProperties1.Title = "Custom From ASPX Task List ";
}
public UserPrincipal
getUserByJobTitle(PrincipalContext
principalContext, string jobTitle)
{
UserPrincipal u = new
UserPrincipal(principalContext);
PrincipalSearcher search = new PrincipalSearcher(u);
foreach (UserPrincipal
result in search.FindAll())
{
if (result != null)
{
// get the DirectoryEntry behind the
UserPrincipal object
DirectoryEntry dirEntryForUser =
result.GetUnderlyingObject() as DirectoryEntry;
try
{
if (dirEntryForUser != null)
{
// check to see if we have a manager name - if so, grab it
if
(dirEntryForUser.Properties["title"]
!= null)
{
string jobTitleOfUser = dirEntryForUser.Properties["title"][0].ToString();
if (jobTitleOfUser == jobTitle)
{
return result;
}
}
}
}
catch
{
}
}
}
return null;
}
public UserPrincipal
GetManager(PrincipalContext ctx, UserPrincipal user)
{
UserPrincipal result = null;
if (user != null)
{
// get the DirectoryEntry behind the
UserPrincipal object
DirectoryEntry dirEntryForUser =
user.GetUnderlyingObject() as DirectoryEntry;
try
{
if
(dirEntryForUser != null)
{
// check to see if we have a manager name - if so, grab it
if (dirEntryForUser.Properties["manager"]
!= null)
{
string
managerDN = dirEntryForUser.Properties["manager"][0].ToString();
// find the manager UserPrincipal via the managerDN
result = UserPrincipal.FindByIdentity(ctx, managerDN);
}
}
}
catch
{
result = null;
}
}
return result;
}
private void
onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e)
{
onTaskChanged1_AfterProperties1.PercentComplete = 1F;
}
public String
createTaskWithContentType2_ContentTypeId1 = default(System.String);
public Guid
createTaskWithContentType2_TaskId1 = default(System.Guid);
public SPWorkflowTaskProperties
createTaskWithContentType2_TaskProperties1 = new
Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
public SPWorkflowTaskProperties
onTaskChanged2_AfterProperties1 = new
Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
public SPWorkflowTaskProperties
onTaskChanged2_BeforeProperties1 = new
Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
private void
createTaskWithContentType2_MethodInvoking(object
sender, EventArgs e)
{
createTaskWithContentType2_TaskId1 = Guid.NewGuid();
createTaskWithContentType2_ContentTypeId1 = "0x0108010059177a2760644cd3b054dd3f929e0380";
createTaskWithContentType2_TaskProperties1.AssignedTo = userHRManager;
createTaskWithContentType2_TaskProperties1.Title = "Custom From ASPX Task List ";
}
private void
onTaskChanged2_Invoked(object sender, ExternalDataEventArgs e)
{
onTaskChanged2_AfterProperties1.PercentComplete = 1F;
}
}
}
/////////////////////////////// *** End All code ***
///////////////////////////////
Demo application
Add 3 user from AD into
sharepoint site (image 20)
Login to sharepoint with user Sharepoint Developer
Add new item and workflow run, then create task for manager
of Sharepoint Developer is Sharepoint Manager
Login to sharepoint with Sharepoint Manager
Task was assigned to Sharepoint Manager
Edit and approve (OK)
Login to sharepoint with HRManager, Task was assigned to
HRManager
Edit and approve (OK)
After HRManager Approve status of workflow is Completed
Item was Completed
0 comments:
Post a Comment