Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to get a list of emails with no attachment?

Posted on by 417

How to get a list of specific E-Mails with no attachment?

Since I read that, the advanced search is not longer a option:
community.dynamics.com/.../199988

Anyway, I tried it with a specific email, and even when this e-mail does not contain a attachment it was in the list:
<fetch>
  <entity name="email" >
    <filter>
      <condition attribute="senton" operator="on-or-after" value="2018-04-01" />
      <condition attribute="wd_activity_type" operator="eq" value="100000016" />
      <condition attribute="statuscode" operator="eq" value="3" />
      <condition attribute="activityid" operator="eq" value="C5E6EA35-C788-E811-A2B8-00505687C621" />
    </filter>
    <link-entity name="annotation" from="annotationid" to="activityid" link-type="outer" >
      <filter>
        <condition attribute="annotationid" operator="null" />
      </filter>
    </link-entity>
  </entity>
</fetch>
Here's the email:
emailwithnoattachment.png

It tried it with C#/SDK but no success:

public void Retrieve_Emails_Without_Attachment()
{
    string connectionString = "AuthType=AD;Url=http://URL/ORG";

    CrmServiceClient conn = new CrmServiceClient(connectionString);

    IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

    string emailXml = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\" distinct=\"false\">" +
                        "  <entity name=\"email\">" +
                        "    <attribute name=\"subject\" />" +
                        "    <attribute name=\"regardingobjectid\" />" +
                        "    <attribute name=\"activityid\" />" +
                        "    <attribute name=\"from\" />" +
                        "    <attribute name=\"to\" />" +
                        "    <attribute name=\"statuscode\" />" +
                        "    <attribute name=\"modifiedon\" />" +
                        "    <attribute name=\"activityid\" />" +
                        "    <attribute name=\"senton\" />" +
                        "    <attribute name=\"createdby\" />" +
                        "    <attribute name=\"createdon\" />" +
                        "    <order attribute=\"subject\" descending=\"false\" />" +
                        "    <filter type=\"and\">" +
                        "      <condition attribute=\"wd_activity_type\" operator=\"eq\" value=\"100000016\" />" +
                        "      <condition attribute=\"statuscode\" operator=\"eq\" value=\"3\" />" +
                        "      <condition attribute=\"senton\" operator=\"on-or-after\" value=\"2018-04-01\" />" +
                        "    </filter>" +
                        "  </entity>" +
                        "</fetch>";

    // MS named attachments as annotations for whatever reason
    string annotationXml = "<fetch>" +
                            "  <entity name=\"annotation\" >" +
                            "    <filter>" +
                            "      <condition attribute=\"createdon\" operator=\"gt\" value=\"2018-04-01\" />" +
                            "      <condition attribute=\"filename\" operator=\"ends-with\" value=\"ikkey\" />" +
                            "    </filter>" +
                            "  </entity>" +
                            "</fetch>";
    

    List<Entity> emails = RetrieveAllRecordsFromAllPages(service, emailXml);

    List<Entity> attachments = RetrieveAllRecordsFromAllPages(service, annotationXml);

    List<Guid> emailsWithAttachment = new List<Guid>();
    List<Guid> emailsWithNoAttachment = new List<Guid>();

    foreach(Entity email in emails)
    {
        Guid emailRegardingObjectId = email.GetAttributeValue<EntityReference>("regardingobjectid") != null ? email.GetAttributeValue<EntityReference>("regardingobjectid").Id : Guid.Empty;

        if(emailRegardingObjectId != Guid.Empty)
        {
            foreach(Entity attachment in attachments)
            {
                int filesize = attachment.GetAttributeValue<int>("filesize");

                Guid noteRegardingObjectId = attachment.GetAttributeValue<EntityReference>("objectid") != null ? attachment.GetAttributeValue<EntityReference>("objectid").Id : Guid.Empty;

                if (emailRegardingObjectId == noteRegardingObjectId)
                {
                    if (filesize < 1)
                    {
                        emailsWithNoAttachment.Add(emailRegardingObjectId);
                        break;
                    }
                    else
                    {
                        emailsWithAttachment.Add(emailRegardingObjectId);
                        break;
                    }
                }

            }
        }
    }
}


*This post is locked for comments

  • bernhards Profile Picture
    bernhards 417 on at
    RE: How to get a list of emails with no attachment?

    That is the solution I was not able to find via Google. Thank you very much!

  • Verified answer
    mvilas02 Profile Picture
    mvilas02 356 on at
    RE: How to get a list of emails with no attachment?

    hi , 

    Why dont you try and check "attachmentcount" attribute  ?

    Regards,

    Vilas

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans