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.