Dynamics CRM 2011 View to display records shared with current user

In my current project, there is a requirement to create a view to display shared record of certain entity.

This kind of view is supported but can not be achieved through customization user interface.

System view and user view are defined by using fetchxml and layoutxml, we could create a view through CRM API and set the correct fetchxml with is not support in out of the box customization user interface.

When records are shared, CRM create a record of “principalobjectaccess” entity; so we can link the entity we want to display to this entity to construct proper fetchxml.

The drawback of the approach is that you can not use customization user interface to modify the column layout and sort order.

Suppose we have an entity called “zzhou_cfiapplication” and we want to create a view to display records shared with current user.

The important piece of xml to add to view fetchxml is the link entity to “principalobjectaccesss” entity.


<link-entity name='principalobjectaccess' to='zzhou_cfiapplicationid' from='objectid' link-type='inner' alias='share'>
 <filter type='and'>
 <condition attribute='principalid' operator='eq-userid' />
 </filter>
</link-entity>

It is much more easier to configure the view in customization user interface to set up correct columns and sort order, and then modify the fetchxml for that view.

We can use the following code to retrieve the view that we want to modify


public Entity RetrieveSavedQuery(OrganizationServiceContext context, Guid queryId)
 {
 var query = from q in context.CreateQuery("savedquery")
 where q.GetAttributeValue<Guid>("savedqueryid") == queryId
 select q;

Entity record = query.FirstOrDefault();

return record;
 }

we can update the view by using the following code

</pre>
public void UpdateSavedQuery(IOrganizationService service)
 {
 Entity savedQuery = new Entity("savedquery");
 savedQuery.Id = new Guid("00bc926f-26c0-e111-a4f9-00155d4c5b01");
 savedQuery["fetchxml"] = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
 <entity name='zzhou_cfiapplication'>
 <attribute name='zzhou_name' />
 <attribute name='createdon' />
 <order attribute='zzhou_name' descending='false' />
 <link-entity name='principalobjectaccess' to='zzhou_cfiapplicationid' from='objectid' link-type='inner' alias='share'>
 <filter type='and'>
 <condition attribute='principalid' operator='eq-userid' />
 </filter>
 </link-entity>
 <attribute name='zzhou_cfiapplicationid' />
 </entity>
 </fetch>";

service.Update(savedQuery);
 }
<pre>
Advertisements

4 comments on “Dynamics CRM 2011 View to display records shared with current user

  1. Unfortunately when we create CRM View and update definition to the presented here everything works, but CRM Outlook Client stops working. Do you know any solution to this issue?

  2. Zhongchen Zhou,

    How can i retrieve data from table PrincipalObjectAccess using javascript? is that possible? could you help me?

  3. Pingback: MS Dynamics CRM. View records shared with current user | Technology & Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s