Thursday, December 20, 2007

How to download and save email activity attachments from CRM 3.0

Ever want to get an attachment from an Email in CRM 3.0?
Well you are in luck, here is how to do it.

1) Use fetchxml to get all attachments from a specific email or for any other emails matching your query, for simplicity sake, the fetch xml will be for a single email.

2) Email attachments will be returned in the result set complete with the contents of the original file in Base64 format, you will have to decode it.

Use the following method to get a nodelist for any query

static XmlNodeList GetFetchResult(string fetchXML)

{

CrmService service = new CrmService();

service.Credentials = new System.Net.NetworkCredential("CRMAdmin", "Pa$$w0rd", "ADVWORKS");

string fetch = fetchXML;

string ret = service.Fetch(fetch);

System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();

xdoc.LoadXml(ret);

System.Xml.XmlNodeList list = xdoc.SelectNodes("resultset/result");

xdoc = null;

return list;

}

All email attachments are kept in the entity "ActivityMimeAttachment", here is the xml for the query.

Replace the "activityID" value with any email value or add your conditions.

<fetch mapping='logical'>
<entity name='activitymimeattachment'>
<all-attributes/>
<filter type='and'>
<condition attribute='activityid' operator='eq' value='{A9B5DD63-D59E-DC11-920F-0003FF612152}'/>
</filter>
</entity>
</fetch>
Call the GetFetchResult into a new Nodelist and loop through it.
XmlNodeList list = GetFetchResult("");

if (list.Count > 0)

{

foreach (XmlNode n in list)

{
string body = n["body"].InnerText;
string decoded = base64Decode(body);

}

}

Use this method to convert the body of the email into it's original format.

static string base64Decode(string data)

{

try

{

System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();

System.Text.Decoder utf8Decode = encoder.GetDecoder();

byte[] todecode_byte = Convert.FromBase64String(data);

int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);

char[] decoded_char = new char[charCount];

utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);

string result = new String(decoded_char);

return result;

}

catch (Exception e)

{

throw new Exception("Error in base64Decode" + e.Message);

}

}
From here you can save the file, the filename is in the "filename" attribute of the result set.
Have a great day.
Oshri Cohen

Labels: , , , , , , , ,

Friday, October 19, 2007

How to Identify the currently logged in user using the MSCRM service

I needed to build a custom CRM page that would be hosted within the crm 3.0 environment, it need to be accessed via the navigation pane. Given that requirement the page need to find out which user was logged in.

using the the webservice do the following:

WhoAmIRequest whoAmI = new WhoAmIRequest();
WhoAmIResponse Iam = (WhoAmIResponse)service.Execute(whoAmI);
Guid userid = Iam.UserId;


The WhoAmIResponse includes also includes the business unit belonging to the user.

Oshri Cohen

Labels: , , , , , , , ,