How to update the quick find field in a SaveQuery entity of type SavedQueryQueryType.QuickFindSearch using C# code?

65 Views Asked by At

I want to update the quick find field <new_prim1> in the default quick find view of a table using FetchXML. The original FetchXML looks like this:

<fetch version="1.0" mapping="logical">
    <entity name="new_testtable2">
        <attribute name="new_testtable2id" />
        <attribute name="new_prim1" />
        <attribute name="createdon" />
        <order attribute="new_prim" descending="false" />
        <filter type="and">
            <condition attribute="statecode" operator="eq" value="0" />
        </filter>
        <filter type="or" isquickfindfields="1">
            <condition attribute="new_prim1" operator="like" value="{0}" />
        </filter>
    </entity>
</fetch>

The new FetchXML should look like this, where <new_prim1> is replaced by <new_name>:

<fetch version="1.0" mapping="logical">
    <entity name="new_testtable2">
        <attribute name="new_testtable2id" />
        <attribute name="new_name" />
        <attribute name="createdon" />
        <order attribute="new_prim" descending="false" />
        <filter type="and">
            <condition attribute="statecode" operator="eq" value="0" />
        </filter>
        <filter type="or" isquickfindfields="1">
            <condition attribute="new_name" operator="like" value="{0}" />
        </filter>
    </entity>
</fetch>

I first rerieved the ID of the SavedQuery with type SavedQueryQueryType.QuickFindSearch:

{
    QueryExpression query = new QueryExpression("savedquery")
    {
        ColumnSet = new ColumnSet("savedqueryid", "name", "fetchxml", "layoutxml")
    };
    query.Criteria.AddCondition("returnedtypecode", ConditionOperator.Equal, "new_testtable2");
    query.Criteria.AddCondition("querytype", ConditionOperator.Equal, SavedQueryQueryType.QuickFindSearch);

    RetrieveUnpublishedMultipleRequest retrieveRequest = new RetrieveUnpublishedMultipleRequest()
    {
        Query = query
    };

    RetrieveUnpublishedMultipleResponse retrieveResponse = (RetrieveUnpublishedMultipleResponse)_serviceClient.Execute(retrieveRequest);

    Entity firstView = views.Entities(0);
    Guid viewId = firstView.Id;
}

I then create a new SavedQuery entity and put in the new FetchXML:

{
    Entity sq = new Entity("savedquery");
    sq.Attributes("savedqueryid") = viewId;
    sq.Attributes("fetchxml") = "<fetch version='1.0' mapping='logical'><entity name='new_testtable2'><attribute name='new_testtable2id' /><attribute name='new_prim' /><attribute name='createdon' /><order attribute='new_prim' descending='false' /><filter type='and'><condition attribute='statecode' operator='eq' value='0' /></filter><filter type='or' isquickfindfields='1'><condition attribute='new_prim' operator='like' value='{0}' /></filter></entity></fetch>";

    UpdateRequest updateRequest = new UpdateRequest();

    updateRequest.Target = sq;
    UpdateResponse updateResponse = (UpdateResponse)_serviceClient.Execute(updateRequest);
}

The code exists with {"An unexpected error occurred."}

When leaving out the isquickfindfields='1' option, it is working. I cannot get it to work to change the Quick Field of the View. What am I overlooking?

1

There are 1 best solutions below

0
Harinarayanan On

Does the layout xml match the new fetch columns?

new_name field is added to the fetchxml and the same should be added to the layoutxml before the view can be updated. as there is an internal validation to match the same.