Random Musings

December 30, 2008

Sharing Records using CRM SDK API

Filed under: Dynamics CRM — haditeo @ 12:03 pm

Developers can programmatically grant access to certain records, either read, write or other access through GrantAccessRequest.

Here is the reference at MSDN : http://msdn.microsoft.com/en-us/library/bb956464.aspx

Here is the code snippet in how to utilize GrantAccessRequest

        private void ShareCase(ref ICrmService service, string incidentId, string userId)
        {
            try
            {
                // Create the SecurityPrincipal Object
                SecurityPrincipal principal = new SecurityPrincipal();
                principal.Type = SecurityPrincipalType.User;

                // PrincipalId is the Guid of the user to whom access is being granted
                principal.PrincipalId = new Guid(userId);

                // Create the PrincipalAccess Object
                PrincipalAccess principalAccess = new PrincipalAccess();

                // Set the PrincipalAccess Object's Properties
                principalAccess.Principal = principal;

                // Gives the principal access to read
                principalAccess.AccessMask = AccessRights.ReadAccess;

                // Create the Target Object for the Request
                TargetOwnedIncident target = new TargetOwnedIncident();

                // EntityId is the Guid of the account access is being granted to
                target.EntityId = new Guid(incidentId);

                // Create the Request Object
                GrantAccessRequest grant = new GrantAccessRequest();

                // Set the Request Object's properties
                grant.PrincipalAccess = principalAccess;
                grant.Target = target;

                // Execute the Request
                GrantAccessResponse granted = (GrantAccessResponse)service.Execute(grant);
            }
            catch (SoapException ex)
            {
                throw ex;
            }
            catch (Exception e)
            {               
                throw e;
            }
        }

Switch Reporting Services to different server

Filed under: Dynamics CRM — haditeo @ 11:52 am
Tags:

Just came across these 2 articles in how to switch or migrate Reporting Services to different servers :

http://msdn.microsoft.com/en-us/library/bb522762.aspx
http://www.microsoft.com/dynamics/crm/using/deploy/changesrs.mspx

December 21, 2008

Load external javascript file using xmlhttprequest

Filed under: Dynamics CRM — haditeo @ 10:47 am
Tags:

A useful post in how to load external javascript file using xmlhttprequest

Please refer to Henry Cordes article : http://www.henrycordes.nl/post/2008/05/External-js-file-and-CRM.aspx

Enable custom HTTP header

Filed under: Dynamics CRM — haditeo @ 10:37 am
Tags:

A very useful post in configuring client side caching as well as forcing the browser to check if there is any latest update of client side caching in the browser.

Please refer to Ronald Lemmen Post : http://ronaldlemmen.blogspot.com/2008/07/javascript-files-and-caching.html

December 14, 2008

Throwing friendly error message to the user after processing plugin

Filed under: Dynamics CRM — haditeo @ 9:41 am

Developers can utilize InvalidPluginException to show friendly error message to the user to inform them that there is an error during plugin processing.

Refer to this code snippet

public void Execute(IPluginExecutionContext context)
{
    try
    {
	if (context.Depth == 1)
	{
	    ICrmService svc = context.CreateCrmService(true);

	    DynamicEntity contact = (DynamicEntity)context.PostEntityImages["image"];
	}
    }
    catch (Exception e)
    {
	throw new InvalidPluginExecutionException("there is an error during plugin processing", e);
    }
}

December 11, 2008

Construct a Query Expression Based On Advanced Find Query

Filed under: Dynamics CRM — haditeo @ 11:10 pm

How do you construct a query expression based on the “Advanced Find” query ?

You can first build the “Advanced Find” query. And then at the result window, press F11 and insert this code in the browser address :

javascript:prompt('query ', resultRender.FetchXml.value);

December 10, 2008

Disable alert when closing the CRM form

Filed under: Dynamics CRM — haditeo @ 9:20 pm
Tags: ,

If developers want to close the form programmatically without the alert “Are you sure you want to navigate away from this page? Your changes have not been saved. To stay on the page so that you can save your changes, click Cancel. Press OK to continue, or Cancel to stay on the current page.”, please refer to the below codes :

crmForm.detachCloseAlert();
window.close();

December 8, 2008

Tagging attachment during DeliverIncomingEmail event

Filed under: Dynamics CRM — haditeo @ 12:01 pm

In the plugin, DeliverIncomingEmail event on the email entity can be used to process any incoming emails. At that event, the email record has been created properly, so that developers can utilize the record for any other purposes, let’s say create a new custom case.

Just recently found out that the email’s attachments are actually created automatically in the activitymimeattachment records. Now here is the problem: how to tag the email’s attachments with the custom case as well ?

The solution is by creating Annotation records based on the ActivityMimeAttachment records.

Refer to this code snippet for sample :

