I've been looking around the API Help and don't readily see a method for changing an Item's Category. Files have the UpdateFileCategories method but I don't see something similar for Items.
That's a good question.
This workflow should work:
- Call EditItem
- On the editable Item object, update the ItemTypId value to the new Category.Id. (The Item Type concept was replaced with the Category concept in Vault 2011)
- Call UpdateAndCommitItems, passing in the updated Item object.
Yes, I'm aware that the documentation doesn't mention that ItemTypId can be edited this way. I'll make a note to fix the docs.
I have tried doing the method you suggested. However, ItemTypId is an integer and Category.Id is a Long. The value for the ID of the category I am trying to select is a larger number than what can be converted to an integer and results in a debug error.
any suggestions? am I doing something wrong?
thanks
I have no idea why itemTypId is an integer instead of a long. It's a minor bug and shouldn't cause problems unless you have over 2 billion categories.
In your code, you can just cast the long Category.Id to the integer itemTypId.
when I find the category I am looking for and do a category.id, I get 20000000027
when I query one of the items for it's ItemTypId, I get -1474836447
the numbers don't really seem to be related in our system for some reason unless I am doing something wrong.
I don't know if it makes any difference, but we are using vault 2013.
I take it back. This may be a serious bug. I keep forgetting that in a replicated environment, you can get ID values greater than 2 billion.
Can you try something for me. What happens when you run the same operation through the Vault Explorer client? When you try to change an item to that category, do you get a failure?
in the voult client, the "Change category" button is never enabled to be able to press it for items.
when in the window for editing the item, you can change the category via drop down menu. the item category changes without issue when doing this.
That surprises me. Are you sure you set the item to the category with ID 20000000027 ?
I traced through some code and UpdateAndCommitItems is how the client sets the Item category. From your code, you can try setting ItemType to the -1474836447 value and see if that works.
ok that seems to work.
my only other question is since if there might be any way to get the list of item categories with the integer typeid's for them since
ServiceManager.CategoryService.GetCategoriesByEntityClassId("ITEM", True) returns the long values that don't work.
It appears that the solution is to just cast the category ID to an integer whenever you need to change an item's category.
Thank you for all the help to this point.
I now have the issue of needing to search for items using their category as a search condition. I know that files hold their category name in a system property which is searchable. However, Items do not appear to assign this system property, which leaves me unsure of how I could use category to narrow down a search.
any thoughts or solutions to this one? or would I have to pull each item object out of the server and into local memory to check the typid/typname?
thanks.
by the way, I was unable to get the (Long) category ID to cast as an integer. it crashed the program every time I tried no matter how I tried to do it. my solution for changing the category was to create a new temporary item in the system with the category that I wanted then query that new item for its typid, then using that typeid to set the typid of the original item I wanted to edit.
after this, I delete the temp item.
it's not a very clean way of doing it, but it seems to work.
hopefully this helps the next person who runs into this problem.
I'm still hopeful someone can help me with my issue of being able to search for items by category
Casting a long to an int should not crash a program. If you are running inside Vault Explorer, it sounds like you are not catching Exceptions properly. See this article for how to avoid that.
If you are not running in Vault Explorer, a try/catch block is still a good idea. That should give you more details on what is going wrong.
the error on that was actually that the long really was a number which was larger than can be converted to a 32 bit integer, therefore "unable to cast long to integer" error occured.
I did recently figure out that if you take the long value for the category ID and convert it to a byte array (8 bytes) and convert the first 4 bytes in this array to an integer, I end up with the correct integer number for use in the ItemTypID... so that problem/workaround is solved.
do you have any thoughts on being able to narrow an item search by the category through the api?
thanks.