How to get all lookup values from MS CRM using XRM SDK - c#

I am writing an app which uses XRM SDK to get data from MS CRM 11. For fields of type lookup I want to get their corresponding definition so I can retrieve all possible ids and values from the related entity.
I cannot seem to find a way to get this. I understand I can get the entity reference for an entity instance value and how to get target entities from meta data but cannot find anything to help me get all lookup ids and values for a lookup field. Please help!
Edit:
In the interest of helping others, and after several more days investigation, I am convinced what I want to do is not possible server-side using sdk.
I can get pick list values no problem using a RetrieveAttributeRequest and cast the resulting AttributeMetaData to PicklistAttributeMetadata. You can use the same technique to deal with Lookups by casting to LookupAttributeMetadata but this only gives you one useful property over the base class: Targets. All this does is provide a string array of entity logical names. It gives you no additional detail such as mapped Id/Name properties or any view details for lookups where queries are applied (such as primary contact lookups where contacts listed are for current organisation).
So, in the end I have had to compromise. I can get the target entity name from Targets and assume that the lookup is simple - just pull all records from entity. The Id column is fixed so that is ok and generally you are safe to assume the Name column is available (albeit likely asl_name etc. if custom).
If anyone knows a better way I will gladly, willingly eat humble pie!

I believe what you're trying to do is fetch all records of a particular entity after you have retrieved the entity name using the metadata service.
If that is what you are looking to do, I suggest having a look at the following location in the SDK (I am referring to the SDK for CRM 2013) SDK\SampleCode\JS\RESTEndpoint\JQueryRESTDataOperations\JQueryRESTDataOperations\Scripts for the file JQueryRESTDataOperationsSample.js
There's this function called RetrieveMultiple which you can use to fetch all records of a particular entity by providing the Entity Name and any filters (optional). If you need help creating the oData query for the filters, you can download the oData Query Deisgner to form the query.

Related

Is there any way to map a Sharepoint 2010 user defined column to a specific column in sql?

I have a customer that is using Sharepoint 2010 as a repository for CRM documents, but has defined two custom user defined columns in Sharepoint to house the document GUID & document type. He spends most of his time in SQL, so has been querying nvarchar11 & navarchar12 for this data. But the columns were not always added in the same order, so the GUID that is usually in 11 can sometimes be in 12 or 13, depending on the Library. Is there any way to specifically map/re-map the GUID to nvarchar11 & the type to nvarchar12 in the AllUserData table in the SQL Content Database?
If you are willing to go down the route of creating/defining your lists as part of custom list templates (i.e. lots of tweaking of XML files) you will get access to the xml element called Field https://msdn.microsoft.com/en-us/library/office/ms437580.aspx which gives you the attribute ColName which has this description:
Optional Text. An internal attribute that defines the mapping of the field to the physical storage name for this field. This must be a valid name in the underlying database and must be identical to the name used in the database table. If not specified, the server generates a column name that does not collide with any existing column names and that contains only characters that are allowed by Microsoft SQL Server.
With that said, I'd strongly advise you against doing this on a couple of fronts.
Creating/maintaining list templates is arcane and annoying
You are causing yourself an upgrade challenge
You shouldn't be encouraging people from directly querying the content database tables. That is strongly discouraged by Microsoft: https://support.microsoft.com/en-us/help/841057/support-for-changes-to-the-databases-that-are-used-by-office-server-pr

$expand outside IQueryable

