Create BulkDelete in Silverlight

Risposta suggerita Create BulkDelete in Silverlight

  • venerdì 1 aprile 2011 06:47
     
      Contiene codice
    Hi

    Has anyone ever managed to create a bulk delete in Silverlight? I set up the SOAP Endpoint as described here: http://msdn.microsoft.com/en-us/library/gg594452.aspx
    Then I have the following code:

       QueryExpression qE = new QueryExpression();
    
       qE.EntityName = "entityName"
    
       qE.ColumnSet = new ColumnSet();
    
       qE.ColumnSet.AllColumns = true;
    
       qE.ColumnSet.Columns = new System.Collections.ObjectModel.ObservableCollection<string>();
    
    
    
       ConditionExpression internalNameExpression = new ConditionExpression();
    
       internalNameExpression.AttributeName = "name";
    
       internalNameExpression.Operator = ConditionOperator.Equal;
    
       internalNameExpression.Values = new System.Collections.ObjectModel.ObservableCollection<object>();
    
       internalNameExpression.Values.Add("testname");
    
    
    
       qE.Criteria = new FilterExpression();
    
       qE.Criteria.Conditions = new System.Collections.ObjectModel.ObservableCollection<ConditionExpression>();
    
       qE.Criteria.Conditions.Add(internalNameExpression);
    
    
    
       Guid[] emptyRecipients = new Guid[0];
    
    
    
       OrganizationRequest request = new OrganizationRequest { RequestName = "BulkDelete" };
    
       request.Parameters = new ParameterCollection();
    
       request.Parameters.Add(new KeyValuePair<string, object>("RecurrencePattern", "FREQ=MINUTELY;INTERVAL=2"));
    
       request.Parameters.Add(new KeyValuePair<string, object>("StartDateTime", DateTime.Now));
    
       request.Parameters.Add(new KeyValuePair<string, object>("JobName", SettingsManager.AddonName.ToLower() + ": Renewer"));
    
       request.Parameters.Add(new KeyValuePair<string, object>("SendEmailNotification", false));
    
       request.Parameters.Add(new KeyValuePair<string, object>("QuerySet", new QueryExpression[] { qE }));
    
       request.Parameters.Add(new KeyValuePair<string, object>("ToRecipients", emptyRecipients));
    
       request.Parameters.Add(new KeyValuePair<string, object>("CCRecipients", emptyRecipients));
    
    
    
       client.ExecuteCompleted += new EventHandler<ExecuteCompletedEventArgs>(client_ExecuteCompleted);
    
       client.ExecuteAsync(request);
    
    

    When I call ExecuteAsync I always get the following error:

    Fehler beim Serialisieren von Parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:request. Die InnerException-Nachricht war "Der Typ "MyNamespace.QueryExpression[]" mit dem Datenvertragsnamen "ArrayOfQueryExpression:http://schemas.microsoft.com/xrm/2011/Contracts" wurde nicht erwartet. Fügen Sie alle statisch nicht bekannten Typen der Liste der bekannten Typen hinzu. Verwenden Sie dazu z. B. das Attribut "KnownTypeAttribute", oder fügen Sie die Typen der an DataContractSerializer übergebenen Liste von bekannten Typen hinzu.". Weitere Details finden Sie unter "InnerException".

    I just have it on German but it means that there is an error in serialization: The type MyNamespace.QueryExpression[] with the data contract name "ArrayOfQueryExpression:http://schemas.microsoft.com/xrm/2011/Contracts" was not found.

    I also tried to add the type QueryExpression[] tot the list of known types of the OrganizationRequest but hat no luck. I think I has something to do with the QuerySet Attribute.

    Any Ideas?


    Thomas Flückiger Flückiger Informatik http://www.f-in.ch

Tutte le risposte

  • giovedì 1 marzo 2012 22:07
     
     

    Thomas,

    Did you ever get around this? I have exactly the same issue.

    Thanks

  • giovedì 1 marzo 2012 22:21
     
     

    Just to expand on that a bit, the QuerySet parameter is expecting what looks like an array of QueryExpression objects.

    I think I need to define QueryExpression[] as a known type but when I tried that it threw a different exception.

    The OrganizationRequest parametercollection in my service reference is defined as an ObservableCollection<KeyValuePair<string, object>>.

    There is a class in the SDK example called CollectionExtensions which appears to extend IList<KeyValuePair<TKey, TValue> with a method called SetItem which turns an Array into a List, although as far as I can see this isn't being called. ParameterCollection is derived from ObservableCollection which is an IList.

    I mention it only because the comment in it says this:

                //If the value is an array, it needs to be converted into a List. This is due to how Silverlight serializes
                //Arrays and IList<T> objects (they are both serialized with the same namespace). Any collection objects will
                //already add the KnownType for IList<T>, which means that any parameters that are arrays cannot be added
                //as a KnownType (or it will throw an exception).

    which sounds related as I have an array of QueryExpression objects.

    My attempt to set up the request looks like this:

                OrganizationRequest request = new OrganizationRequest {RequestName = "BulkDelete"};
                request["QuerySet"] = new[] { qe };
                request["JobName"] = "Test Bulk Delete";
                request["SendEmailNotification"] = false;
                request["ToRecipients"] = new Guid[] {};
                request["CCRecipients"] = new Guid[] { };
                request["RecurrencePattern"] = String.Empty;
                request["StartDateTime"] = new DateTime();




  • giovedì 1 marzo 2012 23:46
     
     Risposta suggerita

    I think I've sussed it. It works if you use ObservableCollection<QueryExpression> (and ObservableCollection<Guid> for the To & Cc recipients).

    I figured it out because I tried applying  [KnownType(typeof(QueryExpression[]))] to the OrganizationReqest again and the message said:

    System.InvalidOperationException: Type 'Microsoft.Xrm.Sdk.QueryExpression[]' cannot be added to list of known types since another type 'System.Collections.ObjectModel.ObservableCollection`1[Microsoft.Xrm.Sdk.QueryExpression]' with the same data contract name 'http://schemas.microsoft.com/xrm/2011/Contracts:ArrayOfQueryExpression' is already present.

    In my case this is a bit of pain because I'm trying to share code between Silverlight & .Net and the CRM SDK assemblies won't work with an ObservableCollection, I guess I'm going to have to use a compiler switch in this instance.

    • Proposto come risposta Laughing John venerdì 2 marzo 2012 10:19
    •  
  • venerdì 2 marzo 2012 10:19
     
     

    Just in case it helps someone else, I ended up doing this:

    #if SILVERLIGHT
                request["QuerySet"] = new ObservableCollection<QueryExpression> { qe };
                request["ToRecipients"] = new ObservableCollection<Guid>();
                request["CCRecipients"] = new ObservableCollection<Guid>();
    #else
                request["QuerySet"] = new[] { qe };
                request["ToRecipients"] = new Guid[] {};
                request["CCRecipients"] = new Guid[] { };
    #endif
                request["JobName"] = "Test Bulk Delete";
                request["SendEmailNotification"] = false;
                request["RecurrencePattern"] = String.Empty;
                request["StartDateTime"] = new DateTime();