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);
Related
This is truly one of the strangest issues I've run into.
I have a Web API which uses EF. I have an audit table which takes an ApplicationUser. I create the new object, add it to the collection and then call SaveChangesAsync(). The weird part is, I get "User name MyUserName is already taken." error.
using (var context = new ApplicationDbContext())
{
var user = context.Users.Single<ApplicationUser>(x => x.UserName == model.UserName);
var sid = context.SessionIds.FirstOrDefault(x => x.Id == model.SessionId);
var audit = new Audit
{
Data = model.Data,
User = user,
IpAddress = Helper.GetClientIp(Request),
Session = sid != null ? sid : ItsMyChance.Entities.Entities.SessionId.Create(scoreModel.UserName, scoreModel.GameId)
};
context.Audits.Add(audit);
await context.SaveChangesAsync();
}
Update
This code has been working for years. The difference is I upgrade from .NET 4.5 to .NET 4.61
Update 2
I also tried the following but still receive the same error
[ForeignKey("User")]
public string UserId { get; set; }
public ApplicationUser User { get; set; }
Update 3
Trying to track this issue down I call
var entries = context.ChangeTracker.Entries();
It returns several entries, 1 for each object, including User. User shows Added and another as Unchanged. I can't figure out how this is happening.
In addition, I added the following before making any changes but there's no effect.
context.Configuration.AutoDetectChangesEnabled = false;
Since You are adding the complete user object in Audit , so SaveChangesAsync will save a new Entry for Audit and User also and since a user with same username already exists that's why you are getting this error. What you should do is just assign just the UserId (Whatever is referral key in Audit table for User) in Audit object
var audit = new Audit
{
Data = model.Data,
UserId = user.Id,
IpAddress = Helper.GetClientIp(Request),
Session = sid != null ? sid : ItsMyChance.Entities.Entities.SessionId.Create(scoreModel.UserName, scoreModel.GameId)
};
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);
I am using VS 2013 and the Neo4j client in an MVC application and can't get past building the query.
In the following code, I can connect to my server but on the var newUser line I get an error over the new User statement saying it's a property but used like a type, that can bee seen in this screen shot:
var client = new GraphClient(new System.Uri("http://localhost:7474/db/data"));
client.Connect();
var newUser = new User { Id = 456, Name = "Jim" };
client.Cypher
.Merge("(user:User { Id: {id} })")
.OnCreate("user")
.Set("user = {newUser}")
.WithParams(new
{
id = newUser.Id,
newUser
})
.ExecuteWithoutResults();
I think I need to add or remove a reference but I a not sure what it is.
If you read the error, you'll see User is a property of Controller, so it's not recognized as a type.
You'll need to prefix the namespace, like new Neo4j.User() or whatever its documentation states it uses.
I am trying to retrieve all users in domain but it's never work. I never got any error in my code.
I am adding as reference Google.GData.Apps.dll in my project. I am using Google Apps Provisioning API. I am referring these link https://developers.google.com/google-apps/provisioning/
Code :-
string domain = #"<domain name>";
string subs = #"<Authorization code>";
AppsService appService = new AppsService(domain, subs);
// appService.CreateUser("john.jain","James","anderson","james#1234");
// appService.DeleteUser("Amitesh");
appService.RetrieveAllUsers();
The following works for me - RetrieveAllUsers() returns a UserFeed object containing all users. Note that I am using a different constructor for the apps service (using username/password credentials and not OAuth).
this.appsService = new AppsService(credential.Domain, credential.Username, credential.Password);
UserFeed userFeed = this.appsService.RetrieveAllUsers();
// Selecting all users except domain super administrators.
var usernamesInDomain =
(from UserEntry user in userFeed.Entries
where user.Login.Admin != true
select user.Login.UserName).ToList();
What does the returned UserFeed object contain in your case?
This is my solution :-
Google.GData.Apps.UserFeed s = appService.RetrieveAllUsers();
foreach (UserEntry s1 in s.Entries)
{
string username = s1.Login.UserName.ToString();
}
I generated entities from CRM like this:
CrmSvcUtil.exe /url:http://<servername>/<organizationname>/XRMServices/2011/Organization.svc
/out:<outputfilename>.cs /username:<username> /password:<password> /domain:<domainname>
/namespace:CRMEntities /serviceContextName:XrmServiceContext
For serviceContextName I set XrmServiceContext.
I want to retrieve some entity from CRM using next code:
var context = new XrmServiceContext(myorgserv);
var marketingList = context.ListSet.Where(item => item.Id == new Guid("SOME GUID"));
And I'm getting error:
Message "Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'CRMEntities.List'."
After 'add to watch' I saw that every set of entities in context have the same message.
What have I missed?
Problem solved. After initializing OrganizationServiceProxy, I have to call
EnableProxyTypes method.
OrganizationServiceProxy orgserv;
ClientCredentials clientCreds = new ClientCredentials();
clientCreds.Windows.ClientCredential.UserName = username;
clientCreds.Windows.ClientCredential.Password = password;
clientCreds.Windows.ClientCredential.Domain = domain;
IServiceConfiguration<IOrganizationService> orgConfigInfo = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(orgServiceUri);
orgserv = new OrganizationServiceProxy(orgConfigInfo, clientCreds);
orgserv.EnableProxyTypes();
Key thing is: orgserv.EnableProxyTypes();