The data contract type cannot be serialized in partial trust because the member is not public while using Serialization in the sandboxed plugin in CRM 2011


Hi,

Was getting the below error while serializing the CRM Entity as mentioned here

Then gave it a try with a DataContract just to check if it works properly with it or not. It worked properly on the sandbox plugin.

Please check the below discussion for more details.

http://social.microsoft.com/Forums/en-US/d0332c9c-3cef-4b34-8c11-2b263369e21f/serializing-entity-in-sandbox-doesnt-work

Bye.

Calling WCF Rest Service from a Plugin in CRM 2011 (Online)


Let us take a simple rest service example here

http://www.rockycherry.net/services/Photos/photos.svc/photos

It returns the string “You have asked for photos”.

Within the plugin class add references to following assemblies.

The sample code of the plugin


namespace TestPlugin
{
 public class Class1 : IPlugin
 {
 public void Execute(IServiceProvider serviceProvider)
 {
 IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

 if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
 {

// using WebClient
 WebClient webClient1 = new WebClient();
 string data1 = webClient1.DownloadString("http://www.rockycherry.net/services/Photos/photos.svc/photos");

// using WebChannelFactory
 WebChannelFactory<IPhotos> factory = new WebChannelFactory<IPhotos>(new Uri("http://www.rockycherry.net/services/Photos/photos.svc"));
 IPhotos proxy = factory.CreateChannel();
 string photos = proxy.GetPhotos();

 }

 }
 }

[ServiceContract]
 public interface IPhotos
 {
 [WebGet(UriTemplate = "Photos")]
 [OperationContract]
 string GetPhotos();
 }
}


Register the plugin in Sandbox mode for CRM 2011 online.

Check this wonderful post for all the details

http://rockycherry.net/blog/?p=179

Hope it helps.

Integrating CRM 2011 and SharePoint 2013 using BCS (WCF Service – CRUD Operation)


Create a new WCF Service Application in Visual Studio with .NET Framework version as 4.0 (since we will be using CRM 2011’s assembly that are in version 4.0).

Please go through this MSDN article first to get a clear understanding on the BCS Service

http://msdn.microsoft.com/en-us/library/office/ff953200(v=office.14).aspx

Define the Service Contract and the Date Contract in the following manner. Here we would be performing CRUD operation on Date of Birth and Last Name field of the Contact Entity.

Add references to the following assemblies

Microsoft.BusinessData can be found at

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI on the SharePoint Server.


[ServiceContract]
 public interface IService1
 {
 [OperationContract]
 IEnumerable<ContactEntity> ReadList();

[OperationContract]
 ContactEntity ReadItem(Guid contactId);

[OperationContract]
 ContactEntity Create(ContactEntity newContact);

[OperationContract]
 void Update(ContactEntity contact);

[OperationContract]
 void Delete(Guid contactId);
 }

 [DataContract]
 public class ContactEntity
 {
 [DataMember]
 public Guid ContactId { get; set; }

[DataMember]
 public DateTime? DateOfBirth { get; set; }

[DataMember]
 public string LastName { get; set; }
 }


Implement the Service Contract


