Introduction:
Here I am Posting how to assign/Change a Owner to any account.Here The First step will find the account to which the owner to set.after finding it pass the Guid of the new owner to the AssignRequest.
Example
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Data;
using System.Collections;
using System.ServiceModel.Description;
using System.ServiceModel;
public class Setowner
{
private static IOrganizationService _service;
private static OrganizationServiceProxy _serviceProxy;
private static OrganizationServiceContext orgContext;
static public void Main(string[] args)
{
Setowner p = new Setowner();
p.Settingowner();
Console.WriteLine("Owner set");
Console.ReadKey();
}
private void Settingowner()
{
try
{
string file = "SetOwner.txt";
GetCRMService();
//Find the account to which the owner has to set
var query = from a in orgContext.CreateQuery("account")
where (String)a["name"] == "manish"
select a["accountid"];
foreach (var accid in query)
{
try
{
AssignRequest assign = new AssignRequest
{
//systemuser; i.e., User to whome you are assigning the entity to
//Current record which you are assigning to the user
Assignee = new EntityReference("systemuser", GetOwnerId("Dinesh Desai")),
Target = new EntityReference("account", new Guid(accid.ToString()))
};
// Execute the Request
_service.Execute(assign);
File.AppendAllLines(file, new string[] { "Assigned" });
}
catch (FaultException ex)
{
File.AppendAllLines("Error.txt", new string[] { "manish" + ":" + (ex.InnerException == null ? ex.Message : ex.InnerException.Message) });
}
}
}
catch (FaultException ex)
{
File.AppendAllLines("error.txt", new string[] { (ex.InnerException == null ? ex.Message.ToString() : ex.InnerException.ToString()) });
}
}
// This Method will Find the Guid of the Owner
private Guid GetOwnerId(string owner)
{
Guid OwnerId = Guid.Empty;
var query = from a in orgContext.CreateQuery("systemuser")
where (String)a["fullname"] == owner.Trim()
select a["systemuserid"];
foreach (var id in query)
{
OwnerId = new Guid(id.ToString());
}
return OwnerId;
}
//For connection to CRM
private static void GetCRMService()
{
try
{
string file = "connection.txt";
Uri organizationuri = new Uri("https://kartik.crm5.dynamics.com/XRMServices/2011/Organization.svc");
Uri homeRealmUri = null;
ClientCredentials credential = new ClientCredentials();
credential.Windows.ClientCredential = new NetworkCredential("username", "password", "Kartik");
_serviceProxy = new OrganizationServiceProxy(organizationuri, homeRealmUri,credential, null);
_serviceProxy.EnableProxyTypes();
_service = (IOrganizationService)_serviceProxy;
orgContext = new OrganizationServiceContext(_serviceProxy);
File.AppendAllLines(file, new string[] { "connection tested" });
}
catch (FaultException ex)
{
File.AppendAllLines("error.txt", new string[] { (ex.InnerException == null ? ex.Message.ToString() : ex.InnerException.ToString()) });
}
}
}
}
GetCRMService()-This Method is For Setting up with CRM.That includes credential+CRM Url etc.
Settingowner():-This Method will be For setting the Owner to any account
GetOwnerId():-This Method is For Finding the Guid Of the new owner for account.
Same way you can set the Owner for any Entity You have to just set the Target in Settingowner() Method.
Here I take "account" in Target.Because we are setting the owner in account if you want to set it in contact then you just need to specify "contact" in place of "account".
No comments:
Post a Comment