Tuesday, August 23, 2016

CRM 2016 Web API Retrieve Attribute Names By Entity Name

GetEntityMetaData function will retrieve the list of attributes from he Account entity.

 function GetEntityMetaData() {  
   var entityType = "Account";  
   var metaDataId = getMetaDataIdByName(entityType);  
   var schemaNames = getAttributeList(metaDataId);  
 }  
 function getAttributeList(metaDataId) {  
   var attributesList;  
   $.ajax({  
     type: "GET",  
     contentType: "application/json; charset=utf-8",  
     datatype: "json",  
     url: Xrm.Page.context.getClientUrl() + "/api/data/v8.1/EntityDefinitions(" + metaDataId + ")?$select=LogicalName&$expand=Attributes($select=LogicalName)",  
     beforeSend: function (XMLHttpRequest) {  
       XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");  
       XMLHttpRequest.setRequestHeader("OData-Version", "4.0");  
       XMLHttpRequest.setRequestHeader("Accept", "application/json");  
       XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");  
     },  
     async: false,  
     success: function (data, textStatus, xhr) {  
       attributesList = data.Attributes;  
     },  
     error: function (xhr, textStatus, errorThrown) {  
       alert(textStatus + " " + errorThrown);  
     }  
   });  
   return attributesList;  
 }  
 function getMetaDataIdByName(entityType) {  
   var MetadataId;  
   $.ajax({  
     type: "GET",  
     contentType: "application/json; charset=utf-8",  
     datatype: "json",  
     url: Xrm.Page.context.getClientUrl() + "/api/data/v8.1/EntityDefinitions?$select=DisplayName,IsKnowledgeManagementEnabled,EntitySetName&$filter=SchemaName eq '" + entityType + "'",  
     beforeSend: function (XMLHttpRequest) {  
       XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");  
       XMLHttpRequest.setRequestHeader("OData-Version", "4.0");  
       XMLHttpRequest.setRequestHeader("Accept", "application/json");  
       XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");  
     },  
     async: false,  
     success: function (data, textStatus, xhr) {  
       var result = data.value[0];  
       MetadataId = result.MetadataId;  
     },  
     error: function (xhr, textStatus, errorThrown) {  
       alert(textStatus + " " + errorThrown);  
     }  
   });  
   return MetadataId;  
 }  

Monday, February 1, 2016

Retrieve Team Members using RetrieveMultipleRequest

As we know that CRM Sdk deprecated RetrieveMembersTeamRequestmessage (https://msdn.microsoft.com/en-us/library/gg309360.aspx). So there is only way to retrieve Team Members using RetrieveMultipleRequest. and here is the method for the same. It works for the AccessTeam too.


 private static void RetrieveTeamMembers(IOrganizationService service, EntityReference teamEntityReference)  
 {  
 QueryExpression userQuery = new QueryExpression("systemuser");  
 userQuery.ColumnSet = new ColumnSet("fullname");  
 LinkEntity teamLink = new LinkEntity("systemuser", "teammembership", "systemuserid", "systemuserid", JoinOperator.Inner);  
 ConditionExpression teamCondition = new ConditionExpression("teamid", ConditionOperator.Equal, teamEntityReference.Id);  
 teamLink.LinkCriteria.AddCondition(teamCondition);  
 userQuery.LinkEntities.Add(teamLink);  
 EntityCollection retrievedUsers = service.RetrieveMultiple(userQuery);  
 foreach (Entity user in retrievedUsers.Entities)  
 {  
 Console.WriteLine(String.Format("User: {0} with GUID {1}", user.GetAttributeValue<string>("fullname"),  
 user.Id));  
 }  
 }