public static void CreateAnnotation(ref CrmService service, email mail, Guid incidentGuid)
{
    try
    {
	BusinessEntityCollection activityMimeAttachmentColl = RetrieveByAttributeReturnColl(ref service, "activitymimeattachment", "activityid", mail.activityid.Value.ToString(), new string[] { "body", "attachmentnumber", "filesize", "filename" });

	foreach (DynamicEntity deActivityMimeAttachment in activityMimeAttachmentColl.BusinessEntities)
	{
	    DynamicEntity deAnnotation = new DynamicEntity();
	    deAnnotation.Name = "annotation";

	    StringProperty spDocumentBody = new StringProperty();
	    if (deActivityMimeAttachment.Properties.Contains("body"))
	    {
               spDocumentBody.Value = deActivityMimeAttachment["body"].ToString();
	    }
	    else
	    {
               spDocumentBody.Value = string.Empty;
	    }
	    spDocumentBody.Name = "documentbody";
	    deAnnotation.Properties.Add(spDocumentBody);

	    StringProperty spFileName = new StringProperty();
	    if (deActivityMimeAttachment.Properties.Contains("filename"))
	    {
               spFileName.Value = deActivityMimeAttachment["filename"].ToString();
	    }
	    else
	    {
               spFileName.Value = string.Empty;
	    }
	    spFileName.Name = "filename";
	    deAnnotation.Properties.Add(spFileName);

	    StringProperty spSubject = new StringProperty();
	    if (deActivityMimeAttachment.Properties.Contains("attachmentnumber"))
	    {
                spSubject.Value = "attachment - " + ((CrmNumber)deActivityMimeAttachment["attachmentnumber"]).Value.ToString();
	    }
	    else
	    {
                spSubject.Value = "subject";
	    }
	    spSubject.Name = "subject";
	    deAnnotation.Properties.Add(spSubject);

	    CrmNumberProperty cnpFileSize = new CrmNumberProperty();
	    if (deActivityMimeAttachment.Properties.Contains("filesize"))
	    {
                cnpFileSize.Value = ((CrmNumber)deActivityMimeAttachment["filesize"]);
	    }
	    else
	    {
                cnpFileSize.Value = new CrmNumber(0);
	    }
	    cnpFileSize.Name = "filesize";
	    deAnnotation.Properties.Add(cnpFileSize);

	    CrmBooleanProperty cbpIsDocument = new CrmBooleanProperty();
	    cbpIsDocument.Value = new CrmBoolean(true);
	    cbpIsDocument.Name = "isdocument";
	    deAnnotation.Properties.Add(cbpIsDocument);

	    EntityNameReferenceProperty enrpObjectTypeCode = new EntityNameReferenceProperty();
	    enrpObjectTypeCode.Value = new EntityNameReference("incident");
	    enrpObjectTypeCode.Name = "objecttypecode";
	    deAnnotation.Properties.Add(enrpObjectTypeCode);

	    LookupProperty lpObjectId = new LookupProperty();
	    lpObjectId.Value = new Lookup("incident", incidentGuid);
	    lpObjectId.Name = "objectid";
	    deAnnotation.Properties.Add(lpObjectId);

	    TargetCreateDynamic target = new TargetCreateDynamic();
	    target.Entity = deAnnotation;
	    CreateRequest req = new CreateRequest();
	    req.Target = target;
	    CreateResponse res = (CreateResponse)service.Execute(req);
	}
    }
    catch (System.Web.Services.Protocols.SoapException ex)
    {
        WriteEntryToEventLog("public static void CreateAnnotation(ref CrmService service, email mail, Guid incidentGuid)", ex);

        throw ex;
    }
    catch (Exception e)
    {
        WriteEntryToEventLog("public static void CreateAnnotation(ref CrmService service, email mail, Guid incidentGuid)", e);

        throw e;
    }
}

RetrieveByAttribute Code Snippet

Filed under: Dynamics CRM — haditeo @ 3:28 am
Tags: , ,

My custom RetrieveByAttribute code snippet is frequently used to retrieve an object based on one attribute.

In example, SELECT ContactId, FirstName,EmailAddress1 FROM Contact WHERE EmailAddress1 = ’email@email.com’

This is the signature of the method :

public static BusinessEntityCollection RetrieveByAttributeReturnColl(ref ICrmService service, string entityName, string attributes, string values, string[] colSet)

This is how to invoke this method based on the above query

BusinessEntityCollection becContact = Utilities.RetrieveByAttributeReturnColl(ref service, "contact", "emailaddress1", "email@email.com", new string[] {"contactid", "firstname", "emailaddress1"});

When there is multiple contact records returned by this method, the collection can be accessed by using becContact.BusinessEntities.

Here is the definition of the method:


public static BusinessEntityCollection RetrieveByAttributeReturnColl(ref ICrmService service, string entityName, string attributes, string values, string[] colSet)
{
    try
    {
	// Create the ConditionExpression.
	ConditionExpression condition = new ConditionExpression();

	// Set the condition for the retrieval  
	condition.AttributeName = attributes;
	condition.Operator = ConditionOperator.Like;
	condition.Values = new string[] { values };

	// Create the FilterExpression.
	FilterExpression filter = new FilterExpression();

	// Set the properties of the filter.
	filter.FilterOperator = LogicalOperator.And;
	filter.Conditions.Add(condition);

	// Create the QueryExpression object.
	QueryExpression query = new QueryExpression();

	// Set the properties of the QueryExpression object.
	query.EntityName = entityName;

	if (colSet.Length > 0)
	{
	    ColumnSet col = new ColumnSet();

	    foreach (string s in colSet)
	    {
               col.AddColumn(s);
	    }

	    query.ColumnSet = col;
	}
	else
	{
	    query.ColumnSet = new AllColumns();
	}

	query.Criteria = filter;

	// Create the request object.
	RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();

	// Set the properties of the request object.
	retrieve.Query = query;
	retrieve.ReturnDynamicEntities = true;

	// Execute the request.
	RetrieveMultipleResponse retrieved = (RetrieveMultipleResponse)service.Execute(retrieve);

	return retrieved.BusinessEntityCollection;
    }
    catch (System.Web.Services.Protocols.SoapException ex)
    {
        WriteEntryToEventLog("public static BusinessEntityCollection RetrieveByAttributeReturnColl(ref ICrmService service, string entityName, string attributes, string values, string[] colSet)", ex);

         throw ex;
    }
    catch (Exception e)
    {
        WriteEntryToEventLog("public static BusinessEntityCollection RetrieveByAttributeReturnColl(ref ICrmService service, string entityName, string attributes, string values, string[] colSet)", e);

        throw e;
    }
}

Create a free website or blog at WordPress.com.