Friday, October 14, 2016

Add SharePoint Workflows using PowerShell



param([string] $env = $(Read-Host -prompt “Specify the environment you want to run on (D-Dev, S-Sit, P-Production) :”))

if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
   Add-PsSnapin Microsoft.SharePoint.PowerShell
}

$env = $env.ToLower()


if($env -ne ‘d’ -and $env -ne ‘s’ -and $env -ne ‘p’ -and $env -ne ‘l’) {

    Write-Host “ERROR: Invalid Input.”

}

else {

$siteAddress = $null

if($env -eq 's')
{ $siteAddress = 'http://sit:1001/' }
elseif($env -eq 'd')
{ $siteAddress = 'http://dev:1001/' }
elseif($env -eq 'p') { $siteAddress = 'http://prod:1001/' }

echo $siteAddress

# Get the SPSite object for a given SharePoint Url

$SPSite = Get-SPSite $siteAddress

    if($SPSite -ne $null)

    {
        AttachWorkflow("subsitename1" "listname1" "workflowname"

        AttachWorkflow "subsitename2" "listname2" "workflowname"
    }



}


function AttachWorkflow($webName,$listName,$workflowName)
{
    try{
   
        # Get the root web site

        $Web = $SPSite.OpenWeb($webName);

        Write-Host $Web.Name

        # Get the list to which we wanted to associate the workflow

        $SPList = $Web.Lists[$listName];

        Write-Host $SPList.Title

        # Get the Approval Workflow Template by specifying current culture info

        # $Culture = New-Object System.Globalization.CultureInfo(“en-US”);

        $Template = $Web.WorkflowTemplates.GetTemplateByName(“Approval – SharePoint 2010”,[System.Threading.Thread]::CurrentThread.CurrentCulture);



        # Try to get workflow history and task list

        $TaskList = $Web.Lists[“Tasks”];

        $HistoryList = $Web.Lists[“Workflow History”];

        # Create tasks list if it doesn't exists
        if($TaskList -eq $null)
        {
            Write-Host "Creating Tasks list"

            $listTemplate = [Microsoft.SharePoint.SPListTemplateType]::Tasks

            $Web.Lists.Add(“Tasks”, “Tasks list to maintains WF tasks”,$listTemplate);

            $TaskList = $Web.Lists["Tasks"];
           
            Write-Host "Tasks list created"
           
        }

        # Create workflow history list if it doesn’t exist by default.

        if($HistoryList -eq $null)

        {
            Write-Host "Creating Workflow History list"

           
            $wfhlistTemplate = [Microsoft.SharePoint.SPListTemplateType]::WorkflowHistory

           
            $Web.Lists.Add(“Workflow History”, “Workflow History”,$wfhlistTemplate);

           
            $HistoryList = $Web.Lists["Workflow History"];
           

            Write-Host "Workflow History list created"

        }



        # Create the Workflow association by using Workflow Template, Task List and History List

        $Association=[Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListAssociation($Template, $workflowName, $TaskList, $HistoryList);


        # Enable manual workflow start and disable autostart option
   
        $Association.AllowManual = $true;

        $Association.AutoStartChange = $false;

        $Association.AutoStartCreate = $false;



        # Provide Association Data which includes as below

        #             1. 'Approvers' SharePoint group for list of approvers.

        #             2. Parallel order Workflow.

        #             3. Specify the notification message while sending notifications to approvers. (currently nothing configured)

        #             4. Automatically reject the document if it is rejected by any participant. (Currently no)

        #             5. Update the approval status after the workflow is completed (use this workflow to control content approval). (Currently no)

        $approvalAssociationData = "

        <dfs:myFields xmlns:xsd='http://www.w3.org/2001/XMLSchema'

                        xmlns:dms='http://schemas.microsoft.com/office/2009/documentManagement/types'

                        xmlns:dfs='http://schemas.microsoft.com/office/infopath/2003/dataFormSolution'

                        xmlns:q='http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields'

                        xmlns:d='http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields'

                        xmlns:ma='http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes'

                        xmlns:pc='http://schemas.microsoft.com/office/infopath/2007/PartnerControls'

                        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>

                        <dfs:queryFields></dfs:queryFields>

                        <dfs:dataFields>

                                        <d:SharePointListItem_RW>

                                            <d:Approvers>

                                                <d:Assignment>

                                                                <d:Assignee>

                                                                    <pc:Person>

                                                                        <pc:DisplayName>Approvers</pc:DisplayName>

                                                                        <pc:AccountId>Approvers</pc:AccountId>

                                                                        <pc:AccountType>SharePointGroup</pc:AccountType>

                                                                    </pc:Person>

                                                                </d:Assignee>

                                                                <d:Stage xsi:nil='true' />

                                                                <d:AssignmentType>Parallel</d:AssignmentType>

                                                </d:Assignment>

                                            </d:Approvers>

                                        <d:ExpandGroups>true</d:ExpandGroups>

                                        <d:NotificationMessage></d:NotificationMessage>

                                        <d:DueDateforAllTasks xsi:nil='true' />

                                        <d:DurationforSerialTasks xsi:nil='true' />

                                        <d:DurationUnits>Day</d:DurationUnits><d:CC />

                                        <d:CancelonRejection>false</d:CancelonRejection>

                                        <d:CancelonChange>false</d:CancelonChange>

                                        <d:EnableContentApproval>false</d:EnableContentApproval>

                                        </d:SharePointListItem_RW>

                        </dfs:dataFields>

        </dfs:myFields>";

              

        $Association.AssociationData = $approvalAssociationData



        # Associate the approval workflow to the List

        $Workflow = $SPList.WorkflowAssociations.Add($Association);

        if($SPSite -ne $null)

        {

            Write-Host “Approval workflow has been attached successfully. SiteName:$webName ListName:: $listName.” -foregroundcolor “Green”;

            $SPList.Update();

        }

        else

        {

            Write-Host “Workflow could not be attached. SiteName:$webName ListName:: $listName.” -foregroundcolor “Yellow”;        

        }

    }
    catch
    {
        $ErrorMessage = $_.Exception.Message
        Write-Host $ErrorMessage
    }
}

Reference

No comments:

Post a Comment

Image noise comparison methods

 1. using reference image technique     - peak_signal_noise_ratio (PSNR)     - SSI 2. non-reference image technique     - BRISQUE python pac...