Tuesday, December 6, 2016

Find things in InfoPath form templates in a web application

This assessment and clean up process to get things ready for SharePoint online migration is pretty interesting.   So there are some custom web services created for InfoPath forms so that it can get data and functions (lots of date functions) that is not available out of the box.   So need to find all the published templates with the calls to these web services.

So after thinking about what can be done with code and reviewing the InfoPath interop classes leading nowhere.   I found an interesting article https://sharepintblog.com/2011/06/07/updating-infopath-form-templates-and-data-connections-with-powershell/

so with the code that they provided, i modified it to spit out all templates with the data connections to these web services.  

Hope this helps since it is saving a lot of time and effort to track these down and find a workaround.

--updated with the code to include list templates not just document libraries. 

Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue

## the web application to be searched for form libraries
$url = 'https://yoursharepointurl'

## temp working directory
$dir = 'D:\ExtractedForms'


$findstring = 'findthis String'

$exportFile = $false

foreach ($web in (Get-SPWebApplication $url | Get-SPSite -Limit All| Get-SPWeb -Limit All))
{
    foreach($list in $web.Lists)
    { 
        if ($list.DocumentTemplateUrl -ne $null -or $list.ContentTypes[0].ResourceFolder.Properties["_ipfs_infopathenabled"])
        {
            if ($list.ContentTypes[0].ResourceFolder.Properties["_ipfs_infopathenabled"])
            {
                #--------------- get the templateurl for the list template....  
                foreach($folder in $list.RootFolder.SubFolders)
                {
                    foreach($file in $folder.Files)
                    {
                        if($file.Name -eq 'template.xsn')
                        {
                            $listUrl = $web.Url + '/' + $list.RootFolder + '/' + $folder.Name
                            $templateUrl = $web.Url + '/' + $list.RootFolder + '/' + $folder.Name +'/template.xsn'

                            $exportFile = $true 
                        }
                    }               
                }             
                                          
            }
            elseif ($list.DocumentTemplateUrl.ToString().ToLower().EndsWith(".xsn"))
            {
                $listUrl = $web.Url + '/' + $list.rootFolder
                $templateUrl = $web.Url + '/' + $list.DocumentTemplateUrl

                $exportFile = $true 
            }

            if($exportFile)
            {
                ## download the form template
                $filename = $file.Name
                $fileID = $file.UniqueId.Tostring()
                $localfile = $fileID + "\" + $filename

                $file = $web.GetFile($templateUrl)
                $bytes = $file.OpenBinary();

                # Download the file to the path
                $localfile = $dir + "\" + $localfile
                New-Item "$dir\$fileID" -type directory -force | Out-Null
                New-Item "$dir\$fileID\Extracted" -type directory -force | Out-Null
                [System.IO.FileStream] $fs = New-Object System.IO.FileStream($localfile, "OpenOrCreate")
                $fs.Write($bytes, 0 , $bytes.Length)
                $fs.Close()
            
                ## crack open the form template
                EXPAND "$localfile" -F:* "$dir\$fileID\Extracted" | Out-Null
                
                ## get file content and check for string
                $extractedFiles = Get-ChildItem "$dir\$fileID\Extracted" *.x*
                foreach ($extractedFile in $extractedFiles)
                {
                    $efn = $extractedFile.Name
                    $f = [System.IO.File]::Open("$dir\$fileID\Extracted\$efn" ,"open","read","Read")
                    $sr = New-Object System.IO.StreamReader $f
                    $content = $sr.ReadToEnd()
                    $sr.close()
                    $f.close()                     
                     
                    if ($content.Contains($findstring))
                    {
                        Write-Host $templateUrl " :  " $extractedFile.Name -foreground green
                    }
                }               
            }
            $exportFile = $false
        }
    }
    $web.Dispose()

}

Monday, December 5, 2016

Find custom action or activity in a designer created 2010 workflow

While cleaning up my SharePoint farm for the cloud migration, i ran into an interesting problem.   If there are custom activity or action created for your designer workflow, how do you track down which ones uses those custom action or activity.

Here is a simple code to go through a site collection that returns all 2010 workflows that is using your custom action/activity that you created with code.

Add-PSSnapin *sharepoint* -ErrorAction SilentlyContinue

$encode = New-Object System.Text.ASCIIEncoding

$site = get-spsite "type in url"

foreach($w in $site.allwebs)
{
    foreach($wf in $w.Lists["workflows"].items)
    {
        if($wf.Name -like "*.xoml")
        {
            $file   = $wf.File
            $data   = $file.OpenBinary()
          
            $test   = $encode.GetString($data)
            if($test.Contains("text to find.  usually the namespace of the assembly of custom dll") -or $test.Contains("more text"))
            {
                  write-host $w.url $wf.Name
            }
        }
    }