CRM 2011 - How to update each field of each entity using c#

Unanswered CRM 2011 - How to update each field of each entity using c#

  • woensdag 23 mei 2012 13:21
     
      Bevat code

    Hi,

    I want to update all fields of an entity. I check if the field property "IsValidForUpdate = true" and want to insert the new values.

    Contact con = xrm.ContactSet.Where(c => c.ContactId == id).First();
    foreach (FieldItem field in listItem.FieldItems)
    {
        if (field.ToUpdate)
        {
            if (field.Value != null) 
            {
                con[field.InternalName] = field.Value;
            }
            xrm.UpdateObject(con);
        }
    }
    xrm.SaveChanges();

    I got an "Incorrect attribute value type System.String" exception. So I tried to cast the field.Value:

    RetrieveEntityResponse res = (RetrieveEntityResponse)xrm.Execute(req);
    //...
    foreach (var s in res.EntityMetadata.Attributes)
    {
        //...
        TypeAsString = s.AttributeType.ToString();
        //...
    }
    
    //update entity
    con[field.InternalName] = Convert.ChangeType(field.Value, Type.GetType(field.TypeAsString));

    But it won't work, because the Types that I get from CRM 2011 are not C# conform.

    I hope that someone can help me.

    Best regards, Markus

Alle reacties

  • donderdag 31 mei 2012 20:54
     
     

    I suppose that FieldItem is your own class, I haven't found it in xrm sdk..

    What is the type of FieldItem.Value? And how to you set it?