Tuesday, January 24, 2017

Powershell Online script to start multiple workflows

# Add Wave16 references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll")

# Specify tenant admin and site URL
$SiteUrl = "https://testlsharepoint.sharepoint.com/sites/developer/"
$ListName = "TestList"
$UserName = "kunnu@TestlSharePoint.onmicrosoft.com"
$SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString

# Connect to site
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$ClientContext.Credentials = $credentials
$ClientContext.ExecuteQuery()

# Get List and List Items
$List = $ClientContext.Web.Lists.GetByTitle($ListName)
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$ClientContext.Load($List)
$ClientContext.Load($ListItems)
$ClientContext.ExecuteQuery()

# Retrieve WorkflowService related objects
$WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($ClientContext, $ClientContext.Web)
$WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService()
$WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService()
$ClientContext.Load($WorkflowServicesManager)
$ClientContext.Load($WorkflowSubscriptionService)
$ClientContext.Load($WorkflowInstanceService)
$ClientContext.ExecuteQuery()
# Get WorkflowAssociations with List
$WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
$ClientContext.Load($WorkflowAssociations)
$ClientContext.ExecuteQuery()

# Prepare Start Workflow Payload
$Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'

# Loop List Items to Start Workflow
For ($j=0; $j -lt $ListItems.Count; $j++){
    $msg = [string]::Format("Starting workflow {0}, on ListItemId {1}", $WorkflowAssociations[0].Name, $ListItems[$j].Id)
    Write-Host $msg
    #Start Workflow on List Item
    $Action = $WorkflowInstanceService.StartWorkflowOnListItem($WorkflowAssociations[0], $ListItems[$j].Id, $Dict)
    $ClientContext.ExecuteQuery()
}

PowerShell Online Script to terminate multiple workflows

Connect-SPOService -Url https://testlsharepoint-admin.sharepoint.com -credential kunnu@TestlSharePoint.onmicrosoft.com



# Add Wave16 references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"

# Specify tenant admin and site URL
$SiteUrl = "https://testlsharepoint.sharepoint.com/sites/developer/"
$ListName = "TestList"
$UserName = "kunnu@TestlSharePoint.onmicrosoft.com"
$SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString  

# Bind to site collection
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$ClientContext.Credentials = $credentials
$ClientContext.ExecuteQuery()

# Get List
$List = $ClientContext.Web.Lists.GetByTitle($ListName)

$ClientContext.Load($List)
$ClientContext.ExecuteQuery()

$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$ClientContext.Load($ListItems)
$ClientContext.ExecuteQuery()

# Create WorkflowServicesManager instance
$WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($ClientContext, $ClientContext.Web)

# Connect to WorkflowSubscriptionService
$WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService()

# Connect WorkflowInstanceService instance
$WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService()

$ClientContext.Load($WorkflowServicesManager)
$ClientContext.Load($WorkflowSubscriptionService)
$ClientContext.Load($WorkflowInstanceService)
$ClientContext.ExecuteQuery()

# Get WorkflowAssociations with List
$WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)
$ClientContext.Load($WorkflowAssociations)
$ClientContext.ExecuteQuery()

# Prepare Terminate Workflow Payload
$EmptyObject = New-Object System.Object
$Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'

# Loop Terminate Workflow
For ($j=0; $j -lt $ListItems.Count; $j++){

   $msg = [string]::Format("Killing workflows {0} on ListItemID {1}", $WorkflowAssociations[0].Name, $ListItems[$j].Id)
   Write-Host $msg

   $itemWfInstances = $WorkflowInstanceService.EnumerateInstancesForListItem($List.Id, $ListItems[$j].Id)
   $ClientContext.Load($itemWfInstances)
   $ClientContext.ExecuteQuery()
   for ($k=0;$k -lt $itemWfInstances.Count;$k++)
   {
            try {
            $WorkflowInstanceService.TerminateWorkflow($itemWfInstances[$k])
                  $msg = "Worfklow terminated on " + $ListItems[$j].Id
                  $ClientContext.ExecuteQuery()
            } catch {
                  $msg = "Error terminating workflow on " + $ListItems[$j].Id + " Details: $_"
            }

            Write-Host $msg
   }
}

Set up SharePoint Online Management Shell

Steps to setup "SharePoint Online Management Shell"

