2

For adding new entity inside my custom API I use code like this:

    var tagGroup = new Dictionary<string, object>();
    tagGroup.Add("Title", "Some new tagGroup name");
    App.Data.Create("TagGroup", tagGroup, "WebApiUser");

This work fine if I just need to add new entity, but If I need to do more operations and in this opetarions I need to use Id or Guid of this newly created entity I don't know how to get it.

One way is to get this new entity by its title back, but If this title is not unique I don't know how?

I try with this code:

    dynamic obj1 = App.Data.Create("TagGroup", tagGroup, "Web Api User");
    // or
    var obj2 = App.Data.Create("TagGroup", tagGroup, "Web Api User");

But get error becouse function don't return any value.. Is there any other way how to make this work?

Is posible to define guid of new entity in advance like for IsPublished?

====================

Solution 1:

var tGuid = System.Guid.NewGuid().ToString();
tagGroup.Add("EntityGuid", tGuid);

and work OK

Jernej Pirc
  • 504
  • 1
  • 4
  • 13

2 Answers2

1

I am OK with solution to set EntityGuid in custom api controller to get new EntityId

var tGuid = System.Guid.NewGuid();
var tagGroup = new Dictionary<string, object>();
tagGroup.Add("Title", "Some new tagGroup name");
tagGroup.Add("EntityGuid", tGuid);
App.Data.Create("TagGroup", tagGroup, "WebApiUser");

var tNewObj =AsDynamic(App.Data["TagGroup"]).Where(x=>x.EntityGuid==tGuid).First();
var tNewEntityId = tNewObj.EntityId;
// do whatewer you want with this entity..

This is my final code...

Jernej Pirc
  • 504
  • 1
  • 4
  • 13
1

Update: 2sxc now has this functionality. .Create() now returns the newly created Entity...

In 2sxc, after an App.Data.Create(), is there a way to get the new EntityId (or Guid) or a Pointer to the new Entity?

Jeremy Farrance
  • 740
  • 6
  • 11