web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How do I retrieve an entity's ObjectTypeCode from a plugin/c# without read permissions on entities

(0) ShareShare
ReportReport
Posted on by 125

Is there any way to retrieve the ObjectTypeCode for a custom entity within the SDK without requiring the user to have read permissions for entities in their role security? I also don't want to do direct SQL reads due to other security issues with that. Thanks!

To get the ObjectTypeCode from a custom entity I retrieve EntityMetaData but this requires the user's role have read permissions on Entities. This is generally something I do when retrieving or adding notes, attachments, or activities to custom entities where I can't just hardcode the ObjectTypeCode. This is done from plugins as well as iframes, custom web pages, etc. thus the compiler directives for both plugins and non-plugins with the parameter.

Here is my code to retrieve the metadata.

    public static EntityMetadata RetrieveEntity(
#if CRM_PLUGIN
    IMetadataService metadataService,
#else
    MetadataService metadataService,
#endif
    string strLogicalName, bool boolFailIfMissing)
    {
        EntityMetadata entity = null;
        try
        {
            RetrieveEntityRequest entityRequest = new RetrieveEntityRequest();
            entityRequest.EntityItems = EntityItems.IncludeRelationships | EntityItems.IncludeAttributes;
            entityRequest.LogicalName = strLogicalName.ToLower();
            entityRequest.RetrieveAsIfPublished = false;
            RetrieveEntityResponse entityResponse = (RetrieveEntityResponse)metadataService.Execute(entityRequest);
            entity = entityResponse.EntityMetadata;
        }
        catch (SoapException ex)
        {
            WriteLog((boolFailIfMissing ? TraceLevel.Error : TraceLevel.Info),
                string.Format("entity {0} does not exist.", strLogicalName + "\n" + ex.Detail.InnerText));
            entity = null;
            if (boolFailIfMissing)
                throw ex;
        }
        return entity;
    }

 

*This post is locked for comments

I have the same question (0)
  • Grzegorz Kalek Profile Picture
    4,210 on at

    Hi, you can register the plugin to be executed using specified user account, you can also create new metadata service instance and define your access credentials.

    Regards,

    Grzegorz

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hi.

    Try to use following code to create IMetaDataService instanse with admin rights inside the code of your plugin:

    IMetadataService ms = context.CreateMetadataService(false;

  • Bill Faulk Profile Picture
    125 on at

    Thanks, I still need to revisit this.  I'll try setting just the metadata service to not impersonate the user and see how that works. As long as the actual updates/reads execute with the right permissions under the CrmService it should be fine.

    Generally I need the metadata to match text to picklist values. I also have a web address that retrieves an xml doc with requested values from a parameter which contains a list of entity.attribute combinations with a given key. I use this to populate other fields on a form before it is saved and where mapping from a relationship isn't possible.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans