Using the Saved Queries Feature

The new Saved Queries feature will allow you to define a fetch to call upon with a JQuery Ajax request as a web service. The fetched results will be returned as an object, which you can parse into JSON and loop through. This feature is capable of supporting up to sixteen different parameters to make your fetch more dynamic.

Creating a Saved Query

  1. First, navigate to the Saved Queries option located underneath The Portal Connector menu option.

    save-query
  2. This will contain a list of your Saved Queries. In order to create one, click on the Create option located at the top. You will be given access to the FetchXML builder to create a query with. For this example, this query will fetch all contacts with a pair of parameters, one for the first name and another for the contacts' last name.

  3. saved-query 

    You can also add static filters to your fetchXML, Please follow this link for more information on static filters : https://www.crmportalconnector.com/developer-network/documentation/how-tos/mvc-based-widgets/static-fetch-xml-filter
  4. The fetch above will generate the Service URL Preview of http://contoso.crmportalconnector.com/SavedQueryService/Execute/fetch-contacts-with-this-first-name/P1/P2. This is used to preview the records returned by your fetch.

    You are allowed to use up to sixteen parameters to filter your results. @P1@ through @P16@ represent your parameters and can be replaced with anything you'd like.

    For example, replacing P1 with J will attempt to retrieve all contacts that have a J in their first name. P2 represents your second parameter and can be replaced with anything you'd like. Replacing P2 with B will attempt to retrieve all contacts that have a J in their first name as well as a B in their last name. The link will look like this: http://contoso.crmportalconnector.com/SavedQueryService/Execute/fetch-contacts-with-this-first-name/J/B. Adding a parameter to a saved query is similar to using the filters feature in The Portal Connector.

    To declare a parameter, simply add @P1@ through @P16@ anywhere you'd like in the fetch. The Service URL Preview will change accordingly.

    Additionally, the parameters feature also supports the use of encrypted Guids as Guids passed through the web service are unencrypted by default. For example, if a parameter is set to filter on Contact ID, you can specify @P1:encrypted@ in your Fetch. The web service will attempt to decrypt this Guid before fetching the results.

  5. Once you have finished creating your saved query, click Publish to save it. Please note that only published saved queries can be called upon.

Using JavaScript and JQuery to Work with your Saved Query

You can use the Jquery $.ajax() function to retrieve the results of your saved query. Then you can parse the results as a JSON object in order to work with them. Here is an example of how to do this:

  1. Open your developer console on any JQuery enabled page and enter the following:

    var x = $.ajax("http://contoso.crmportalconnector.com/SavedQueryService/Execute/fetch-contacts-with-this-first-name/A/E")
    

  2. This will return an object containing all Contacts wherein the first name will contain A and the last name will contain E. Here are some of the values returned from the Fetch:

    [{"Attributes":{"contactid":"4da0e5b9-88df-e311-b8e5-6c3be5a8b200","emailaddress1":"brian@blueyonderairlines.com","firstname":"Brian","lastname":"LaMee"},"FormattedValues":{},"Id":"4da0e5b9-88df-e311-b8e5-6c3be5a8b200","LogicalName":"contact"},
    {"Attributes":{"contactid":"55a0e5b9-88df-e311-b8e5-6c3be5a8b200","emailaddress1":"evacorets@fabrikam.com","firstname":"Eva","lastname":"Corets"},"FormattedValues":{},"Id":"55a0e5b9-88df-e311-b8e5-6c3be5a8b200","LogicalName":"contact"},
    {"Attributes":{"contactid":"5fa0e5b9-88df-e311-b8e5-6c3be5a8b200","emailaddress1":"psteiner@lucernepublishing.com","firstname":"Patrick","lastname":"Steiner"},"FormattedValues":{},"Id":"5fa0e5b9-88df-e311-b8e5-6c3be5a8b200","LogicalName":"contact"}]
    

  3. In order to loop through these objects, you will have to parse the returned object as a JSON object and store it in a variable. The below example will loop through the fetched records and will log the concatenated first and last names to the console. Please note that setting async to false will make this call synchronous. This means that the browser will wait until all of the results have been returned:

    var resp = $.ajax({ 
    	url: "http://contoso.crmportalconnector.com/SavedQueryService/Execute/fetch-contacts-with-this-first-name/E/A", 
    	async: false, 
    	dataType: "JSON", 
    	}).responseJSON;
    for(var i = 0; i < resp.length; i++) 
    { 
    	console.log(resp[i].Attributes.contactid) 
    } 
    

  4. This will return a list of matching contacts with their first and last names concatenated.

Saved Query Permissions

Another facet of functionality for saved queries involves using the Sitefinity permissions feature to allow or deny access to a particular saved query based on one's permission level. Much like editing the permissions for a page or a widget, you can specify permissions for a saved query. In order to access this, you must do the following:

  1. From the Saved Queries page, click on the Actions drop down list.

    savequery-permissions

  2. Select the Permissions option to open the permissions page for this saved query. This will display a list of permissions criteria (Who can View, Modify, Delete, etc.) along with a list of accompanying users for each criteria.

    In the context of saved queries, the view permission determines which users are allowed to execute the query. Groups of users that aren't allowed to view this query will also not be able to execute it.

    savequery-permission-settings