Strange behavior of ItemRollbackLifeCycleStatesEvents.GetRestrictions

Strange behavior of ItemRollbackLifeCycleStatesEvents.GetRestrictions

John204
Participant Participant
861 Views
6 Replies
Message 1 of 7

Strange behavior of ItemRollbackLifeCycleStatesEvents.GetRestrictions

John204
Participant
Participant

I am trying to restrict item rollbacks to a certain group of users on items with a specific custom item category. When I execute an item rollback with the code below, I get:

  1. Roll Back Lifecycle State Change dialog box and click YES.
  2. Get an error dialog that states, “Object reference not set to an instance of an object” and click OK.
  3. My message box displays with “Has ASME Privilege: False” and I click OK.
  4. Roll Back Lifecycle State Change Failed dialog box is displayed listing the restriction.

However, if I comment out the dim statement and the second message box statement and uncomment the first message box statement, my message box with the item ID will display and then the Roll Back Lifecycle State Change Failed dialog box but no “Object reference” error message. It seems like I can’t call any custom sub or function inside the ItemRollbackCheck Sub without getting the error.  I need to get a Boolean value in if the current user is part of an approved group and I need a way to get the current item category.  I can get the item ID easy enough within the sub but can’t do anything with it.

 

Public Sub OnLoad() Implements Autodesk.Connectivity.WebServices.IWebServiceExtension.OnLoad

           AddHandler ItemService.ItemRollbackLifeCycleStatesEvents.GetRestrictions, AddressOf ItemRollbackCheck

          

       End Sub

 

Private Sub ItemRollbackCheck(ByVal sender As Object, ByVal e As ItemRollbackLifeCycleStateCommandEventArgs)

           Try

 

               Dim approved As Boolean = HasASMEPrivilege(approvedgroup)

               'MessageBox.Show("ItemId: " & e.ItemIterationId.ToString) 'this works

               MessageBox.Show("Has ASME Privilege: " & approved) ' errors first, then displays this message box

              Dim restrict As ExtensionRestriction = New ExtensionRestriction(e.ItemIterationId.ToString, "No Permission")

               e.AddRestriction(restrict)

           Catch ex As Exception

               'MessageBox.Show(ex.Message)

           End Try

 

 

       End Sub

 

 

0 Likes
Accepted solutions (1)
862 Views
6 Replies
Replies (6)
Message 2 of 7

psaarloos
Collaborator
Collaborator

Hi,

 

You should be able to call any function or sub from inside the event handler. Are you sure 'approvedgroup' is declared and not empty? This could be causing the 'object not set ..' error.

 

You can get to the item cateogry by using the CategoryService. You need to add the code below to get a reference to the WebServiceManager.

 

Dim connection As IWebService = DirectCast(sender, IWebService)
Dim item As Item = connection.WebServiceManager.ItemService.GetItemsByIds(New Long() {e.ItemIterationId}).FirstOrDefault()
Dim itemCat As ItemCat = item.Cat
Dim itemCatName As String = itemCat.CatName

Hope it helps.

 

Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
0 Likes
Message 3 of 7

John204
Participant
Participant

Hi Pim,

Thank you for the response. I have the following lines at the top of my class:

 

Public Const approvedgroup As String = "ASME Engineering"

Private _connection As Autodesk.DataManagement.Client.Framework.Vault.Currency.Connections.Connection

 

Therefore, I do have approvedgroup defined but even when I hardcode “ASME Engineering” into the function argument, I still get the error. If I comment everything out, paste in your code for getting the item category, and then display it in a message box, everything is fine.  This leads me to believe there is an issue with the HasASMEPrivilege function and how I have it written.

 

Private Function HasASMEPrivilege(ByVal appgrp As String) As Boolean

           Dim approvedflag As Boolean = False

           Try

               'Pass in a group name and get back a group object for GetGroupByName. UserRead if logged in user

               'is a member of the group. Otherwise AdminUserRead is required or a 303 error

               '(permission denied) is thrown. This means that only members of approvedgroup

               ' or a member of the Administrators group will have ASME privileges.

               Dim objGroup = _connection.WebServiceManager.AdminService.GetGroupByName(approvedgroup)

 

             If objGroup Is Nothing Then

                   approvedflag = False

               Else

                   approvedflag = True

               End If

 

           Catch ex As Exception

               If ex.Message.ToString = "303" Then

                   approvedflag = False

               Else

                   MessageBox.Show(ex.Message)

               End If

           End Try

 

           Return approvedflag

       End Function

 

There may be a better way of doing this but I needed to determine the group of the currently logged in user. I did not want to open a new connection with admin privileges which I think will consume another license so I used GetGroupByName which will allow read if the user is a member of the group but throw an error if he isn’t.  I trap the error if they are not a member of the group and return False.  I think the problem may be with my dim objGroup statement.

 

Thank you for your assistance.

0 Likes
Message 4 of 7

psaarloos
Collaborator
Collaborator
Accepted solution

Hi,

 

Basically looks all good. Opening another connection with a different user doesn't consume a second license anymore with the new licensing mechanism of Vault 2017, but still testing with a try..catch is the most easiest. I've re-written your function a bit. Can you give it a go?

 

Call it with 'Dim approved as Boolean = HasASMEPrivilege("ASME Engineering")

 

Private Function HasASMEPrivilege(ByVal appgrp As String) As Boolean

            Dim result As Boolean = False

            Try

                'Pass in a group name and get back a group object for GetGroupByName. UserRead if logged in user
                'is a member of the group. Otherwise AdminUserRead is required or a 303 error
                '(permission denied) is thrown. This means that only members of approvedgroup
                ' or a member of the Administrators group will have ASME privileges.

                Dim objGroup = _connection.WebServiceManager.AdminService.GetGroupByName(appgrp)
                result = True

            Catch ex As Exception

            End Try

            Return result

        End Function
Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
0 Likes
Message 5 of 7

John204
Participant
Participant

Hi Pim,

 

Your revised function works great, no errors. A much better job than my clumsy attempt.

 

Thank you for the information on the licensing. We do have Vault 2016, would that version consume another license if I opened another connection with admin privileges?

 

Thanks.

0 Likes
Message 6 of 7

psaarloos
Collaborator
Collaborator

Hi,

 

Glad to hear it works! Vault 2016 will indeed consume two licenses if you make multiple (writeable) connection with different users. Big improvement of Vault 2017!

Regards,
Pim Saarloos
Product Manager
If my post answers your question, please click the "Accept as Solution" button. Kudos are much appreciated!
0 Likes
Message 7 of 7

John204
Participant
Participant

Hi Pim,

 

Thank you for that info and for all your help.

0 Likes