ĐÀO TẠO DOANH NGHIỆP : SỞ KHOA HỌC CÔNG NGHỆ TỈNH ĐỒNG NAI

ENTERPRISE TRAINING: DONG NAI DEPARTMENT OF SCIENCE AND TECHNOLOGY.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

Sunday, May 20, 2012

CREATE WORKFLOW WITH CUSTOM TASK FORM IN SHAREPOINT 2010 INTEGRATE TO USER FROM ACTIVE DIRECTORY


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

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

Saturday, May 19, 2012

Populating InfoPath Drop Down List Box with SharePoint Fields Choice

Populate InfoPath drop-down programmatically from Sharepoint Field Choice

  • Article 1: How to code C-Sharp on infopath form 2010
  • Article 2: How to set the value of a Field in Infopath
  • Article 3: How to set value of a field from sharepoint list
  • Article 4: Populating InfoPath Drop Down List Box with SharePoint List Lookup Data 
  • Article 5Populating InfoPath Drop Down List Box with SharePoint Fields Choice
  • Article 6: Deploy an InfoPath 2010 Form with Managed Code to a Browser Enabled Sharepoint Document Library

Download Infopath Form at here

Designing  and coding infopath

Open Infopath | Design user interface follows as

Go to File => Form Options => Programming
Form template code language: C#
Go to File => Form Options => Security and Trust
Configure follows as:
Go to Tab Developer | click to Loading Event ribbon
Right click to myFields | Add
Name: GroupStatus
Type: Group
Check to Repeating
Right click to GroupStatus | Add
Name: Displayname
Continue Right click to GroupStatus | Add then input Name: Value
Right click to DropdownList “Country” | Drop-Down List Box Properties…
At List box choices------------------------------
Check to radio button “Get choice from fields in this from”, then click to Entries, select value follows as
Add reference to Microsoft.sharepoint.dll
Using  using Microsoft.SharePoint;
Add method AddStatus (); to FormEvents_Loading
Paste this method  to below FormEvents_Loading method public void AddStatus ()
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using Microsoft.SharePoint;

namespace SetValueToDropdownListInfoPathFromChoiceField
{
    public partial class FormCode
    {
        // NOTE: The following procedure is required by Microsoft InfoPath.
        // It can be modified using Microsoft InfoPath.
        public void InternalStartup()
        {
            EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
        }

        public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
            AddStatus();
        }

        public void AddStatus()
        {
            try
            {
                SPSite site = new SPSite("http://win-67038mbkel7");
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists["Tasks"];
                SPFieldMultiChoice spFieldMultiChoice = (SPFieldMultiChoice)list.Fields["Status"];
                XPathNavigator nav = this.CreateNavigator().SelectSingleNode("/my:myFields/my:GroupStatus",
                    this.NamespaceManager);
                for (int item = 0; item < spFieldMultiChoice.Choices.Count; item++)
                {
                    XPathNavigator newNode = null;
                    newNode = nav.Clone();
                    newNode.SelectSingleNode("/my:myFields/my:GroupStatus/my:Displayname",
                        this.NamespaceManager).
                        SetValue(spFieldMultiChoice.Choices[item].ToString());
                    newNode.SelectSingleNode("/my:myFields/my:GroupStatus/my:Value",
                        this.NamespaceManager).
                        SetValue(spFieldMultiChoice.Choices[item].ToString());
                    nav.InsertAfter(newNode);
                    newNode = null;
                }
                nav.DeleteSelf();
                nav = null;
            }
            catch
            {

            }
        }
    }
}


Build project then click F5, result as below

Deploy an InfoPath 2010 Form with Managed Code to a Browser Enabled Sharepoint Document Library

Populating InfoPath Drop Down List Box with SharePoint List Lookup Data

Populate InfoPath drop-down programmatically from Sharepoint List Lookup data



  • Article 1: How to code C-Sharp on infopath form 2010
  • Article 2: How to set the value of a Field in Infopath
  • Article 3: How to set value of a field from sharepoint list
  • Article 4: Populating InfoPath Drop Down List Box with SharePoint List Lookup Data 
  • Article 5Populating InfoPath Drop Down List Box with SharePoint Fields Choice
  • Article 6: Deploy an InfoPath 2010 Form with Managed Code to a Browser Enabled Sharepoint Document Library

Download infopath project at here

Designing  and coding infopath

Open Infopath | Design user interface follows as
Go to File => Form Options => Programming
Form template code language: C#
Go to File => Form Options => Security and Trust
Configure follows as:
Go to Tab Developer | click to Loading Event ribbon
Right click to myFields | Add
Name: Countries
Type: Group
Check to Repeating
Right click to Countries | Add
Name: Displayname
Continue Right click to Countries | Add then input Name: Value
Right click to DropdownList “Country” | Drop-Down List Box Properties…
At List box choices------------------------------
Check to radio button “Get choice from fields in this from”, then click to Entries follows as
Select value follows as
Open your  site collection | Create new list Countries then add new some Item
Add reference to Microsoft.sharepoint.dll
Using  using Microsoft.SharePoint;
Add method AddCountries(); to FormEvents_Loading

Paste this method  to below FormEvents_Loading method public void AddCountries()
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using Microsoft.SharePoint;

namespace PopulateDropdownListFromCode
{
    public partial class FormCode
    {       
        // NOTE: The following procedure is required by Microsoft InfoPath.
        // It can be modified using Microsoft InfoPath.
        public void InternalStartup()
        {
            EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
        }

        public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
            //Populate country drop down
            AddCountries();
        }

        public void AddCountries()
        {
            try
            {
                SPSite site = new SPSite("http://win-67038mbkel7");
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists["Countries"];
                SPListItemCollection listitems = list.Items;
                XPathNavigator nav = this.CreateNavigator().
                    SelectSingleNode("/my:myFields/my:Countries", this.NamespaceManager);// Note: "/my:myFields/my:Countries" => Group Countries
                foreach (SPListItem li in listitems)
                {
                    XPathNavigator newNode = null;
                    newNode = nav.Clone();
                    newNode.SelectSingleNode("/my:myFields/my:Countries/my:Displayname",
                        this.NamespaceManager).SetValue(li["Title"].ToString());
                    newNode.SelectSingleNode("/my:myFields/my:Countries/my:Value",
                        this.NamespaceManager).SetValue(li["Title"].ToString());
                    nav.InsertAfter(newNode);
                    newNode = null;
                }
                nav.DeleteSelf();
                nav = null;
            }
            catch
            {
            }
        }
    }
}

Build project then click F5, result as below


Deploy an InfoPath 2010 Form with Managed Code to a Browser Enabled Sharepoint Document Library