How we can Insert and Update Lookup fields using CDS sdk in C#.
var data = entity.CreateEntity();
data.SetValue("Name", "abc"); // Text field
data.SetValue("Direct", ?? ); // Lookup field
Hi @NehaSingh,
Lookup fields in Common Data Service under PowerApps is defined as a record.
The formula to update this Lookup field in PowerApps would require the fields defined in the parent entity DefaultLookup group.
For example, here we have custom entity testing1 and testing2.
Testing2 has a lookup field configured with the entity testing1.
To update the lookup field in PowerApps, the formula should be:
Patch( Testing2, Gallery.selected, { Testing_: {PrimaryId:"",Text:""} } )
I am not sure how the Common Data Service defines the record in the SetValue method.
Hope staff @CWesener could share more information about this.
Or you may contact the following Email directly:
"
This feature is preview. If you are interested in participating in the preview program, contact us at cdspreviewprogs_at_microsoft.com.
"
Regards,
Michael
I understand this question is about how to generally perform the operations mentioned using the C# SDK. As it happens, this is quite easy, once you understand the basics.
In the SDK you treat entities like plain old C# objects (aka POCOs), or as generic entities that are not strongly defined. In the sample that was provided in the question I get the feeling that the user is using some custom entities for which he has not provided any type safe C# representation.
In the SDK all CRUD operations occur in batches. The code on the client tier simply queues up the work (in a socalled executor), and then sends it off to the server for execution using the asynchronous ExecuteAsync call on the executor. In you example this would look like:
Snippet
public static async Task GenericEntitySetUsageAsync(Client client) { RelationalEntitySet genericEntitySet = client.GetRelationalEntitySet( new EntitySetReference("Microsoft.CommonDataService.CommonEntities", "ProductCategory", Version.Create("1.0.0"))); // Create a few product categories var genericEntitySurface = genericEntitySet.CreateEntity(); genericEntitySurface.SetValue<string>("Name", "Surface"); // The compiler can infer the type, so the generic argument describing // the type of the value is not strictly required. This is a shorthand. genericEntitySurface.SetValue("Description", "Surface produce line"); var genericEntityPhone = genericEntitySet.CreateEntity(); genericEntityPhone.SetValue("Name", "Phone"); genericEntityPhone.SetValue("Description", "Phone produce line"); var insertExecutor = client.CreateRelationalBatchExecuter( RelationalBatchExecutionMode.Transactional); await insertExecutor .Insert(genericEntitySurface) .Insert(genericEntityPhone) .ExecuteAsync(); // Create a query using the indexing operators var query = genericEntitySet.CreateQueryBuilder() .Where(pc => pc["Name"] == "Surface" || pc["Name"] == "Phone") .OrderByAscending(pc => new[] { pc["CategoryId"] }) .Project(pc => pc.SelectField(f => f["CategoryId"]) .SelectField(f => f["Name"]) .SelectField(f => f["Description"])); var selectExecutor = client.CreateRelationalBatchExecuter( RelationalBatchExecutionMode.Transactional); OperationResult<IReadOnlyList<RelationalEntity>> queryResult = null; await selectExecutor .Query(query, out queryResult) .ExecuteAsync(); var updateExecutor = client.CreateRelationalBatchExecuter( RelationalBatchExecutionMode.Transactional); // Traverse the results foreach (var entry in queryResult.Result) { string categoryId = ""; object name = null; string description = ""; // The same shorthand is applied there, where the generic type // parameter can be omitted. The expected type can also be // passed as a parameter of type Type. if (entry.TryGetValue<string>("CategoryId", out categoryId) && entry.TryGetValue("Name", out name, typeof(string)) && entry.TryGetValue("Description", out description)) { Console.WriteLine("{0} - {1} - {2}", categoryId, name, description); } } }
I hope this leaves you something to work with. You can always find more information in https://docs.microsoft.com/en-us/common-data-service/entity-reference/ , particularly: https://docs.microsoft.com/en-us/common-data-service/entity-reference/cds-sdk-manipulate-data
I am trying to insert values for Lookup type field. I am able to insert and update values for other types of fields.
But lookup relations are not getting updated.
Can you please provide any example of updating a Lookup type field using CDS sdk ?
Thank you Michael. But I am trying to update lookup fields using CDS sdk through code in C#.
User | Count |
---|---|
158 | |
91 | |
68 | |
63 | |
63 |
User | Count |
---|---|
210 | |
155 | |
93 | |
81 | |
71 |