How to create custom entity itself in CRM Dynamics via API - c#

Is there way I can connect to CRM dynamics and create custom entity itself. In API, I will pass entity name, fields, datatype and etc details required for creating custom entity.
I would like to use C# for making API calls.

Yes. You need to use the Metadata services.
Sample: Create and update entity metadata.
CreateEntityRequest createrequest = new CreateEntityRequest
{
//Define the entity
Entity = new EntityMetadata
{
SchemaName = _customEntityName,
DisplayName = new Label("Bank Account", 1033),
DisplayCollectionName = new Label("Bank Accounts", 1033),
Description = new Label("An entity to store information about customer bank accounts", 1033),
OwnershipType = OwnershipTypes.UserOwned,
IsActivity = false,
},
// Define the primary attribute for the entity
PrimaryAttribute = new StringAttributeMetadata
{
SchemaName = "new_accountname",
RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
MaxLength = 100,
FormatName = StringFormatName.Text,
DisplayName = new Label("Account Name", 1033),
Description = new Label("The primary attribute for the Bank Account entity.", 1033)
}
};
_serviceProxy.Execute(createrequest);

Related

Microsoft Dynamics CRM Annotation Entity wrong "Created by" Field value

I am working with Dynamics CRM annotation, i developed an external application that use the organization service in order to create new annotation linked to a custom entity and linked to the user based on the user id, by set the CallerId in the organization Service and by set the field "CreatedBy" in the annotation object on create.
The problem is that the annotation is sometimes the value of "Created by" is not correct and it randomly set it by another user.
below used code:
Guid callerID = new Guid(HttpContext.Current.Request.QueryString["CallerId"].ToString());
CrmServiceClient connection = new CrmServiceClient(connectionString);
OrganizationServices = connection.OrganizationServiceProxy;
OrganizationServices.CallerId = new Guid(callerID);
.
.
.
Entity Annotation = new Entity("annotation");
Annotation.Attributes["objectid"] = new EntityReference("RelatedEntityLogical", RelatedEntity.Id);
Annotation.Attributes["objecttypecode"] = RelatedEntity.LogicalName;
.
.
.
Annotation.Attributes["createdby"] = new EntityReference("systemuser", callerID);
OrganizationServices.Create(Annotation);
Any ideas?
Thanks
Maybe try this:
Do not set the createdby attribute - i.e. remove this line:
Annotation.Attributes["createdby"] = new EntityReference("systemuser", callerID);
Set the CallerId directly on the instance of CrmServiceClient:
connection.CallerId = new Guid(callerID);
Invoke Create directly from the instance of CrmServiceClient:
connection.Create(Annotation);

Create entity for events in CRM 2011

I built a web api using C# to communicate to CRM . I was successful to retrieve accounts. What I want to do is I want to retrieve the events from the CRM . I think I have to create entity for the events in the crm but how can I do that?
This is the code for the accounts
ColumnSet colsPrincipal = new ColumnSet("lastname", "firstname", "domainname", "systemuserid", "businessunitid");
QueryExpression queryPrincipal = new QueryExpression();
queryPrincipal.EntityName = "event";//systemuser
queryPrincipal.ColumnSet = colsPrincipal;
var myAccounts = CommonCrm.crmContext.RetrieveMultiple(queryPrincipal);
foreach (var myEntity in myAccounts.Entities)
{
//create new crm users and add it to the list
CrmUser thisOne = new CrmUser();
thisOne.firstName = myEntity.GetAttributeValue<string>("firstname");
thisOne.lastName = myEntity.GetAttributeValue<string>("name");
thisOne.userId = myEntity.GetAttributeValue<string>("domainname");
thisOne.userGuid = myEntity.GetAttributeValue<Guid>("systemuserid");
thisOne.buId = myEntity.GetAttributeValue<EntityReference>("businessunitid").Id;
// and so on and so forth...
Console.Write(thisOne.firstName + " " + thisOne.userGuid + " " + thisOne.lastName);
CrmUsers.Add(thisOne);
arra.Add(thisOne.firstName);
}
The easiest way, via CRM Customizations and Solutions. As it is a pretty huge topic, I'd recommend you getting started with this link

