locked
C# Access All Records w/ DeletionStateCode = 2 RRS feed

  • Question

  • Hello,
     
    My immediate goal is as follows:

    SELECT Name
    FROM Account
    INNER JOIN Contact ON
    Account.AccountID = Contact.AccountID
    WHERE Account.DeletionStateCode = 2

    My code using the SDK is as follows:

    int x;

    x = 2;

    CrmAuthenticationToken token = new CrmAuthenticationToken();

    token.AuthenticationType = 0;

    token.OrganizationName = "Org";

    CrmService crm = new CrmService();

    crm.Url = http://appsdev:9999/mscrmservices/2007/crmservice.asmx;

    crm.CrmAuthenticationTokenValue = token;

    crm.Credentials = System.Net.CredentialCache.DefaultCredentials;

    QueryExpression q1 = new QueryExpression();

    ColumnSet cs1 = new ColumnSet();

    LinkEntity l1 = new LinkEntity();

    ConditionExpression c1 = new ConditionExpression();

    FilterExpression f1 = new FilterExpression();

    q1.EntityName = EntityName.account.ToString();

    //q1.EntityName = EntityName.contact.ToString();

    cs1.Attributes = new string[] { "Name" };

    //l1.JoinOperator = JoinOperator.Inner;

    //l1.LinkFromEntityName = "contact";

    //l1.LinkFromAttributeName = "accountid";

    //l1.LinkToEntityName = "account";

    //l1.LinkToAttributeName = "accountid";

    c1.AttributeName = "DeletionStateCode";

    c1.Operator = ConditionOperator.Equal;

    c1.Values = new object [] {System.Convert.ToInt32(x)};

    f1.FilterOperator = LogicalOperator.And;

    f1.Conditions = new ConditionExpression[] { c1 };

    //q1.LinkEntities = new LinkEntity[] { l1 };

    q1.ColumnSet = cs1;

    q1.Criteria = f1;

    BusinessEntityCollection contacts = crm.RetrieveMultiple(q1);

    Just trying to test grabbing all accounts where DeletionStateCode = 2 is failing with a generic Unable To Process Request. I have verified that the token's Org Name and URL are spelled correctly, and that there are indeed records that still exist with a DeletionStateCode = 2.

    Any thoughts?

    Monday, February 23, 2009 6:59 PM

Answers

  • You cannot use the CRM SDK to do this - it doesn't expose the DeletionStateCode attribute, and will only return data with DeletionStateCode = 0. Your only option is to use the System.Data classes and submit the SQL statement you posted. Note that CRM users will not have SQL permission on the Account or Contact views
    Microsoft CRM MVP - http://mscrmuk.blogspot.com/
    • Marked as answer by MC81 Tuesday, February 24, 2009 3:22 PM
    Monday, February 23, 2009 8:52 PM
    Moderator

All replies

  • You cannot use the CRM SDK to do this - it doesn't expose the DeletionStateCode attribute, and will only return data with DeletionStateCode = 0. Your only option is to use the System.Data classes and submit the SQL statement you posted. Note that CRM users will not have SQL permission on the Account or Contact views
    Microsoft CRM MVP - http://mscrmuk.blogspot.com/
    • Marked as answer by MC81 Tuesday, February 24, 2009 3:22 PM
    Monday, February 23, 2009 8:52 PM
    Moderator
  • David,

    OK, thanks for the info.
    Tuesday, February 24, 2009 3:22 PM