barrycarey / SnipeSharp

A c# wrapper for the Snipe IT API
25 stars 30 forks source link

How to create a new Asset with custom field information? #6

Closed mduesi closed 6 years ago

mduesi commented 6 years ago

Hi,

I want to add a new asset and fill out a custom field.

Asset asset = new Asset() { Name = name, Model = snipe.ModelManager.Get(modella), StatusLabel = snipe.StatusLabelManager.Get("New"), Serial = serial Customfield =?????? };

Thx in advance, Marcus

theazzyg commented 6 years ago

Have you figured out how to do this?

I tried to make a dictionary and add that but it doesn't add. It just adds the rest of the information and skips the CustomFields. I tried to update a specific custom field on an existing object but it did the same thing, returned as updating successfully but the new customfield data wasn't set. Any Ideas?

Updating Attempt: Asset asset = snipe.AssetManager.Get("Asset1"); asset.Serial = "678923"; asset.CustomFields["_snipeit_lastseen_2"] = "12345678"; Console.WriteLine(snipe.AssetManager.Update(asset));

Create Attempt: Asset asset1 = new Asset() { Name = "Asset1", AssetTag = "12345679", Model = snipe.ModelManager.Get(1), //Manufacturer = snipe.ManufacturerManager.Get("Lenovo"), StatusLabel = snipe.StatusLabelManager.Get("Ready to Deploy"), Location = snipe.LocationManager.Get("A - A1"), CustomFields = dictionary,

        };
        snipe.AssetManager.Create(asset1);
azmcnutt commented 6 years ago

Hi Marcus and theazzyg. I know this issue is a bit old, but if you guys are still struggling, I think I got the custom fields to work. This is the code I used:

string mac = "01:23:45:67:89:ab";
Dictionary<string, string> custom = new Dictionary<string, string>();
custom.Add("_snipeit_ethernet_mac_address_1", mac);

Asset asset = new Asset();
asset.Name = "Test2";
asset.AssetTag = "01003";
asset.Model = snipe.ModelManager.Get("Chromebook");
asset.StatusLabel = snipe.StatusLabelManager.Get("Ready to Deploy");
asset.Location = snipe.LocationManager.Get("Kitchen");
asset.CustomFields = custom;

snipe.AssetManager.Create(asset);

The field _snipeit_ethernet_mac_address_1 was taken directly from the database table field list.... I think there is a way to determine the field name pragmatically. I'm still working on it. I'm still working on refining my script for what I need it to do, but this seems to have resolved one of the issues I was having.

Hope that helps.

James

barrycarey commented 6 years ago

James, you are correct.

I didn't document it but the CustomFields attribute needs to be a dict with the database column name and the value. Not elegant but at the time I wrong this I don't think there was another way.

I'm going through the code this weekend. If I find a better approach I'll update it.