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:
<div>$SAM = "sche1223"</div><div>
$Site = "http://servicedesk.cpcpipe.ru"</div><div>
$CIRUser = Invoke-RestMethod -Uri "$Site/api/V3/User/GetUserList?userFilter=$SAM" -UseDefaultCredentials -ContentType 'application/json'</div><div>
$SR = Invoke-RestMethod -Uri "$Site/api/V3/Projection/CreateProjectionByTemplate?id={064fd7f8-4400-6e06-ebfa-e8e0820bc885}&createdById={$($CIRUser.ID)}" -UseDefaultCredentials -ContentType 'application/json'</div><div>
$jsonBody = $SR | ConvertTo-Json</div><div>
$Result = Invoke-RestMethod "$Site/api/V3/Projection/Commit" -Method POST -Body $jsonBody -UseDefaultCredentials -ContentType 'application/json'</div>
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 Customer IT Monkey ✭
    Answer ✓
    Guys, I found the solution!
    Look at the marked code:
    <div>$SAM = "username"</div>
    <div>$Site = "http://sitename"</div>
    <div>$CiresonUser = Invoke-RestMethod -Uri "$Site/api/V3/User/GetUserList?userFilter=$SAM" -UseDefaultCredentials -ContentType 'application/json'</div>
    <div>$SR = Invoke-RestMethod -Uri "$Site/api/V3/Projection/CreateProjectionByTemplate?id=064fd7f8-4400-6e06-ebfa-e8e0820bc885&createdById=$($CiresonUser.ID)" -UseDefaultCredentials -ContentType 'application/json'</div>
    <div>$JSONOrig = $SR | ConvertTo-Json</div>
    
    <div><b><i>$SR | Add-Member -Name RequestedWorkItem -Value $SR.CreatedWorkItem -MemberType NoteProperty</i></b></div>
    
    <div>$JSONCurrent = $SR | ConvertTo-Json</div>
    <div>$JSONCombined = '{ "formJson" : { "current" : ' + $JSONCurrent + ', "original" : ' + $JSONOrig + ' } }'</div>
    <div>$postData = [System.Text.Encoding]::UTF8.GetBytes($JSONCombined)</div>
    <div>$Result = Invoke-RestMethod "$Site/api/V3/Projection/Commit" -Method POST -Body $postData -UseDefaultCredentials -ContentType 'application/json; charset=utf-8'</div>
    Thanks to all

Answers

  • Vladimir_BudyakVladimir_Budyak Customer IT Monkey ✭
    edited August 2017
    Guys, I solved this issue.
    New code like this:
    <div>$SAM = "username"</div><div>
    $Site = "http://sitename"</div><div><br></div><div>$CIRUser = Invoke-RestMethod -Uri "$Site/api/V3/User/GetUserList?userFilter=$SAM" -UseDefaultCredentials -ContentType 'application/json'</div><div>
    $SR = Invoke-RestMethod -Uri "$Site/api/V3/Projection/CreateProjectionByTemplate?id=064fd7f8-4400-6e06-ebfa-e8e0820bc885&createdById=$($CIRUser.ID)" -UseDefaultCredentials -ContentType 'application/json'</div><div>
    $JSONOrig = $SR | ConvertTo-Json</div><div><br></div><div>$SR.RequiredBy = $SR.CreatedWorkItem</div><div><br></div><div>$JSONCurrent = $SR | ConvertTo-Json</div><div><b>
    $JSONCombined = '{ "formJson" : { "current" : ' + $JSONCurrent + ', "original" : ' + $JSONOrig + ' } }'</b></div><div>
    $postData = [System.Text.Encoding]::UTF8.GetBytes($JSONCombined)</div><div><br></div><div>$Result = Invoke-RestMethod "$Site/api/V3/Projection/Commit" -Method POST -Body $postData -UseDefaultCredentials -ContentType 'application/json; charset=utf-8'</div>
    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 ✭
    Answer ✓
    Guys, I found the solution!
    Look at the marked code:
    <div>$SAM = "username"</div>
    <div>$Site = "http://sitename"</div>
    <div>$CiresonUser = Invoke-RestMethod -Uri "$Site/api/V3/User/GetUserList?userFilter=$SAM" -UseDefaultCredentials -ContentType 'application/json'</div>
    <div>$SR = Invoke-RestMethod -Uri "$Site/api/V3/Projection/CreateProjectionByTemplate?id=064fd7f8-4400-6e06-ebfa-e8e0820bc885&createdById=$($CiresonUser.ID)" -UseDefaultCredentials -ContentType 'application/json'</div>
    <div>$JSONOrig = $SR | ConvertTo-Json</div>
    
    <div><b><i>$SR | Add-Member -Name RequestedWorkItem -Value $SR.CreatedWorkItem -MemberType NoteProperty</i></b></div>
    
    <div>$JSONCurrent = $SR | ConvertTo-Json</div>
    <div>$JSONCombined = '{ "formJson" : { "current" : ' + $JSONCurrent + ', "original" : ' + $JSONOrig + ' } }'</div>
    <div>$postData = [System.Text.Encoding]::UTF8.GetBytes($JSONCombined)</div>
    <div>$Result = Invoke-RestMethod "$Site/api/V3/Projection/Commit" -Method POST -Body $postData -UseDefaultCredentials -ContentType 'application/json; charset=utf-8'</div>
    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 O.G.

    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.