How to view records shared with a user in Dynamics CRM

Dynamics crm How to see records shared with current user

In Dynamics crm you can create different types of views but by oob views it’s not possible to show the records shared with current user in any view.
For achieving we need to update the xml of a system view to make it show shared records.
Scenario: Let’s say we have a view of lead named “my shared leads” in which we want that user should be able to see leads shared with him.

Following is the fetch xml of the view which can be downloaded from advanced find.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="lead">
    <attribute name="fullname" />
    <attribute name="companyname" />
    <attribute name="telephone1" />
    <attribute name="leadid" />
    <order attribute="fullname" descending="false" />
  </entity>
</fetch>

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Now to make it show shred records we have to update it’s fetch xml with following xml:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

principalobjectaccess table store shared records.

to: primary field of the entity for which the view is (leadid in our case)

condition: principalid

Operator: eq-userid means princinpal user should be equal to current user.

Updated XML will look like

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">

  <entity name="lead">

    <attribute name="fullname" />

    <attribute name="companyname" />

    <attribute name="telephone1" />

    <attribute name="leadid" />

    <order attribute="fullname" descending="false" />

<link-entity name='principalobjectaccess' to='leadid' from='objectid' link-type='inner' alias='share'>

 <filter type='and'>

 <condition attribute='principalid' operator='eq-userid' />

 </filter>

</link-entity>

  </entity>

</fetch>

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Now get the guid of view from database or by querying as follows

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public Guid GetSavedQueryId(IOrganizationService service,String viewname)

{

Guid result = Guid.Empty;

 QueryExpression q = new QueryExpression("savedquery");

q.Criteria.AddCondition("name", ConditionOperator.Equal, viewname);

EntityCollection en = service.RetrieveMultiple(q);

if (en.Entities.Count > 0)

result= en.Entities[0].Id;

return result;

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Once you got the Guid of the view you can update it with the help of following code:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public void UpdateSavedQuery(IOrganizationService service,Guid viewid)
 {
 Entity UpdateQuery = new Entity("savedquery");
 UpdateQuery.Id = viewid;  //new Guid("12bc926f-26c0-e111-a4f9-00155d1c5b01"); //Guid of the view to be updated
 UpdateQuery["fetchxml"] = @" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
   <entity name='lead'>
 <attribute name='fullname' />
 <attribute name='companyname' />
 <attribute name='telephone1' />
 <attribute name='leadid' />
 <order attribute='fullname' descending='false' />
 <link-entity name='principalobjectaccess' to='leadid' from='objectid' link-type='inner' alias='share'>
  <filter type='and'>
  <condition attribute='principalid' operator='eq-userid' />
  </filter>
 </link-entity>
   </entity>
 </fetch>";

service.Update(UpdateQuery);
 }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Don’t forget to replace ” in fetch xml with ‘ .
Once you execute above code, you’ll be able to see shared record in your view.
Happy CRM

One thought on “How to view records shared with a user in Dynamics CRM

Add yours

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: