Search code examples
dotnetnuke2sxc

How can I add a value to Fields of Type Entity when using App.Data.Create()


When I am in external code and trying to add an entry to an existing Content Type, how can add one or more values to a field of type Entity? So lets use the FAQ v3 app as an example, here is the code with the add Categories line commented out:

    int appId = 4; // FAQ3 for me 
    var appX = ToSic.Sxc.Dnn.Factory.App(appId);
        
    var fields = new Dictionary<string, object>();
    fields.Add("InternalTitle", askQ.Name + ", " + askQ.Email);
    fields.Add("Question", askQ.Question);
    // fields.Add("Categories", ""); // what is the syntax to set 1 category = Entityid 1111?
    try
    {
      appX.Data.Create("Question", fields);
    }
      catch (Exception ex)
    {
      return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
    }

I need the new Question to start life already in a Category named "Un-answered" which is EntityId 1111.


Solution

  • This is very simple. Just add a List<int> or List<Guid> as that field with the property. Something like

    fields.Add("Category", new List<int> { 1111 });