Skip to main content
Version: 24.3

Updating Assets

This topic explains how to update assets using the .NET SDK.

Overview

Updating assets through the .NET SDK involves calling the Save method of the Services object. The process is that you first have to query for the asset to update, make the update in memory using the SDK, then save the asset back to VersionOne.

Prior to updating an asset in VersionOne, you must first instantiate a V1Connector and Services object:

            V1Connector connector = V1Connector
.WithInstanceUrl("<Server Base URI>")
.WithUserAgentHeader("AppName", "1.0")
.WithAccessToken("1.rWM8lKLk+PnyFxkEWVX5Kl2u6Jk=")
.Build();

IServices services = new Services(connector);

In versions of the SDK prior to the 15.0.0.0 release, you would also have to instantiate a connector for theMetaModelobject. However, starting with the 15.0.0.0 release, that is no longer necessary. TheMetaModelobject is now available from theMetaproperty of theServicesobject.

Updating a Scalar Value Attribute

This example shows that updating a scalar attribute on an asset is accomplished by calling the SetAttribute method on an asset, specifying the attribute definition of the attribute you wish to change and the new scalar value. This example updates the Name attribute on the Story with ID 1094:

                Oid storyId = services.GetOid("Story:1094");
Query query = new Query(storyId);
IAssetType storyType = services.Meta.GetAssetType("Story");
IAttributeDefinition nameAttribute = storyType.GetAttributeDefinition("Name");
query.Selection.Add(nameAttribute);
QueryResult result = services.Retrieve(query);
Asset story = result.Assets[0];

string oldName = story.GetAttribute(nameAttribute).Value.ToString();
story.SetAttributeValue(nameAttribute, "New Name");
services.Save(story);

Console.WriteLine(story.Oid.Token);
Console.WriteLine(oldName);
Console.WriteLine(story.GetAttribute(nameAttribute).Value);

/***** OUTPUT *****
Story:1094:1446
Logon
New Name
******************/

Updating a Single-Value Relation Attribute

This example shows that updating a single-value relation is accomplished by calling the SetAttribute method on an asset, specifying the attribute definition of the attribute you wish to change and the ID for the new relation. This example updates the source of the Story with ID 1094:

                Oid storyId = services.GetOid("Story:1094");
Query query = new Query(storyId);
IAssetType storyType = services.Meta.GetAssetType("Story");
IAttributeDefinition sourceAttribute = storyType.GetAttributeDefinition("Source");
query.Selection.Add(sourceAttribute);
QueryResult result = services.Retrieve(query);
Asset story = result.Assets[0];

string oldSource = story.GetAttribute(sourceAttribute).Value.ToString();
story.SetAttributeValue(sourceAttribute, "StorySource:149");
services.Save(story);

Console.WriteLine(story.Oid.Token);
Console.WriteLine(oldSource);
Console.WriteLine(story.GetAttribute(sourceAttribute).Value);

/***** OUTPUT *****
Story:1094:1446
StorySource:148
StorySource:149
******************/

Updating a Multi-Value Relation Attribute

This example shows that updating a multi-value relation is accomplished by calling either the RemoveAttributeValue or AddAttributeValue methods on an asset, specifying the attribute definition of the attribute you wish to change and the ID of the relation you wish to add or remove. This example updates one Member and removes another Member from the Story with ID 1094:

                Oid storyId = services.GetOid("Story:1094");
Query query = new Query(storyId);
IAssetType storyType = services.Meta.GetAssetType("Story");
IAttributeDefinition ownersAttribute = storyType.GetAttributeDefinition("Owners");
query.Selection.Add(ownersAttribute);
QueryResult result = services.Retrieve(query);
Asset story = result.Assets[0];

ArrayList oldOwners = new ArrayList();
oldOwners.AddRange(story.GetAttribute(ownersAttribute).Values);
story.RemoveAttributeValue(ownersAttribute, "Member:1003");
story.AddAttributeValue(ownersAttribute, "Member:1000");
services.Save(story);

Console.WriteLine(story.Oid.Token);

foreach (Oid oid in oldOwners)
{
Console.WriteLine(oid.Token);
}

foreach (Oid oid in story.GetAttribute(ownersAttribute).Values)
{
Console.WriteLine(oid.Token);
}

/***** OUTPUT *****
Story:1094:1446
Member:1003
Member:1000
******************/