Home General Discussion

API for new SR from template

Vladimir_BudyakVladimir_Budyak Customer IT Monkey ✭
edited August 2017 in General Discussion
Hi!
I'm trying to create SR from a template using Cireson API with PowerShell.

The code looks like this:
$SAM = "sche1223"
$Site = "http://servicedesk.cpcpipe.ru"
$CIRUser = Invoke-RestMethod -Uri "$Site/api/V3/User/GetUserList?userFilter=$SAM" -UseDefaultCredentials -ContentType 'application/json'
$SR = Invoke-RestMethod -Uri "$Site/api/V3/Projection/CreateProjectionByTemplate?id={064fd7f8-4400-6e06-ebfa-e8e0820bc885}&createdById={$($CIRUser.ID)}" -UseDefaultCredentials -ContentType 'application/json'
$jsonBody = $SR | ConvertTo-Json
$Result = Invoke-RestMethod "$Site/api/V3/Projection/Commit" -Method POST -Body $jsonBody -UseDefaultCredentials -ContentType 'application/json'
But I have an error:
Invoke-RestMethod : {"success":false,"message":"An error occurred.","exception":"Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference at CallSite.Target(Closure , CallSite , Object , String ) at Cireson.ServiceManager.Services.Implementations.ProjectionPersistenceService.d__7.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at CallSite.Target(Closure , CallSite , Object ) at CiresonWebConsole.Controllers.api.V3.ProjectionController.d__5.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Threading.Tasks.TaskHelpersExtensions.d__3`1.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ExceptionFilterResult.d__0.MoveNext()"}At line:6 char:11+ $Result = Invoke-RestMethod "$Site/api/V3/Projection/Commit" -Method POST -Body  ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Please help resolve this issue.

Best Answer

  • Vladimir_BudyakVladimir_Budyak IT Monkey ✭
    Accepted Answer
    Guys, I found the solution!
    Look at the marked code:
    $SAM = "username"
    $Site = "http://sitename"
    $CiresonUser = Invoke-RestMethod -Uri "$Site/api/V3/User/GetUserList?userFilter=$SAM" -UseDefaultCredentials -ContentType 'application/json'
    $SR = Invoke-RestMethod -Uri "$Site/api/V3/Projection/CreateProjectionByTemplate?id=064fd7f8-4400-6e06-ebfa-e8e0820bc885&createdById=$($CiresonUser.ID)" -UseDefaultCredentials -ContentType 'application/json'
    $JSONOrig = $SR | ConvertTo-Json
    
    
    Thanks to all

Answers

  • Vladimir_BudyakVladimir_Budyak Customer IT Monkey ✭
    edited August 2017
    Guys, I solved this issue.
    New code like this:
    $SAM = "username"
    $Site = "http://sitename"
    The solution is found here: https://community.cireson.com/discussion/comment/9468#Comment_9468

    But I do not understand, how I can set Affected User for new SR. Help pls.
  • Vladimir_BudyakVladimir_Budyak Customer IT Monkey ✭
    Accepted Answer
    Guys, I found the solution!
    Look at the marked code:
    $SAM = "username"
    $Site = "http://sitename"
    $CiresonUser = Invoke-RestMethod -Uri "$Site/api/V3/User/GetUserList?userFilter=$SAM" -UseDefaultCredentials -ContentType 'application/json'
    $SR = Invoke-RestMethod -Uri "$Site/api/V3/Projection/CreateProjectionByTemplate?id=064fd7f8-4400-6e06-ebfa-e8e0820bc885&createdById=$($CiresonUser.ID)" -UseDefaultCredentials -ContentType 'application/json'
    $JSONOrig = $SR | ConvertTo-Json
    
    
    Thanks to all
  • Roman_NepomniashchiiRoman_Nepomniashchii Customer IT Monkey ✭

    I know how to create SR using these API. But SR is created without relationship with Request Offering. I need to attach Request Offering to SR during creation or after creation.

  • Stefan_AllanssonStefan_Allansson Customer IT Monkey ✭

    Hi

    I have problems with the commit when I'm trying to create a new SR. I got this error message:

    "An error occurred.","exception":"System.NullReferenceException: Object reference not set to an instance of an object.

    How does your whole script or the last commit look like? Here is my script that I'm trying with:


    $SAM = "testuser"

    $Site = "https://sitename"

    $CiresonUser = Invoke-RestMethod -Uri "$Site/api/V3/User/GetUserList?userFilter=$SAM" -UseDefaultCredentials -ContentType 'application/json'

    $OriginalData = Invoke-RestMethod -Uri "$Site/api/V3/Projection/CreateProjectionByTemplate?id=66c429f7-ef43-a6d6-d6f6-60272696a951&createdById=$($CiresonUser.ID)" -UseDefaultCredentials -ContentType 'application/json'

    $JSONOriginal = $OriginalData | ConvertTo-Json

    $CurrentData = $OriginalData | ConvertTo-Json -depth 100 | ConvertFrom-Json

    $Title = "test test"

    $CurrentData.Title = $Title

    $CurrentData.LastModified = "0001-01-01T00:00:00.000Z"

    $JSONCurrent = $CurrentData | ConvertTo-Json

    $JSONCombined = '{ "formJson" : { "current" : ' + $JSONCurrent + ', "original" : ' + $JSONOriginal + ' } }'

    $CommitSite = $Site +"/api/V3/Projection/Commit"

    Invoke-RestMethod -Uri $CommitSite -Method Post -ContentType "application/json" -Body $JSONCombined -UseDefaultCredentials

  • Geoff_RossGeoff_Ross Cireson Consultant Super IT Monkey ✭✭✭✭✭

    Hi @Stefan_Allansson

    This code worked for me. Can you confirm that The first two calls brought back data as expected and one of those failing would cause the final one to throw the NullReferenceException

    Geoff

Sign In or Register to comment.