Options

Powershell & NAV Web Services

eYeeYe Member Posts: 168
edited 2015-11-04 in NAV Tips & Tricks
For me, it started off just to see if it was possible and to what extent I can use NAV Web Services in PowerShell. Here is an example to read, and update NAV.

I published Page 5050 - Contact Card as a web service.

Created an export for the list of Web Services, and export it to Settings.xml to use in my PowerShell script
OBJECT XMLport 50001 Web Services
{
  OBJECT-PROPERTIES
  {
    Date=19/10/15;
    Time=07:50:33;
    Modified=Yes;
    Version List=EV;
  }
  PROPERTIES
  {
    Direction=Export;
    Format/Evaluate=C/SIDE Format/Evaluate;
    UseRequestPage=Yes;
  }
  ELEMENTS
  {
    { [{99CDBD45-35AC-4B12-917D-DE4351227FE4}];  ;Services            ;Element ;Text     }

    { [{BDC99D28-F3B9-4620-819A-65A7223A0CC3}];1 ;Service             ;Element ;Table   ;
                                                  SourceTable=Table2000000076;
                                                  SourceTableView=SORTING(Field3,Field9)
                                                                  WHERE(Field12=CONST(Yes)) }

    { [{B1AD82A5-34F9-4509-B425-B2EA8FBE81FC}];2 ;Name                ;Attribute;Field  ;
                                                  DataType=Text;
                                                  SourceField=Web Service::Service Name }

    { [{3A44F401-6EBE-406D-A6A5-32C686795EED}];2 ;Type                ;Element ;Field   ;
                                                  DataType=Option;
                                                  SourceField=Web Service::Object Type }

    { [{C21E3A9F-2545-4645-BD59-44B258B0B537}];2 ;Address             ;Element ;Text    ;
                                                  Export::OnBeforePassVariable=BEGIN
                                                                                 CASE "Web Service"."Object Type" OF
                                                                                   "Web Service"."Object Type"::Page:
                                                                                     Address := GETURL(CLIENTTYPE::SOAP,COMPANYNAME,OBJECTTYPE::Page,"Web Service"."Object ID");
                                                                                   "Web Service"."Object Type"::Codeunit:
                                                                                     Address := GETURL(CLIENTTYPE::SOAP,COMPANYNAME,OBJECTTYPE::Codeunit,"Web Service"."Object ID");
                                                                                   ELSE
                                                                                     Address := '';
                                                                                 END;
                                                                               END;
                                                                                }

  }
  EVENTS
  {
  }
  REQUESTPAGE
  {
    PROPERTIES
    {
    }
    CONTROLS
    {
    }
  }
  CODE
  {

    BEGIN
    END.
  }
}



This finds all the people for a specific Company and updates a new custom field "Registered"
Param(  
  [Parameter(Mandatory=$True)]
  [string]$CompanyNo
)

#Read settings file and connect to web service
[xml]$Settings = Get-Content 'C:\Script_Modules\Settings.xml'

$servicenode = $Settings.Services.Service | Where-Object {$_.Name -eq 'Contact'}
$service = New-WebServiceProxy -Uri $servicenode.Address -UseDefaultCredential true

$filters = @()
$ContactFilter = New-Object ('{0}.Contact_Filter' -f ($service.GetType().Namespace))
$ContactFilter.Criteria = ('{0}' -f $CompanyNo)
$ContactFilter.Field = 'Company_No'
$filters += $ContactFilter

$ContactFilter = New-Object ('{0}.Contact_Filter' -f ($service.GetType().Namespace))
$ContactFilter.Criteria = 'Person'
$ContactFilter.Field = 'Type'
$filters += $ContactFilter

#
##Alternatively, you can use this to return the entire contact list, and then filter the result set
#
#$Contacts = $service.ReadMultiple($null, '', 0) | Where-Object `
#       {$_.Company_No -eq $CompanyNo -and $_.Type -eq 'Person'}

$Contacts = $service.ReadMultiple($filters, '', 0);

$Contacts | Format-Table

foreach($contact in $contacts)
{
  $contact.Registered = $true
  $service.Update([ref] $contact)
}

$Contacts = $service.ReadMultiple($filters, '', 0);
$Contacts | Format-Table

Kind Regards,
Ewald Venter
Sign In or Register to comment.