Connect CouchDB with asp.net C# application

How to connect couchDB with ASP.NET C# application? If any one can you give a sample application.
I had the same need and after evaluating the options available, to meet the requirements of my application, I created any components that helped me a lot and maybe they can help you and also others. I make it clear that I have no intention of promoting myself here, just sharing something that may be useful.
The detailed explanation of how to configure and use it is on Github.
Link: Nuget Package |
Github
Example of use for retrieving documents with mango-querie:
IList<User> users;
var sts = new List<String> { "ACTIVE", "LOCKED" };
using (UserRepository db = new UserRepository())
{
var query = db.FindOf("list-status", new { id = "OwnerIdloop.user.7", statuses = sts });
users = db.List<User>(query);
}
Array.ForEach(users.ToArray(), Console.WriteLine);
Example of adding documents:
User user = createUser("email#email.com");
using (UserRepository db = new UserRepository())
{
var result = db.Insert<User>(user); // add document and return instance changed with operation revision id
Console.WriteLine(result.Revision);
}
Example of changing documents:
using (UserRepository db = new UserRepository())
{
// Load document data by ID
var user = db.Get<User>("email#email.com");
user.Name = user.Name + "::CHANGED";
var result = db.Update<User>(user); // update document and return instance changed with operation revision id
Console.WriteLine(result.Revision);
}
Example of deleting documents:
using (UserRepository db = new UserRepository())
{
// Load document data by ID
var user = db.Get<User>("email#email.com");
var result = db.Delete<User>(user); // delete document from database. Return true case sucess or false case not deleted
Console.WriteLine($"Sucesso: {result}");
}
After installing the NuGet, just create an instance of MyCouch.Client and pass it the URL of your database.
using (var client = new MyCouchClient("http://127.0.0.1:5984/test"))
{
//Consume here
}
The format is: {scheme}://[{username}:{password}]/{authority}/{localpath}. From v0.11.0, there's a specific MyCouchUriBuilder that you can use for building the Uri. It will automatically e.g. apply Uri.EscapeDataString to username and password when calling SetBasicCredentials.
var uriBuilder = new MyCouchUriBuilder("http://localhost:5984/")
.SetDbName(TestConstants.TestDbName)
.SetBasicCredentials("foob#r", "p#ssword");
return new MyCouchClient(uriBuilder.Build());
For more details Click Here

How to add an inherited type using wcf data service client

I have an inheritance hierarchy where Owners, Tenants, Vendors and Agents are derived from People. In my WCF Data Service client, I want to use AddObject to create a new Owner, but I cannot find how to do this. When I try:
var owner = new Owner()
{
FirstName = "Test"
,LastName = "Person"
,CheckName = "Test Person"
,PersonNo = "Test"
,UseFullNameForName = false
,TypeOfPerson = "Owner"
};
//Add
context.AddObject("People", owner);
context.SaveChanges();
the service throws a dynamic sql error. I am using WCF Services 5.4 with EF 4.5.
you should create the entity type of the table.
var person = new Person {
FirstName = "Test"
,LastName = "Person"
,CheckName = "Test Person"
,PersonNo = "Test"
,UseFullNameForName = false
,TypeOfPerson = "Owner"
};
context.Persons.AddObject(person);
context.SaveChanges();

How do you insert odata edm?

A c# client app is accessing a web api 2 odata controller by way of a service reference. How do yo do an insert? This code does not work:
CourseServiceRef.Container container1;
CourseServiceRef.NotNeeded notNeeded;
notNeeded.Username1 = "test";
notNeeded.Email = "test";
var serviceResponse = container1.SaveChanges();
The service reference is called CourseServiceRef. It contains an entity set called NotNeeded. Using Entity Framework 6.1
In case someone finds useful:
Uri uri = new Uri("http://localhost:50222/odata");
var container = new CourseServiceRef.Container(uri);
CourseServiceRef.NotNeeded newTempAccount = new CourseServiceRef.NotNeeded()
{
Email = model.UserName,
Username1 = model.UserName
};
if (newTempAccount != null)
{
container.AddToNotNeededs(newTempAccount);
container.SaveChanges();
}

Categories

Resources