Thursday, March 31, 2022

Sitecore Powershell script - Mapping JSON into Sitecore Items

 With Sitecore everything seems to be possible. We can convert the Sitecore Items into JSON format and vice versa.

Recently we have received once such requirement of converting third party JSON in Sitecore Items. I would like to share my experience of exploring Conversion of JSON into Item using Sitecore Powershell.

      Given the option for Content authors to choose the file and store in Temp folder of Sitecore CM server(depends on scenario this can be modified).
     Based on the JSON file format, I have created the Sitecore Items.

     Here goes my Sample JSON:
    
    

As per this sample JSON, My field mappings goes little different may not be the same.

Employee Template===>                   

    EmployeeId-->       "emplid"
           EmployeeName -->  "name"    
                   EmployeeAddress--> "address"    
                                                EmployeeContactNumber--> "contact_number"
                                                     EmployeeDept-->  "Node_name" (eg:"IT","CS")


Please find the powershell snippet:

$dataFolder = [Sitecore.Configuration.Settings]::DataFolder
$tempFolder = $dataFolder + "\temp\upload"
$filePath = Receive-File -Path $tempFolder -overwrite
$itemPath ="/sitecore/content/sample-prjct/Content/EmployeeData"
$templateID="{70CD4D36-64AB-4450-8A5A-E0BC18A962E2}"

$a = Get-Content -Path $filePath  | ConvertFrom-Json

$a = $a.psobject.properties | select name,value 

$a | foreach { 
    $_.value =   $_.value.psobject.properties | select name,value 

    try
{
    $item = New-Item -Path $itemPath -Name $_.value.value[1]  -ItemType $templateID
}
catch
{
    write-host "Failed to create Item: " $itemPath                
write-host $_.Exception.Message    
exit
}
if($item){
    
$item.Editing.BeginEdit()
$item["EmployeeId"]= $_.value.value[0]
$item["EmployeeName"]= $_.value.value[1]
$item["EmployeeAddress"]= $_.value.value[2]
$item["EmployeeContactNumber"]= $_.value.value[3]
$item["EmployeeDept"]=$_.Name
$item.Editing.EndEdit()
}
}

Sitecore Template

Powershell Script execution  - Outcomes

                   I have attached the outcomes after the execution of powershell scripts.  




If the items has been created successfully, the response would be true as in the attached snapshot.


     Final Item in Sitecore





Quick Flowchart Representation






Happy Sitecore learning!!!

Friday, March 11, 2022

Troubleshooting Sitecore GraphQL search query using SOLR - Part-2

 In this part, I will explain the troubleshooting steps with a sample example. 

 Use case: 

        In this scenario mentioned below, the articles are tagged using different Grades. I have used the Sitecore Tagging repository to create the tag. Based on the Grades(Tags), they should be grouped and displayed in the application and for fetching it from Sitecore we are using GraphQL query.

Sitecore Structure:

Article-List:


Tag Repository:

Mapping between tag and Articles:


SOLR Interface

        We have to identify, how the fields are getting indexed in SOLR. Let's take a single item "full path" and check it in the SOLR server.

 solr query: _fullpath:"/sitecore/content/sample-prjct/Content/MainItem/Article/Article1"



Check the field name semantics in the SOLR result.


Now we have identified how the field is getting displayed in index and value stored. Will start the Sitecore GraphQL query and check the results of the article.

    Sitecore GraphQL

                   I have searched the article which has been tagged Grade-1 (Tag) under the Article folder path and it returned the result.
                 

SOLR Interface                   

            Now troubleshoot the same query in the SOLR server and execute it to check the result.

              solr-query: _path:"4a0feb9686dd44c68a6d0cb112e54a46" && __semantics:"a4f9f07cb31b42ca98805433fadfcf63"




Here the results are matching if in any case, both do not seem to return the same result then Rebuild the index from Index Manager.