April 18, 2011 by Christoff Truter C#
In one of my previous posts we had a look at
how to add a file to a CRM 4.0 account using
the webservices that ship with CRM - in this post we're going to look at how to accomplish this using CRM 2011.
In CRM 4.0 I used the webservice "/MSCRMServices/2007/CrmService.asmx", which is also available
in CRM 2011 - which in theory should provide us with some compatibility, allowing us to still use our older code.
(in my example I didn't use the CRM 4.0 SDK)
But unfortunately (at the moment I wrote this, perhaps Microsoft fixed it by the time you read this) this is not the case
at all, if we attempt to add a file using the 2007 service provided by CRM 2011 you will get an exception that looks
something like this:
If we look at the detail of our SoapException, we'll find a message looking something like this:
<error> <code>0x80040216</code> <description>Attribute objectidtypecode must have the same value as attribute objecttypecode</description> <type>Platform</type> </error>
public EntityNameReference objectidtypecode { get { return this.objecttypecodeField; } set { this.objecttypecodeField = value; } }
[System.Xml.Serialization.XmlElementAttribute(Order=24)] public EntityNameReference objecttypecode { get { return this.objecttypecodeField; } set { this.objecttypecodeField = value; this.RaisePropertyChanged("objecttypecode"); } }
crmsvcutil /url:"http://dionysis:5555/CSTruter/XRMServices/2011/Organization.svc" /out:Xrm.cs
Exiting program with exception: Could not load file or assembly 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'This means that you need to install the WIF (Windows Identity Foundation) on your machine, available from here
using System; using System.Linq; using System.IO; using Microsoft.Xrm.Sdk.Client; using System.ServiceModel.Description; class Program { static OrganizationServiceContext context; static void Main(string[] args) { ClientCredentials credentials = new ClientCredentials(); credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials; //credentials.Windows.ClientCredential = new System.Net.NetworkCredential("username", "password", "domain"); Uri uri = new Uri("http://dionysis:5555/UIT/XRMServices/2011/Organization.svc"); OrganizationServiceProxy proxy = new OrganizationServiceProxy(uri, null, credentials, null); proxy.EnableProxyTypes(); // enable support for early-bound entities context = new OrganizationServiceContext(proxy);
static void AddDocument(string filename, Guid id, string logicalName) { Annotation annotation = new Annotation(); FileInfo fileInfo = new FileInfo(filename); annotation.FileName = fileInfo.Name; annotation.DocumentBody = Convert.ToBase64String(File.ReadAllBytes(filename)); annotation.IsDocument = true; // rather pass the actual type annotation.MimeType = "application/octet-stream"; annotation.ObjectId = new Microsoft.Xrm.Sdk.EntityReference(logicalName, id); context.AddObject(annotation); context.SaveChanges(); }
Account account = (from p in context.CreateQuery<Account>() where p.Name == "test" select p).SingleOrDefault(); if (account != null) { AddDocument(@"c:\test.txt", account.Id, "account"); }
August 5, 2011 by Anonymous
THANK YOU. I didnt realize Documentbody was the actual contents of the file, your guide helped alot.