Surendra Sharma

Surendra Sharma

Search This Blog

Friday, November 15, 2019

How to get Sitecore multilist items details in GraphQL

I have graphQL query for my JSS app as

{
  search(fieldsEqual: [{name: "_fullpath", value: "/sitecore/content/abc"}]
  , keyword: "") {
    results {
      items {
        item {
          ... on Brand {
            heading {
              value
            }
            questionList {
              value
            }
          }
        }
      }
    }
  }
}

Here "QuestionList" is a list field for which I am getting output with Item ids separated by pipe "|" as

"questionList": {
  "value": "{79BADBCE-3824-4664-B74C-D5D931154E86}|{2D0FCE78-8377-4A1E-B47B-A3BDBB6D8D62}"
}

But instead of this items ids, how to get multilist items details?

For this we must use "targetItems" cluase as

{
  search(fieldsEqual: [{name: "_fullpath", value: "/sitecore/content/abc"}
   ], keyword: "") {
    results {
      items {
        item {
          ... on Brand {
            heading {
              value
            }
            questionList {
              value
              name
              targetItems {
                id
                name
                ... on Question
                {
                  questionstatement {value}
                }
              }
            }
          }
        }
      }
    }
  }
}

As you can observe here we can use strongly type items "Question" inside of "targetItems". Its output is

{
  "data": {
    "search": {
      "results": {
        "items": [
          {
            "item": {
              "heading": {
                "value": "This is Heading"
              },
              "questionList": {
                "value": "{79BADBCE-3824-4664-B74C-D5D931154E86}|{2D0FCE78-8377-4A1E-B47B-A3BDBB6D8D62}",
                "name": "questionList",
                "targetItems": [
                  {
                    "id": "79BADBCE38244664B74CD5D931154E86",
                    "name": "Question 1",
                    "questionstatement": {
                      "value": "Question 1"
                    }
                  },
                  {
                    "id": "2D0FCE7883774A1EB47BA3BDBB6D8D62",
                    "name": "Question 2",
                    "questionstatement": {
                      "value": "Question 2"
                    }
                  }
                ]
              }
            }
          }
        ]
      }
    }
  }
}

I hope this trick helps you to write GraphQL queries in better way for Sitecore JSS app.

Tuesday, October 29, 2019

Multilist in Sitecore JSS App

For the list data, Sitecore JSS offering "ContentList" as a field datatype. When you deploy it in Sitecore, it converts into "Treelist".

But what to do if you need "Multilist" instead of Treelist at Sitecore side?

Solution:
 

You have to specify "MultiList" as a string type when you define the Sitecore field definition for the component as

name: 'content'type: CommonFieldTypes.RichText },
name: 'articleList'type: 'MultiList'},

As you can notice here I defined "ArticleList" field as a "MultiList" datatype.

You can also set the datasource or query with this Multilist field definition as


name: 'articleList'type: 'MultiList'source: `query:./Articles/*`, },

Here I have set the datasource as a child folder "articles" of current Sitecore context item.

I hope this quick tip help you in Sitecore JSS App development.

Wednesday, October 23, 2019

Problem with Sitecore GraphQL on Azure

We have deployed JSS App on Sitecore instance running on Azure.

For one of the component we are using GraphQL which is perfectly working on local machine.


But when we deploy it on Azure our GraphQL query was not returning anything.

Our query was


var query = `{
search(fieldsEqual: [{name: "_fullpath",
value: "/sitecore/content/myjssproj/Content/BlogsCollection/ContentListField/*"}
])

As you can note, we have used "fieldsEqual" parameter and pass the name and value. You will get this query result on your local machine but this is not going to work on Azure.

I think this is because we are using "Azure Search" and Azure have limitation of 1000 fields index. It may be possible that Azure search not indexed system fields.

So if you want to use Azure Search with GraphQL you should rewrite your query with "rootItem" parameter as


var query = `{
search(rootItem: "/sitecore/content/myjssproj/Content/BlogsCollection/ContentListField/*"
)

Other alternative is to use SOLR as a search on Azure. I find this link is very simple and handy to install SOLR on azure.