1. Install anyone from the below files based on your OS setup (32-bit or 64-bit)
a) Windows6.0-KB2506146-x64.msu
b) Windows6.0-KB2506146-x86.msu
c) Windows6.1-KB2506143-x64.msu (Worked in my case)
d) Windows6.1-KB2506143-x86.msu

2. Install 'SharePoint Online Management Shell' based on your OS (32-bit or 64-bit)
a) sharepointonlinemanagementshell_6008-1200_x64_en-us.msi (Worked in my case)
b) sharepointonlinemanagementshell_6008-1200_x86_en-us.msi

3. Install SharePoint Online Client Components SDK based on your OS (32-bit or 64-bit)
https://www.microsoft.com/en-us/download/confirmation.aspx?id=35585
a) sharepointclientcomponents_16-4002-1211_x64-en-us.msi (Worked in my case)
b) sharepointclientcomponents_16-4002-1211_x86-en-us.msi

4. Go to Start >> All Programs >> SharePoint Online Management Shell >> Right click and select ‘Run as administrator’.

5. Connect to Office 365 Admin site first using the below script. It should be run before running any script.
Connect-SPOService -Url <Admin site URL> -credential <Office 365 email address>
The email address should have access to Office 365 Admin Center for running PowerShell Online Script.
<Admin Site URL> say https://testlsharepoint-admin.sharepoint.com
<Office 365 email address> say kunnu@TestlSharePoint.onmicrosoft.com




Tuesday, January 17, 2017

Cascading of Lookup Dropdown Fields on SharePoint 2013 and Office 365

Welcome to an article on the ‘Cascading of Dropdown fields on SharePoint 2013 & Office 365’. This is a major approach when we build forms and when we have to use multiple look up fields on the form and they should be related to each other as a parent and child relationship.

So what happens is, when you select a parent, other child items are related to it. So if you select a particular parent, all the child items related to the parent will appear.

Let’s see it.


  • Create a list named “Company”.


  • Click on Add an app,
     Add an app

  • Choose the Custom List App.
  Custom List App

  • Click on it and create a ‘Company’ List.
create a Company List


  • Once the list is created, add in the names of the companies.

    list is created
  • As per the screenshot below.Company  

  • Create another list named as “Employee

    Create another list
  • Once the list is created, Create a look up column named as Company from our Company List.

    Company List
  • As you can see below, Clock on Create Column and the choose Lookup.

    Create Column
  • Select the list as Company and choose the Title field while configuring the lookup column.

    configuring the lookup
  • So here we can see our look up column has values from another list.

    values from another list
  • Fill in your required values; as for mine, I have filled in the name of the employees under the Title and have selected the company names from my look up field.
employees  
  • Now create a third list as Database.

    list as Database

    new item
  • Add the two look up columns, Company from the company list, and select title as the source of the column as per the screen shot below.

    Add the two look up column
  • Add the second look up columns; Employee from the employee list as per the screen shot below, selecting title as the field of source column.

    Employee from
  • So this will be your blank list view with all the columns,

    list view
  • When you click on New Item as you have added both the column as lookup they should display all the values but no, we are controlling their values as parent and child relationship as per the screen below.

    - If you select Company as “CTS”, the employee sees Manpreet as the value.
  • select Company as CTS

    CTS

    - Here when you select “Infy” as the Company name, Baghel is displayed as the Employee.

    Infy
  • How was it done?

    - Go to the Database list.

    - Under the list tab select “Default New Form”.

    Default New Form

    - Add a ‘Script Editor’ web part and paste the code below.

    database

    Code:
    1. <script src="https://site name/jquery.min.js"></script>  
    2. <script src="https://sitename/jquery.SPServices.min.js"></script>  
    3. <script type="text/javascript">  
    4. $(document).ready(function ()  
    5. {  
    6.     $().SPServices.SPCascadeDropdowns(  
    7.     {  
    8.         relationshipList: "Employee",  
    9.         relationshipListParentColumn: "Company",  
    10.         relationshipListChildColumn: "Title",  
    11.         parentColumn: "Company",  
    12.         childColumn: "Employee",  
    13.         debug: true  
    14.     });  
    15. });  
    16. </script>  
  • Click on OK and come back to the Database List and click on New Form.
  • Your Cascading will start functioning.
Here we saw today an amazing functionality of Cascading of lookup Dropdown fields on SharePoint 2013 & Office 365. Keep reading more articles and keep learning.