locked
Add a record with JSON and PowerShell to a table with relationships defined RRS feed

  • Question

  • Hey all,

    I'm sure I'm missing something obvious, but I just can't see what. Looking for a second set of eyes to catch what it is ;-)

    I have two tables with the following relationship defined:

    Using the following PowerShell commands I can create records in tables that don't have relationships defined:

    $RootURI = "http://MSLS-App.com/Data.svc"
    $credential = (Get-Credential)
    
    $webclient = new-object System.Net.WebClient
    $credCache = new-object System.Net.CredentialCache
    $creds = new-object System.Net.NetworkCredential($credential.UserName, $credential.Password)
    $credCache.Add($RootURI, "Basic", $creds)
    $webclient.Credentials = $credCache
    
    $PartnerContact = @{
        FirstName='Lead'
        LastName='Follow'
        Title='Regional Manager WA'
        Mobile=$null
        Email='LFM@example.com.au'
        PrimaryContact=$True
        PartnerContact_Partner='1'
    
    }
        $json = $PartnerContact | ConvertTo-Json
        $URI = "$RootURI/PartnersContacts/"
        $response = Invoke-RestMethod $URI -Method Post -Body $json -ContentType 'application/json' -Credential $credential

    However on the PartnerContacts table above I get the following error:

    Invoke-RestMethod : <?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code>1</m:code><m:message xml:lang="en-US">&lt;?xml version="1.0" 
    encoding="utf-16"?&gt;&lt;ExceptionInfo&gt;&lt;Message&gt;Resource not found for the segment 'PartnersContacts'.&lt;/Message&gt;&lt;/ExceptionInfo&gt;</m:message></m:error>
    At line:14 char:17
    +     $response = Invoke-RestMethod $URI -Method Post -Body $json -ContentType 'ap ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
        + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

    Anyone spot what I'm missing?

    Cheers,

    Adam

    Friday, June 12, 2015 3:31 PM

Answers

  • Just passing through...

    Should:

    $URI = "$RootURI/PartnersContacts/"

    be...

    $URI = "$RootURI/PartnerContacts/"

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Friday, June 12, 2015 4:57 PM

All replies

  • Just passing through...

    Should:

    $URI = "$RootURI/PartnersContacts/"

    be...

    $URI = "$RootURI/PartnerContacts/"

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Friday, June 12, 2015 4:57 PM
  • Very much so. Silly mistake, but much appreciate it being pointed out :)
    Friday, June 12, 2015 6:53 PM