I seem to have an unusual need that I need to be able to $expand objects, but I can not do that using standard IQUeryable.
I am using EntityFramework and AutoMapper to project from database entities to API data objects that I then expose via OData. This has the problem that it limits what I can do - to those elements that AutoMapper can ProjectTo.
This generally is not a problem - if the items in the Api object are part of the database. I now have certain objects where I must (sometimes optionally) add data that comes from other sources. We talk generally about data only held in memory (like an object's error details, which include the stack trace, or some runtime stats).
As such, I need to find a way to manipulate (filter) queries and - more important actually - to handle $expand in single entity GET operations (though support for multi get is also welcome, but there I can possibly handle this by using ODataQueryDetails).
For single item queries, though, I have a controller method in the form like:
Get ([FromODataUri] Guid key) {
which gives me no access to the ODataQueryDetails at all. Anyone knows how to get those query details in this case?
Documentation on the whole "customize the query" element is not particularly big - and generally seems to assume someone is "Just playing with some IQueryable interface" - not taking into account that you may need a multi stage processing or do something that mixes in memory and database data.
Note: Web API OData: How do you $expand on a single entity? is NOT a duplicate - that was a single issue with a parameter name (key required as name) and does not go deep enough.
If you simply add a parameter to Get as follows, Web API parameter binding will supply a value to the method. Let Thing be the entity type handled by the controller.
public IHttpActionResult Get([FromODataUri] Guid key, ODataQueryOptions<Thing> opts)
Note that you need to specify the generic version of ODataQueryOptions or you will get an exception.
This will get you an object representing the query options for the current request, but you will not be able to modify the options (none of the interesting properties have public setters). There seems to be a lot of developer demand for the ability to intercept and modify query options, but there is no out-of-the-box solution at the moment. See the open issue on Github that is currently targeting the 5.1.0 release. See OData V4 modify $filter on server side for current best practice on modifying query options in a controller method.

Quick CRUD access for simple SQL schema

I'm a little behind the times on data access and need to be pointed in the right direction. I currently have just a single SQL Server db table with about 35 columns in it. I'm building a WCF web service to provide access to it and am trying to figure out the quickest way to link up the C# based web service to the database.
I've been looking at Entity Framework but it seems pretty heavy and everything I've found on it so far seems to assume you already know something about it so I didn't want to get too far into it if it's the wrong path. I'm not fully sold on the idea of generating SQL in the application. I already have a DataContract class with properties for each column in the table, I'm just looking for an automatic way to map columns to properties and properties back to columns/sproc parameters. I already wrote some code that uses reflection to map data from a different source to this DataContract (matching on property name with a dictionary of additional mappings as a backup) so it's not that much work to do the same here, but I wanted to see what else is available. What I want to avoid is writing out each PropertyName = ColumnName.Value. Is there something light weight built into VS2010 .NET 4.0 for a simple case like this? Would directly calling a stored procedure through EF as is mentioned here be a good option? It looks a little out of date.
I like Dapper, a "micro-ORM". It's used by SO. I've used it beautifully as "an automatic way to map columns to properties" and it appears to also do "properties back to columns/sproc parameters" but I haven't used it for that. It's superb - I had it going in about 5 minutes after getting it with Nuget. I'm a newbie to EF and wouldn't recommend it without a guide.
IEnumerable<TModel> result;
using (MySqlConnection conn = new MySqlConnection(_mysqlConnString))
{
// "Query" is a Dapper extension method that stuffs the datareader into objects based on the column names
result = conn.Query<TModel>("Select * from YourTable");
}
// do stuff with result
This links to a full example, instead of just the piece I pulled out of my current project. http://www.tritac.com/bp-24-dapper-net-by-example
other Dapper information: c-sharpcorner.com/UploadFile/4d9083/… and liangwu.wordpress.com/2012/08/16/dapper-net-samples talk about it.
(moved from comment as this was more an answer than a comment - I'm trying to do the proper task in the proper place).

What is the difference between calling OrganizationService.Associate() and explicitly setting the foreign key field of an entity in Dynamics CRM?

Is there some additional logic performed by Associate() operation?
I want to programmatically copy a lot of data from one Dynamics CRM instance to another one. And I suppose it would be simpler to make plain copies of rows (starting from the root objects in order to avoid breaking constraints).
And furthermore, is it possible to clone systemuser and business units instances (rows), too?
Thank you in advance!
PS: by cloning a row (using OrganizationServiceProxy), I mean:
fetch all attributes of a row (from Dynamics CRM 1)
e = new entity(), set all attributes (including id), then service.create(e) (on Dynamics CRM 2)
Did you consider to do a backup and restore to another server your CRM database? Might be it could help you. In any case, you can add new records to any tables inside of CRM database, but it is on your own risk. Using SQL to modify any data is in the list of unsupported technologies by Microsoft. Especially if you are talking about system users and business units.
Also you can write simple application which will insert data using CRM SDK.
Associate can be used to clean up in the end, but your order of entities will be what you want to first layout.
So for example, you will want to copy Accounts before Contacts. But then, on the Account you may have a primary contact that you will need to go back and Associate. This is no different than going back and updating the account record with the lookup value (post contacts being inserted).
I would also suggest looking at programmatically exporting the base unmanaged solution and then importing it if need be.

How do I programmatically set a custom field on the Lead/Contact entity to be merged automatically in MS CRM 4 and 5?

I have two fields on the Lead and Contact record and I want to set those up so that when duplicate Lead/Contact records are merged or we convert a lead into a contact, those custom fields get merged every time without having to do any extra work. You can set this up manually so I imagine it can also be done programmatically... I just can't seem to find information on how to do it.
Thanks in advance.
You can register a plugin on the Merge message for the lead or contact entities. There, you'll have access to the primary and secondary records, and you can move fields to the primary as you need to.
UPDATE: Per your comment, there's an entity called 'attributemap' that can be used to retrieve the mappings ('entitymap' is the parent entity if you need to retrieve the maps for only a certain set of entities). You can give creating one of these a try programmatically, not sure if the API will let you.

Categories