public class Service1 : IService1
 {
 private readonly IOrganizationService _orgService = GetOrganizationService();

public ContactEntity ReadItem(Guid contactId)
 {
 try
 {
 var cols = new ColumnSet(new[] {"lastname", "birthdate"});
 ContactEntity contactRecord = null;
 Entity entity = _orgService.Retrieve("contact", contactId, cols);
 if (entity != null)
 {
 contactRecord = new ContactEntity();
 contactRecord.ContactId = contactId;
 if (entity.Contains("lastname"))
 {
 contactRecord.LastName = entity.Attributes["lastname"].ToString();
 }
 if (entity.Contains("birthdate"))
 {
 contactRecord.DateOfBirth = (DateTime) entity.Attributes["birthdate"];
 }
 }

return contactRecord;
 }
 catch (InvalidOperationException)
 {
 throw new ObjectNotFoundException(
 "Unable to read data for the conatct with ID = " +
 contactId.ToString() +
 ". The contact no longer exists.");
 }
 catch (Exception generalException)
 {
 throw new RuntimeException(
 "There was a problem reading contact data.",
 generalException);
 }
 }

public IEnumerable<ContactEntity> ReadList()
 {
 try
 {
 var queryExpression = new QueryExpression("contact");
 var conditionExpression = new ConditionExpression("statuscode", ConditionOperator.Equal, 1);
 queryExpression.ColumnSet = new ColumnSet(new[] {"contactid", "lastname", "birthdate"});
 queryExpression.Criteria.AddCondition(conditionExpression);

EntityCollection contactCollection = _orgService.RetrieveMultiple(queryExpression);
 var lstContactType = new List<ContactEntity>();
 ContactEntity contactRecord;
 foreach (Entity entity in contactCollection.Entities)
 {
 contactRecord = new ContactEntity();
 contactRecord.ContactId = new Guid(entity.Attributes["contactid"].ToString());
 if (entity.Contains("lastname"))
 {
 contactRecord.LastName = entity.Attributes["lastname"].ToString();
 }
 if (entity.Contains("birthdate"))
 {
 contactRecord.DateOfBirth = (DateTime) entity.Attributes["birthdate"];
 }

lstContactType.Add(contactRecord);
 }

return lstContactType;
 }
 catch (Exception generalException)
 {
 throw new RuntimeException(
 "There was a problem reading contact data.",
 generalException);
 }
 }

public ContactEntity Create(ContactEntity newContact)
 {
 try
 {
 var returnContact = new ContactEntity();
 returnContact.ContactId = Guid.NewGuid();
 returnContact.DateOfBirth = newContact.DateOfBirth;
 returnContact.LastName = newContact.LastName;

var contactRecord = new Entity();
 contactRecord.LogicalName = "contact";
 contactRecord.Attributes["lastname"] = returnContact.LastName;
 contactRecord.Attributes["contactid"] = returnContact.ContactId;
 contactRecord.Attributes["birthdate"] = returnContact.DateOfBirth;
 newContact.ContactId = _orgService.Create(contactRecord);

return returnContact;
 }
 catch (Exception generalException)
 {
 throw new RuntimeException(
 "There was a problem creating a new contact.",
 generalException);
 }
 }


 public void Update(ContactEntity updateContact)
 {
 try
 {
 var contactRecord = new Entity();
 contactRecord.LogicalName = "contact";
 contactRecord.Attributes["lastname"] = updateContact.LastName;
 contactRecord.Attributes["contactid"] = updateContact.ContactId;
 contactRecord.Attributes["birthdate"] = updateContact.DateOfBirth;

_orgService.Update(contactRecord);
 }
 catch (InvalidOperationException)
 {
 throw new ObjectNotFoundException(
 "Unable to update the contact with ID = " +
 updateContact.ContactId.ToString() +
 ". The contact no longer exists.");
 }
 catch (Exception generalException)
 {
 throw new RuntimeException(
 "There was a problem updating the lawyer with ID = " +
 updateContact.ContactId.ToString() + ".", generalException);
 }
 }

public void Delete(Guid contactId)
 {
 try
 {
 _orgService.Delete("contact", contactId);
 }
 catch (InvalidOperationException)
 {
 throw new ObjectNotFoundException(
 "Unable to delete the contact with ID = " +
 contactId.ToString() +
 ". The contact no longer exists.");
 }
 catch (Exception generalException)
 {
 throw new RuntimeException(
 "There was a problem lawyer the customer with ID = " +
 contactId.ToString() + ".", generalException);
 }
 }

public static IOrganizationService GetOrganizationService()
 {
 var organizationUri = new Uri("http://crmserver/orgname/XRMServices/2011/Organization.svc");
 Uri homeRealmUri = null;
 var credentials = new ClientCredentials();
 credentials.Windows.ClientCredential = new NetworkCredential("username", "password", "domain");
 var orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
 IOrganizationService _service = orgProxy;
 return _service;
 }

 

Host the Service in the IIS.

Open SharePoint Designer 2013 and connect to the site where we want to create an external content type based on our WCF Service.

Create a new External Content Type

Specify name for the content type and click on the link for defining operations

Click on Add Connection and specify WCF Service as the connection type

Specify the url of the WCF service

For each of the Web Methods, specify the corresponding operation.

For each of the operations define ContactId as Identifier

Once defined, select Create Lists and Form button

Open the SharePoint site and open the list created

Corresponding contact records in CRM

Hope it helps.

“Network Error” while calling WCF Service through JavaScript.


Hi,

I was getting the following “Network Error” on calling WCF Service using XMLHttpRequest within an html page through JavaScript.

I was using IE 10 in a Windows 8 machine.

After some searching found out that this is a known issue.

http://bugs.jquery.com/ticket/12790

Running the same in compatibility mode worked properly.

Hope it helps.

An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.


We were getting the above error while testing one of our WCF service. On syncing the clock of our client and server machine we were able to resolve the issue.

This helped us resolve the issue

http://social.msdn.microsoft.com/Forums/eu/wcf/thread/eccf2b09-6ea1-4663-9ddf-af29901ea60a

Bye.

 

Using JavaScript to consume a WCF service


Suppose this is our simple WCF service which takes the name as input and returns the resulting string with Hello appended to it.

namespace HelloWorldWCFService{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(string name);
}
}

namespace HelloWorldWCFService{
public class Service1 : IService1 {
public string GetData(string name) {
return “Hello “ + name;

}}}
It uses BasicHttpBinding.

Now this would be our JavaScript function which can consume the above WCF service.

function MyRequest() {var xmlhttp = new XMLHttpRequest();

xmlhttp.open(‘POST’, http://localhost:64833/Service1.svc&#8217;, false);xmlhttp.setRequestHeader(‘SOAPAction’, http://tempuri.org/IService1/GetData&#8217;);

xmlhttp.setRequestHeader(‘Content-Type’, ‘text/xml’);
var data = ;

data += ‘<s:Envelope xmlns:s=”http://schemas.xmlsoap.org/soap/envelope/”>&#8217;;data += ‘ <s:Body>’;

data += ‘ <GetData xmlns=”http://tempuri.org/”>&#8217;;

data += ‘ <name>Nishant</name>’;

data += ‘ </GetData>’;

data += ‘ </s:Body>’;

data += ‘</s:Envelope>’;xmlhttp.send(data);

alert(“status = “ + xmlhttp.status);alert(“status text = “ + xmlhttp.statusText);

alert(xmlhttp.responseText); 

}

Here for

xmlhttp.setRequestHeader(‘SOAPAction’, http://tempuri.org/IService1/GetData&#8217;); 

We can get the value for SOAPAction from the wsdl of the WCF service. 

Open the http://localhost:64833/Service1.svc?wsdl

Search for soapAction there.

We would find soapAction value there.

<wsdl:operation name=”GetData“>  <soap:operation
soapAction=”http://tempuri.org/IService1/GetData style=”document” />
<wsdl:input>

Hope it helps !