Reputation: 205
I have the following Json defining a specific configuration which has to be stored in custom CRM entities:
{
"useraccountid": "U12345",
"profiles": [
{
"applicationrole": "RelationshipManager",
"maindatascopetypecd": 858000001,
"organisationalunitno": "10000000",
"ishierachical": 1
},
{
"applicationrole": "CountrySpecialist",
"maindatascopetypecd": 858000002,
"attributetypecd": 858000000,
"attributevalue": "SY",
"isreadonly": 0
}
]
}
Each user can have multiple user provisioning profiles. this data finally needs to be written into custom CRM entities. The "useraccountid" is a lookup to a systemuser (entityreference).
What I already have is e deserializer like this:
public static T JsonDeserialize<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(ms);
return obj;
}
And classes like this for the parent class:
[DataContract]
class CrmUserProvisioning
{
[DataMember]
public String clm_useraccountid
{
get; set;
}
// will be set on runtime
public DateTime clm_createdon
{
get; set;
}
[DataMember]
public List<CrmProfile> clm_profiles { get; set; } = new List<CrmProfile>();
}
and for the profile(s)
[DataContract]
public class CrmProfile
{
[DataMember]
public Guid clm_userprovisioningid
{
get; set;
}
[DataMember]
public string clm_applicationrole
{
get; set;
}
[DataMember]
public int clm_maindatascopetypecd
{
get; set;
}
[DataMember]
public string clm_organisationalunitno
{
get; set;
}
[DataMember]
public bool clm_ishierachical
{
get; set;
}
[DataMember]
public int clm_applyingtypecd
{
get; set;
}
[DataMember]
public int clm_globalopenaccesscd
{
get; set;
}
[DataMember]
public int clm_attributetypecd
{
get; set;
}
[DataMember]
public string clm_attributevalue
{
get; set;
}
[DataMember]
public bool clm_isreadonly
{
get; set;
}
}
missing fields in the config for the profile will not be deserialized. I'm stuck with the fact on how deserialize this config regarding the fact that the config can contain many profile classes but only one parent class (UserProvisioning).
Could anybody put me in the right direction? Any help is really appreciated.
kind regards
After having deserialized the Jsion into object(s) I need to store now the objects into the corresponding Microsoft Dynamics CRM entites. The entities and the attributes are named like the object- and the property-names.
I already have the organisation service, etc. I only need to know how to map the objects to a regular crm create or update request.
If somebody could help me out with this, it would be very appreciated.
Upvotes: 0
Views: 1912
Reputation: 776
Use DataContract and DataMember
[DataContract]
class UserProvisioning
{
[DataMember]
public String useraccountid
{
get { return this.useraccountid; }
set { this.useraccountid = value; }
}
// Will be set on runtime
public DateTime createdon
{
get { return this.createdon; }
set { this.createdon = value; }
}
// Must declare this for the child list of Profile
[DataMember]
public List<Profile> profiles {get;set;}=new List<Profile>();
}
In CrmProfile
also set DataContract
for the class and DataMember
for the properties.
Check the docs:
DataContract
DataMember. Only the properties with DataMember
will serialize.
You can also set required properties or set not to serialize default values.
Upvotes: 1