-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport-workflow.ps1
More file actions
147 lines (111 loc) · 4.59 KB
/
import-workflow.ps1
File metadata and controls
147 lines (111 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Import required librearies for Sharepoint Client
Add-Type -Path "libraries\Microsoft.SharePoint.Client.dll"
Add-Type -Path "libraries\Microsoft.SharePoint.Client.Runtime.dll"
$Logfile = "importWorfkflow.log"
function LogWrite([string]$logstring, [string]$statusColor = "Green") {
Add-content $Logfile -value $logstring
Write-Host $logstring -ForegroundColor $statusColor
}
function New-Context([String]$WebUrl) {
$context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
$context.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$context
}
function HandleMixedModeWebApplication(){
param([Parameter(Mandatory=$true)][object]$clientContext)
Add-Type -TypeDefinition @"
using System;
using Microsoft.SharePoint.Client;
namespace Toth.SPOHelpers
{
public static class ClientContextHelper
{
public static void AddRequestHandler(ClientContext context)
{
context.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>(RequestHandler);
}
private static void RequestHandler(object sender, WebRequestEventArgs e)
{
//Add the header that tells SharePoint to use Windows authentication.
e.WebRequestExecutor.RequestHeaders.Remove("X-FORMS_BASED_AUTH_ACCEPTED");
e.WebRequestExecutor.RequestHeaders.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
}
}
}
"@ -ReferencedAssemblies "libraries\Microsoft.SharePoint.Client.dll", "libraries\Microsoft.SharePoint.Client.Runtime.dll";
[Toth.SPOHelpers.ClientContextHelper]::AddRequestHandler($clientContext);
}
Try {
$configurations = Get-Content 'configurations.json' | Out-String | ConvertFrom-Json
LogWrite "Json has been Loadded successfully."
$siteCollectionUrl= $configurations.webURL
$context = New-Context -WebUrl $siteCollectionUrl
$isNotValidCredentials = $true
$credentials = $null;
if ($configurations.MixedAuthenticationMode) {
$context.AuthenticationMode = [Microsoft.SharePoint.Client.ClientAuthenticationMode]::Default
HandleMixedModeWebApplication $context
}
if ($configurations.useStaticCredentials) {
$credentials = New-Object System.Net.NetworkCredential($configurations.username, $configurations.password, $configurations.domain)
$context.Credentials = $credentials
}else{
# Check the validity of credential and repeat the proccess
# if they are not valid
while($isNotValidCredentials -eq $true){
try{
LogWrite "Input your credentials:"
$credentials = Get-Credential
$context.Credentials = $credentials.GetNetworkCredential()
# Check if the credentials is valid
$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()
# Valid Credential
$isNotValidCredentials = $false
}catch{
$ErrorMessage = $_.Exception.Message
LogWrite "$($ErrorMessage)" Red
if ($ErrorMessage -like "*(401) Unauthorized*") {
# not valid credentials
LogWrite "Wrong Credential" Red
}else {
# Error other Credential validity
$isNotValidCredentials = $false
}
}
}
}
LogWrite "Connection to Site collection has been done successfully. - $($siteCollectionUrl)"
#Nintex Web Service URL
$WebSrvUrl=$configurations.webURL+"_vti_bin/nintexworkflow/workflow.asmx"
LogWrite "Initialize Proxy..." yellow
<#-Credential $configurations.username#>
$proxy=New-WebServiceProxy -Uri $WebSrvUrl -UseDefaultCredential
$proxy.timeout = 600000; # 10 Minutes
$proxy.URL=$WebSrvUrl
foreach ($element in $configurations.Lists) {
LogWrite "Start loading $($element.Title)"
# Load the current list
$currentList = $context.Web.Lists.GetByTitle($element.Title)
$context.Load($currentList)
$context.ExecuteQuery()
LogWrite "List was loadded successfully. List Name: $($element.Title)"
LogWrite "Start loading workflow file"
#Path of the NWF Workflow File
$WorkflowFile= $element.workflowLocation
#Get the Workflow from file
$NWFcontent = get-content $WorkflowFile
LogWrite "Workflow Loaded successfully"
LogWrite "Start Publishing Workflow"
$proxy.PublishFromNWFXml($NWFcontent, $element.Title ,$element.workflowName, $true)
LogWrite "Workflow has been imported successfully, on List Name: $($element.Title)"
}
LogWrite "Completed successfully."
Read-Host "Press Enter to exit"
$context.Dispose()
}Catch {
$ErrorMessage = $_.Exception.Message
LogWrite "Error: $($ErrorMessage)." red
Read-Host "Press Enter to exit"
}