Back

TechnologyNov 10, 2014

CRM 2011: Creating a View of Records Shared With the User

Levi Fuller

One of the great features of Microsoft CRM is the ability to create custom views. Often, these views can be created by saving an Advanced Find or sharing it as a view. But what about sharing specific records with a user? Is there a view to display records that have been shared with the current user?

The short answer: Not by default.

In order to create a view of all records of a certain entity that are shared with the current user, the view must be created through custom FetchXML and LayoutXML. When records are shared, an entry is added to the PrincipalObjectAccess table, connecting the ID of the record with the ID of the system user. When creating a custom view in the user interface (UI), the Advanced Find functionality cannot use the Principal Object Access table because this is not an entity that can be accessed publicly. As such, developers must link the entity directly in the Fetch XML with the following code:

The first filter condition finds any users associated with the record. The second condition ensures the user has any type of rights on the record. If there are no rights then the record will hold a ‘0’ under the AccessRightsMask field. This ensures the record disappears from the new custom view if the record is “unshared” with any given user.

In order to use this code, simply replace “tableid” with the ID column of the entity that has the shared records (e.g. “accounted”).

Personalize marketing campaigns and increase revenue with Microsoft Dynamics

Explore Our Microsoft Consulting Services  →

It is important to note this type of view cannot be customized through the UI. Any updates must be done in code, or a new view must be created when the XML is changed. As such, you cannot search within the custom views and you cannot change it though the UI.

As it cannot be accomplished in the UI, a view of shared records is created with a new “Saved Query” entity. Developers will need properly formatted FetchXML and LayoutXML, then use the CRM service proxy to create the Saved Query.

The following code will create a view called “Accounts Shared With Me”:

string fetchXml = @"

";

string layoutXML = @"

";

SavedQuery sq = new SavedQuery { Name = "Accounts Shared With Me", Description = "Shows Accounts shared with user", //not required ReturnedTypeCode = "account", FetchXml = fetchXml, LayoutXml = layoutXML, QueryType = 0 //Ensures the view will be public };

try { crmServiceProxy.Create(sq); //crmServiceProxy is the service proxy in the current //context (a console application in this sample code) } catch (Exception e) { Console.WriteLine(e); }

While there are some limits imposed with this view (mainly not being able to search or edit the view in the UI), this is a clean way to directly see all records of a certain entity shared with the current user in CRM 2011.

Be sure to follow us on Twitter or LinkedIn for more useful tips.  If you have a question or comment related to the blog, you can use the comments section below to join the conversation.